1. 程式人生 > >設計模式(八)裝飾模式

設計模式(八)裝飾模式

裝飾模式(Decotator):動態地給一個物件新增一些額外的職責,就增加功能來說,裝飾模式比生成子類更加靈活

這是在網上找的關於裝飾模式的類圖,

http://img0.imgtn.bdimg.com/it/u=3834882730,1020120806&fm=214&gp=0.jpg

Component是定義一個物件的介面,可以給這些物件動態地新增職責,ConcreteComponet是定義了一個具體的物件,也可以給這個物件新增一些職責。Decorator裝飾抽象類,繼承了Component從外類擴充套件Componet的功能。至於ConcreteDecorator就是具體的裝飾物件

public abstract class Component {
    public abstract void operation();
}
public class ConcreteComponent extends Component {
    @Override
    public void operation() {
        System.out.println("具體物件的操作");
    }
}
public abstract class Decorator extends Component{
    protected Component component;

    public void setComponent(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        if (component!=null)
        {
            component.operation();
        }
    }
}
public class ConcreteDecoratorA  extends Decorator{
    private String addedState;
    @Override
    public void operation() {
        super.operation();
        addedState="new State";
        System.out.println("具體裝飾物件A的操作");
    }
}
public class ConcreteDecoratorB extends Decorator {
    @Override
    public void operation() {
        super.operation();
        addBehavior();
    }
    private void addBehavior()
    {
        System.out.println("具體裝飾物件B的操作");
    }
}
public class Main {
    public static void main(String[] args) {
        ConcreteComponent c=new ConcreteComponent();
        ConcreteDecoratorA d1=new ConcreteDecoratorA();
        ConcreteDecoratorB d2=new ConcreteDecoratorB();
        //用裝飾類A去裝飾c
        d1.setComponent(c);
        //用裝飾類A去裝飾C
        d2.setComponent(d1);
        d2.operation();
    }
}

下面再舉一個具體應用到裝飾物件的例子:

一個人要穿衣服,搭配服飾。下面是具體的實現:

public class Person {
    private String name;
    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }
    public void show()
    {
        System.out.println("裝扮的"+name);
    }

}
public abstract class Finery extends Person{
    public Person person;

    public void setPerson(Person person) {
        this.person = person;
    }

    @Override
    public void show() {
        if(null!=person)
        {
            person.show();
        }
    }
}
public class BigTrouser extends Finery {
    @Override
    public void show() {
        System.out.print("大垮褲"+" ");
        super.show();
    }
}
public class Dress extends Finery {
    @Override
    public void show() {
        System.out.print("裙子"+" ");
        super.show();
    }
}
public class Shoes extends Finery{
    @Override
    public void show() {
        System.out.print("破球鞋"+" ");
        super.show();
    }
}
public class TShirts extends Finery {
    @Override
    public void show() {
        System.out.print("大T恤"+" ");
        super.show();
    }
}
public class Main {
    public static void main(String[] args) {
        Person p=new Person("小明") ;
        System.out.println("第一種打扮");
        TShirts tShirts=new TShirts();
        Dress dress=new Dress();
        Shoes shoes=new Shoes();
        tShirts.setPerson(p);
        dress.setPerson(tShirts);
        shoes.setPerson(dress);
        shoes.show();

        System.out.println("第二種打扮");
        Shoes shoes1=new Shoes();
        BigTrouser bigTrouser=new BigTrouser();
        shoes1.setPerson(p);
        bigTrouser.setPerson(shoes1);
        bigTrouser.show();

    }
}

裝飾模式的優點就是把類中的裝飾功能從類中搬移去除,這樣可以簡化原有的類

有效地把類的核心職責和裝飾功能區分開了,而且可以去除相關類中的重複值邏輯