策略模式
策略模式
1.策略模式,
定义算法族,分别封装起来, 让它们之间可以互相替换, 此模式让算法的变化独立于使用算法的客户端。
class Duck{
constructor(opts){
this.name = opts.name || "";
this.sex = opts.sex || "";
this.flyBehavi = opts.flyer;
this.swimBehavi = opts.swimer;
}
fly(){
this.flyBehavi.fly();
}
swim(){
this.swimBehavi.swim();
}
}
class Flyer{
fly(){
console.info("i am flyer");
}
}
class Swimer{
swim(){
console.info("i am swimer");
}
}
class NewDuck extends Duck{
constructor(opts){
super(opts);
this.name = opts.name;
}
}
class NoFlyer extends Flyer{
fly(){
console.info("i can not fly");
}
}
let a = new NewDuck({name:"哈哈", flyer:new NoFlyer(), swimer:new Swimer()});
a.fly();
a.swim();
简单来说,就是每次调用对象的方法