大话设计模式-装饰模式

本文转载自点击打开链接

需求
开发一个类似于QQ的服饰搭配系统。
实现
级别1

//person类代码  
public class Person  
{  
    private String name;  
    public Person(String name)  
    {  
        this.name = name;  
    }  
    public void wearTShirts()  
    {  
        System.out.println("大T恤");  
    }  
    public void wearBigTrouser()  
    {  
        System.out.println("大裤衩");  
    }  
    public void wearSneakers()  
    {  
        System.out.println("破球鞋");  
    }  
    public void wearSuit()  
    {  
        System.out.println("西装");  
    }  
    public void wearTie()  
    {  
        System.out.println("领带");  
    }  
    public void wearLeatherShoes()  
    {  
        System.out.println("皮鞋");  
    }  
    public void show()  
    {  
        System.out.println("装扮的" + name);  
    }  
}  
//客户端代码 
public class Main  
{  
    public static void main(String[] args)  
    {  
        Person person = new Person("小菜");  
        System.out.println("第一种装扮:");  
        person.wearTShirts();  
        person.wearBigTrouser();  
        person.wearSneakers();  
        person.show();  
        System.out.println("第二种装扮:");  
        person.wearSuit();  
        person.wearTie();  
        person.wearLeatherShoes();  
        person.show();  
    }  
}  
级别2

//Person类  
public class Person  
{  
    private String  name;    
    public Person(String name)  
    {  
        this.name = name;  
    }  
    public void show()  
    {  
        System.out.println("装扮的" + name);  
    }  
}  
//服饰抽象类  
public interface Finery  
{  
    public void show();  
}  
public class TShirts implements Finery  
{  
    public void show()  
    {  
        System.out.println("大T恤");  
    }  
}
public class BigTrouser implements Finery  
{  
    public void show()  
    {  
        System.out.println("大裤衩");  
    }  
}  
public class Sneakers implements Finery  
{  
    public void show()  
    {  
        System.out.println("破球鞋");  
    }  
} 
public class Suit implements Finery  
{  
    public void show()  
    {  
        System.out.println("西装");  
    }  
}  
public class Tie implements Finery  
{  
    public void show()  
    {  
        System.out.println("领带");  
    }  
}  
public class LeatherShoes implements Finery  
{  
    public void show()  
    {  
        System.out.println("皮鞋");  
    }  
} 
//客户端代码  
public class Main  
{  
    public static void main(String[] args)  
    {  
        Person person = new Person("小菜");  
        System.out.println("第一种装扮:");  
        Finery dtx = new TShirts();  
        Finery kk = new BigTrouser();  
        Finery pqx = new Sneakers();  
        dtx.show();  
        kk.show();  
        pqx.show();  
        person.show();  
        System.out.println("第二种装扮:");  
        Finery xz = new Suit();  
        Finery ld = new Tie();  
        Finery px = new LeatherShoes();  
        xz.show();
        ld.show();  
        px.show();  
        person.show();  
    }  
}  
级别3

装饰模式能够动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。java的IO流就是典型的装饰模式。

装饰模式的基本代码:

