装饰者模式

装饰模式:动态地给一个对象添加一些额外的职责,就增加功能而言,装饰者模式比生成子类更加灵活。Component是定义一个对象接口,可以给这些对象动态地添加职责,ConcreteComppnent是定义了一个具体的对象,也可以给这个对象添加一些职责,Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的,至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能




上面是装饰者模式Decorator的类图。

实现代码如下:

Component类:

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

ConcreteComponent类:

class  ConcreteCompoent extends Component{
	public void operation{
		System.out.println("具体对象的操作:");
	}
}

Decorator类:

abstract class Decorator extends Compoent{
	protected Component compoent;
	
	pbulic void setComponent(Component component){
		this.component = component;
	}
	
	public void operation(){
		if(component!=null){
			component.Operation();
		}
	}
}


class ConcreteDecortorA extends Decorator{
	private String addedState;
	
	public void operation(){
		super.operation();
		addedState = "new Stare";
		System.out.println("具体装饰对象A的操作");
	}
}


class ConcreteDecortorB extends Decorator{

	
	public void Operation(){
		super.Operation();
		AddedBehavior();
		System.out.println("具体装饰对象B的操作");
	}
	private void AddedBehavior(){
	
	}
}

public class Test{
	public static void main(String[] args) {
		ConcreteComponent c = new ConcreteComponent();
		ConcreteComponentA d1 = new ConcreteComponentA();
		ConcreteComponentB d2 = new ConcreteComponentB();
		
		d1.setComponent(c);
		d2.setComponent(d1);
		d2.operation();
	}
}

装饰者模式是利用setComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decotator类可以是ConcreteComponent的一个子类,同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

下面是一个例子:

public class Person {
	public Person() {
	}

	private String name;

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

	public void show() {

		System.out.println("装扮的:" + name);
	}
}



public class Finery extends Person {

	protected Person component;

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

	public void show() {
		if (component != null) {
			component.show();
		}
	}
}




class TShirts extends Finery {
	public void show() {
		System.out.println("大T恤");
		super.show();
	}
}

class BigTrouser extends Finery {
	public void show() {
		System.out.println("垮裤");
		super.show();
	}
}

class Sneakers extends Finery {
	public void show() {
		System.out.println("破球鞋");
		super.show();
	}
}

class LeatherShoes extends Finery {
	public void show() {
		System.out.println("皮鞋");
		super.show();
	}
}

class Tie extends Finery {
	public void show() {
		System.out.println("领带");
		super.show();
	}
}

class Suit extends Finery {
	public void show() {
		System.out.println("西装");
		super.show();
	}
}



public class Test {
	public static void main(String[] args) {
		Person xc = new Person("菜鸟");
		System.out.println("第一种装扮");

		Sneakers pqx = new Sneakers();
		BigTrouser kk = new BigTrouser();
		TShirts dtx = new TShirts();

		pqx.Decorate(xc);
		kk.Decorate(pqx);
		dtx.Decorate(kk);
		dtx.show();
	}
}



小结:
装饰者模式是为已有功能动态地添加更多功能的一种方式,什么情况用到呢?当系统需要新的功能的时候,在一般情况下,我们想到的是向旧的类中添加新的代码。这些新的代码通常装饰了原有类的核心职责或主要行为,他们在主类中加入了新的字段、新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足一些只有在某种特定情况下才会执行的特殊行为的需要,而装饰者模式可以提供一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。装饰者模式的优点是把类中的装饰功能从类中搬移去除,这样可以简化原有的类;另外一个使有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值