装饰模式

  装饰模式:

1. 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
2. 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。

 

 

/**
 * Component定义一个对象的接口,可以给这些对象动态地添加职责。
 *
 */
public abstract class Component {
	
	public abstract void operation();

}

 

/**
 * ConcreteComponent定义了一个具体的对象,也可以给这个对象添加一些职责。
 *
 */
public class ConcreteComponent extends Component {

	@Override
	public void operation() {
		System.out.println("具体对象的操作");
	}
}

 

/**
 * 装饰抽象类,继承Component,从外类来扩展Component类的功能。但对于Component来说,是
 * 无须知道Decorator类的存在的。至于ConcreteComponent就是具体的装饰对象,起到给
 * Component添加职责的功能。
 *
 */
public abstract class Decorator extends Component {
	
	protected Component component;

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

	//重写operation,实际执行的是Component的operation();
	@Override
	public void operation() {
		if(component!=null){
			component.operation();
		}
	}

}
/**
 * 具体装饰对象A
 *
 */
public class ConcreteDecoratorA extends Decorator {
	
	// 本类独有的功能,以区别于ConcreteDecoratorrB
	private String addedState;

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

  

/**
 * 具体装饰对象B
 *
 */
public class ConcreteDecoratorB extends Decorator {
	@Override
	public void operation() {
		super.operation();
		this.addBehaviorr();
		System.out.println("具体装饰对象B的操作。");
	}
	/**
	 * 本类独有的方法,以区别于ConcreteDecoratorA
	 */
	private void addBehaviorr(){		
	}
}

 

public class Main {
	/**
	 * @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();
		 装饰模式是利用setComponent来对对象进行包装的,这样每个装饰对象的实现就
		 和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,
		 不需要关心如何被添加到对象链当中。**/

		d1.setComponent(c);
		d2.setComponent(d1);		
		d2.operation();
	}
}

 

 

装饰模式实例:(衣服装扮)

 

/**
 * Person类,具体的对象类ConcreteComponet
 *
 */
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 {
	
	private Person person;	

	// 装扮(setComponent)
	public void decorate(Person person) {
		this.person = person;
	}

	@Override
	public void show() {
		if(person!=null){
			person.show();
		}
	}
}

 

/**
 * 具体的服饰类大T恤(ConcreteDecorator)
 *
 */
public class FineryTShirts extends Finery {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.print("大T恤 ");
		super.show();
	}
}

 

/**
 * 具体的服饰类垮裤(ConcreteDecorator)
 *
 */
public class FineryBigTrouser extends Finery {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.print("垮裤 ");
		super.show();
	}
}

 

/**
 * 具体的服饰类皮鞋(ConcreteDecorator)
 *
 */
public class FineryLeatherShoes extends Finery {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.print("皮鞋 ");
		super.show();
	}
}

 

/**
 * 具体的服饰类运动鞋(ConcreteDecorator)
 *
 */
public class FinerySneakers extends Finery {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		System.out.print("运动鞋 ");
		super.show();
	}
}

 

public class Main {

	public static void main(String[] args) {
		
		Person person = new Person("小王");		
		
		FineryTShirts shirts = new FineryTShirts();
		FineryBigTrouser bigTrouser = new FineryBigTrouser();
		FinerySneakers sneakers = new FinerySneakers();
		FineryLeatherShoes leatherShoes = new FineryLeatherShoes();
		
		System.out.println("第一种装扮:");		
		sneakers.decorate(person);
		bigTrouser.decorate(sneakers);
		shirts.decorate(bigTrouser);
		shirts.show();
		
		System.out.println("第二种装扮:");		
		leatherShoes.decorate(person);
		bigTrouser.decorate(leatherShoes);
		shirts.decorate(bigTrouser);
		shirts.show();		
	}
}

 

 

运行结果如下:

 

第一种装扮:
大T恤 垮裤 运动鞋 装扮的小王
第二种装扮:
大T恤 垮裤 皮鞋 装扮的小王

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值