w11 design patterns
This commit is contained in:
11
w10-patterns/factory/index.html
Normal file
11
w10-patterns/factory/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<script src="index.js"></script>
|
||||
<meta charset="utf-8" />
|
||||
</head>
|
||||
|
||||
<body></body>
|
||||
|
||||
</html>
|
||||
26
w10-patterns/factory/index.js
Normal file
26
w10-patterns/factory/index.js
Normal file
@@ -0,0 +1,26 @@
|
||||
class Computer {
|
||||
constructor(brand, os, ram, cpu, ssdSize) {
|
||||
this.brand = brand;
|
||||
this.os = os;
|
||||
this.ram = ram;
|
||||
this.cpu = cpu;
|
||||
this.ssd = ssdSize;
|
||||
}
|
||||
|
||||
showSpects() {
|
||||
console.log(
|
||||
`Brand is ${this.brand} with ${this.os} OS.
|
||||
CPU: ${this.cpu} / RAM: ${this.ram} / SSD size: ${this.ssd}
|
||||
`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Create a Builder for this
|
||||
class ComputerBuilder {}
|
||||
|
||||
// 2. Create a factory
|
||||
// 3. Change it using a singleton
|
||||
class ComputerFactory {}
|
||||
|
||||
console.log('ready');
|
||||
5
w10-patterns/observer/Subject.js
Normal file
5
w10-patterns/observer/Subject.js
Normal file
@@ -0,0 +1,5 @@
|
||||
class Subject {}
|
||||
|
||||
class Observer {}
|
||||
|
||||
export { Subject, Observer };
|
||||
11
w10-patterns/observer/index.html
Normal file
11
w10-patterns/observer/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<script type="module" src="index.js"></script>
|
||||
<meta charset="utf-8" />
|
||||
</head>
|
||||
|
||||
<body></body>
|
||||
|
||||
</html>
|
||||
44
w10-patterns/observer/index.js
Normal file
44
w10-patterns/observer/index.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Observer, Subject } from './Subject.js';
|
||||
|
||||
class Fish {
|
||||
constructor(weight) {
|
||||
super();
|
||||
this.weight = weight;
|
||||
}
|
||||
getWeight() {
|
||||
return this.weight;
|
||||
}
|
||||
getSpecies() {
|
||||
return this.species;
|
||||
}
|
||||
}
|
||||
|
||||
class Salmon extends Fish {
|
||||
constructor() {
|
||||
super(2);
|
||||
this.species = 'Atalntinc salmon';
|
||||
}
|
||||
}
|
||||
|
||||
class Tuna extends Fish {
|
||||
constructor() {
|
||||
super(200);
|
||||
this.species = 'Bluefin tuna';
|
||||
}
|
||||
}
|
||||
|
||||
class Fisherman {
|
||||
constructor(name) {
|
||||
super();
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
// Main
|
||||
const bob = new Fisherman('Bob');
|
||||
|
||||
const fish1 = new Tuna();
|
||||
const fish2 = new Salmon();
|
||||
// 1. subsribe bob (observer) to fish1 and fish2 (subjects)
|
||||
|
||||
// 2. fishes (subjects) notify when they are caught
|
||||
38
w10-patterns/observer/uml.wsd
Normal file
38
w10-patterns/observer/uml.wsd
Normal file
@@ -0,0 +1,38 @@
|
||||
class Subject {
|
||||
- observers: Observer []
|
||||
+ constructor();
|
||||
+ subscribe(observer: Observer);
|
||||
+ unsubscribeAll();
|
||||
+ unsubscribe(observer);
|
||||
+ notifiSubstribers();
|
||||
}
|
||||
|
||||
class Observer {
|
||||
+ {abstract} update(src: Subject);
|
||||
}
|
||||
|
||||
|
||||
class Fish extends Subject {
|
||||
- species: String;
|
||||
- weight: Number;
|
||||
|
||||
+ constructor(weight: Number);
|
||||
+ getWeight(): Number;
|
||||
+ getSpecies(): String;
|
||||
}
|
||||
|
||||
class Salmon extends Fish {
|
||||
+ constructor();
|
||||
}
|
||||
|
||||
class Tuna extends Fish {
|
||||
+ constructor();
|
||||
}
|
||||
|
||||
class Fisherman extends Observer {
|
||||
- name: String;
|
||||
+ constructor(name: String);
|
||||
+ update(src);
|
||||
}
|
||||
|
||||
Fisherman "1" -left- "many" Fish : has
|
||||
Reference in New Issue
Block a user