您的位置:首页 > 其它

大话设计模式之装饰模式

2013-07-26 21:33 351 查看
装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。



package Decorator;

public class DecoratorPattern {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB();
/**
* 首先用ConcreteComponent实例化对象c,然后用ConcreteDecoratorA的
* 实例化对象d1来包装c,再用ConcreteDecoratorB的对象d2包装d1,最终执行d2的Operation()
*/
d1.setComponent(c);
d2.setComponent(d1);
d2.Operation();
}
}

abstract class Component {
public abstract void Operation();
}

class ConcreteComponent extends Component {

@Override
public void Operation() {
System.out.println("Concrete Operation");
}
}

abstract class Decorator extends Component {
protected Component component;

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

@Override
public void Operation() {
// TODO Auto-generated method stub
if (component != null) {
component.Operation();
}
}
}

class ConcreteDecoratorA extends Decorator {
private String addedState;

@Override
public void Operation() {
// TODO Auto-generated method stub
super.Operation();
addedState = "new state";
System.out.println("具体装饰对象A的操作");
}
}

class ConcreteDecoratorB extends Decorator {

@Override
public void Operation() {
// TODO Auto-generated method stub
super.Operation();
addedBehavior();
System.out.println("具体装饰对象B的操作");
}

private void addedBehavior() {
System.out.println("addedBehavior");
}
}


每一个装饰器都应该调用基类的方法。装饰器相当于一个累加的过程,c累加到d1,再把d1累加到d2,类似于d2(d1(c))

实例:

package Decorator;

public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person xc = new Person("jack");
System.out.println("First decoration");

Sneakers sneakers = new Sneakers();
BigTrouser bigTrouser = new BigTrouser();
TShirts tShirts = new TShirts();

sneakers.Decorate(xc);
bigTrouser.Decorate(sneakers);
tShirts.Decorate(bigTrouser);
tShirts.Show();
}

}

//ConcreteComponent class
class Person {
public Person() {}

private String name;
public Person(String name) {
this.name = name;
}

public void Show() {
System.out.println("装扮的"+this.name);
}
}

//Decorator
class Finery extends Person {
protected Person component;

public void Decorate(Person component) {
this.component = component;
}

@Override
public void Show() {
// TODO Auto-generated method stub
if (component != null) {
component.Show();
}
}
}

//ConcreteDecorator
class TShirts extends Finery {
@Override
public void Show(){
System.out.println("T-shirt");
super.Show();
}
}

class BigTrouser extends Finery {
@Override
public void Show() {
System.out.println("big trouser");
super.Show();
}
}

class Sneakers extends Finery {
@Override
public void Show() {
System.out.println("sneaker");
super.Show();
}
}

class Suit extends Finery {
@Override
public void Show() {
System.out.println("suit");
super.Show();
}
}

class Tie extends Finery {
@Override
public void Show() {
System.out.println("Tie");
super.Show();
}
}

class LeatherShoes extends Finery {
@Override
public void Show() {
System.out.println("leatherShoes");
super.Show();
}
}


在装扮1中,tShirt.Show()->super.Show()(调用component.Show()->bigTrouser.Show())->

super.Show()(调用component.Show()->sneakers.Show())->super.Show()(调用component.Show()->person.Show())

Summary:

装饰模式是为已有功能动态的添加更多功能的一种方式。

应用场景:当系统需要新功能的时候,是向旧类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为。通常需要在主类中加入新的字段,新的方法和新的逻辑,从而增加了主类的复杂度。而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

装饰模式的优点:把类中装饰功能从类中搬移去除,这样可以简化原有的类;有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

注意:装饰器的顺序至关重要
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: