//工厂模式
class Ldh {
constructor(name,age){
this.name = name;
this.age = age;
}
song(){
console.log(this.name)
}
}
class Creator {
create(name){
return new Ldh(name)
}
}
let creator = new Creator();
let p = creator.create('11')
p.song()
//观察者模式
//订阅者
class Subject {
constructor(){
this.state = 0;
this.observers = []
}
getState(){
return this.state
}
setState(state){
this.state = state;
this.notifyAllObservers()
}
notifyAllObservers(){
this.observers.forEach(observer => {
observer.update()
})
}
attach(observer){
this.observers.push(observer)
}
}
//观察者
class Observer {
constructor(name,subject){
this.name = name
this.subject = subject
this.subject.attach(this)
}
update(){
console.log(this.name,111)
}
}
var s = new Subject()
var o = new Observer('name11', s)
s.setState(1)
console