//Component接口  
public interface Component  
{  
    public void operation();  
}  
//ConcreteComponent类  
public class ConcreteComponent implements Component  
{  
    public void operation()  
    {  
        System.out.println("具体对象的操作");  
    }  
}  
//Decorator类  
public class Decorator implements Component  
{  
    protected Component component;  
    public void operation()  
    {  
        if (null != component)  
        {  
            component.operation();  
        }  
    }  
    public Component getComponent()  
    {  
        return component;  
    }  
    public void setComponent(Component component)  
    {  
        this.component = component;  
    }  
}  
//装饰类  
public class ConcreteDecoratorA extends Decorator  
{  
    private String addedState;  
    public void operation()  
    {  
        super.operation();  
        addedState="new state";  
        System.out.println("具体装饰对象A的操作");  
    }  
}  
public class ConcreteDecoratorB extends Decorator  
{  
    public void operation()  
    {  
        super.operation();  
        addedBehavior();  
        System.out.println("具体装饰对象B的操作");  
    }  
    private void addedBehavior()  
    {  
    }  
}  
//客户端代码  
public class Main  
{  
    public static void main(String[] args)  
    {  
        ConcreteComponent c = new ConcreteComponent();  
        ConcreteDecoratorA d1 = new ConcreteDecoratorA();  
        ConcreteDecoratorB d2 = new ConcreteDecoratorB();  
        d1.setComponent(c);  
        d2.setComponent(d1);  
        d2.operation();  
    }  
}
按照装饰模式修改后的代码:
//Person类(ConcreteComponent)  
public class Person  
{  
    private String name;  
    public Person()  
    {  
    }  
    public Person(String name)  
    {  
        this.name = name;  
    }  
    public void show()  
    {  
        System.out.println("装扮的" + name);  
    }  
} 
//服饰类(Decorator)  
public class Finery extends Person  
{  
    protected Person component;  
    public void decorate(Person component)  
    {  
        this.component = component;  
    }  
    public void show()  
    {  
        if (null != component)  
        {  
            component.show();  
        }  
    }  
}
public class TShirts extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("大T恤");  
    }  
}
public class BigTrouser extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("大裤衩");  
    }  
}  
public class Sneakers extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("破球鞋");  
    }  
}  
public class Suit extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("西装");  
    }  
}  
public class Tie extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("领带");  
    }  
}
public class LeatherShoes extends Finery  
{  
    public void show()  
    {  
        super.show();  
        System.out.println("皮鞋");  
    }  
}
//客户端代码  
public class Main  
{  
    public static void main(String[] args)  
    {  
        Person person = new Person("小菜");  
        System.out.println("第一种装扮:");    
        Sneakers pqx = new Sneakers();  
        BigTrouser kk = new BigTrouser();  
        TShirts dtx = new TShirts();  
        pqx.decorate(person);  
        kk.decorate(pqx);  
        dtx.decorate(kk);  
        dtx.show();  
        System.out.println("第二种装扮:");  
        LeatherShoes px = new LeatherShoes();  
        Tie ld = new Tie();  
        Suit xz = new Suit();  
        px.decorate(person);  
        ld.decorate(px);  
        xz.decorate(ld);  
        xz.show();  
    }  
}
我们再举一个例子。现在需要一个汉堡,主体是鸡腿堡,可以选择添加生菜、酱、辣椒等等许多其他的配料,这种情况下就可以使用装饰者模式。

汉堡基类:

public abstract class Humburger 
{        
    protected  String name;  
    public String getName()
    {  
        return name;  
    }    
    public abstract double getPrice();  
}  
鸡腿堡类:
public class ChickenBurger extends Humburger
{        
    public ChickenBurger()
    {  
        name = "鸡腿堡";  
    }  
    @Override  
    public double getPrice() 
    {  
        return 10;  
    }  
}  
配料的基类:
public abstract class Condiment extends Humburger
{        
    public abstract String getName();  
}
生菜类:
public class Lettuce extends Condiment 
{        
    Humburger humburger;     
    public Lettuce(Humburger humburger)
    {  
        this.humburger = humburger;  
    }  
    @Override  
    public String getName() 
    {  
        return humburger.getName()+" 加生菜";  
    }  
    @Override  
    public double getPrice() 
    {  
        return humburger.getPrice()+1.5;  
    }  
} 
辣椒类:
public class Chilli extends Condiment
{        
    Humburger humburger;     
    public Chilli(Humburger humburger)
    {  
        this.humburger = humburger;  
          
    }  
    @Override  
    public String getName() 
    {  
        return humburger.getName()+" 加辣椒";  
    }  
    @Override  
    public double getPrice()
    {  
        return humburger.getPrice();  
    }  
}
测试一下:
public class Main
{     
    public static void main(String[] args)
    {  
        Humburger humburger = new ChickenBurger();  
        System.out.println(humburger.getName()+"  价钱:"+humburger.getPrice());  
        Lettuce lettuce = new Lettuce(humburger);  
        System.out.println(lettuce.getName()+"  价钱:"+lettuce.getPrice());  
        Chilli chilli = new Chilli(humburger);  
        System.out.println(chilli.getName()+"  价钱:"+chilli.getPrice());  
        Chilli chilli2 = new Chilli(lettuce);  
        System.out.println(chilli2.getName()+"  价钱:"+chilli2.getPrice());  
    }  
}  










你可能感兴趣的:(大话设计模式-装饰模式)