zl程序教程

您现在的位置是:首页 >  后端

当前栏目

2022-10-04-装饰者模式与策略模式

模式 2022 10 策略 04 装饰
2023-06-13 09:13:50 时间

装饰者模式

public abstract class Component { //抽象的方法
    public abstract void operate();
}
public class ConcreteComponent extends Component { //具体实现
    @Override  
    public void operate() {
        System.out.println("do Something");
    } 
}

如果需要 ConcreteComponent 的 operate 变化,要么修改 operate 的代码,要么写个类继承 ConcreteComponent,基于它的 operate 做扩展,要么直接写类继承 Component 重写 operate。

这就是继承的缺点,类会越来越多,不好维护。改成用装饰者模式,就是用组合代替了继承,动态地扩展一个实现类的功能。

public abstract class Decorator extends Component { 
    private Component component = null; //通过构造函数传递被修饰者  
    public Decorator(Component _component){
        this.component = _component;
    }  
    //委托给被修饰者执行 @Override  
    public void operate() {
        this.component.operate();
    } 
}
public class ConcreteDecorator1 extends Decorator { //定义被修饰者
    public ConcreteDecorator1(Component _component){ 
        super(_component);
    }  
    //定义自己的修饰方法 
    private void method1(){
        System.out.println("method1 修饰");
    }

    //装饰参数传进来的那个对象的方法
    public void operate(){
        this.method1(); 
        super.operate();
    } 
}
Component component = new ConcreteComponent(); //最初的对象  
component = new ConcreteDecorator1(component); //用一种装饰
component = new ConcreteDecorator2(component); //再用一种装饰 
component.operate();

特点:

  • 装饰类和被装饰类可以独立发展,而不会相互耦合。换句话说,Component 类无须知道 Decorator 类,Decorator 类是从外部来扩展 Component 类的功能,而 Decorator 也不用知道具体的构件。
  • 但多层装饰也会有点复杂,一层一层的包装

例子:InputStream 的子类 FilterInputStream 就是一个抽象装饰者,BufferedInputStream 就是一个具体的抽象装饰者。

策略模式

策略比较简单,实际也用过不少,就是将算法封装起来。比如原来代码里

public class Client {
    public void test() {
        if () {
            // do A
        } else if () {
            // do B
        } else {
            // do C
        }
    }

    public void test2() {
        // 有些地方,又要判断不同的情况执行不同的操作
        if () {
            // do A2
        } else if () {
            // do B2
        } else {
            // do C2
        }
    }
}

会有一些判断,当前是什么情况啦我要做什么事情啊,事情的代码还可能很复杂,现在将它们封装成一种策略,就是什么情况,去执行一下某种策略。

// 一个抽象的策略,可以做什么事情
public interface Strategy {
    public void do();
}

public class StrategyA implements Strategy {
    public void do() {
        // 做若干事情
        // ...
        // ...
    }
    public void do2() {
        // 做若干事情
    }
}

然后用这些策略。

public class Client {
    Strategy strategy
    public void init() {
        if () {
            strategy = new StrategyA(); // 某种情况下,我用 A 策略
        } else if () {
            strategy = new StrategyB(); 
        } else {
            strategy = new StrategyC(); 
        }
    }
    // 让具体的策略执行就行
    public void test() {
        strategy.do();
    }
    public void test2() {
        strategy.do2();
    }
}