装饰者模式和工厂模式

1.java io就是装饰者模式,图看这里https://www.cnblogs.com/intsmaze/p/5202213.html

package main;

abstract class Component{   //组件
    abstract String methodA();
    abstract int methodB();
}
class ConcreteComponent extends Component{  //具体组件
    @Override
    String methodA() {
        return "具体方法A";
    }
    @Override
    int methodB() {
        return 10;
    }
}
abstract class Decorator extends Component{  //装饰
}
class ConcreteDecorator extends Decorator{   //具体装饰
    private Component component;
    ConcreteDecorator(Component component){
        this.component=component;
    }
    @Override
    String methodA() {
        return component.methodA()+",装饰方法A";
    }

    @Override
    int methodB() {
        return component.methodB()+3;
    }
}
public class Test {
    public static void main(String[] args){
        Component component=new ConcreteComponent();
        component=new ConcreteDecorator(component);
        component=new ConcreteDecorator(component);
        component=new ConcreteDecorator(component);
        System.err.println(component.methodA());
        System.err.println(component.methodB());
    }
}

2.简单工厂模式(靠if来判断),工厂模式(靠子类来生成),抽象工厂模式(选择一个工厂,生产的全部是这个工厂的产品),看下面的笔记http://www.runoob.com/design-pattern/abstract-factory-pattern.html

你可能感兴趣的:(设计模式)