装饰者模式

今天把java编程思想的IO系统重新温习了一下,对装饰者模式有了更加深刻的理解。下面以我的理解重新构思了程序,直接上code

Human(人类)-我们需要装饰的对象

 1 /**
2 *
3 */
4 package com.decorate;
5
6 /**
7 * @author Administrator
8 *
9 */
10 public interface Human {
11
12 /**
13 *
14 */
15 public void printDescription();
16 }

 

由Human派生出来的两个被装饰类Man(男人)和Woman(女人)

/**
*
*/
package com.decorate;

/**
*
@author Administrator
*
*/
public class Man implements Human {

/* (non-Javadoc)
* @see com.decorate.Human#printDescription()
*/
@Override
public void printDescription() {
System.out.println("男人");
}

}
 
/**
*
*/
package com.decorate;

/**
*
@author Administrator
*
*/
public class Woman implements Human {

/* (non-Javadoc)
* @see com.decorate.Human#printDescription()
*/
@Override
public void printDescription() {
System.out.println("女人");
}

}

 

有了需要装饰的对象,我们开始实现装饰者。首先需要一个所有装饰者基类,即抽象类Decorator,以后定义的所有装饰者都有继承自这个类

/**
*
*/
package com.decorate;

/**
*
@author Administrator
*
*装饰人的抽象类
*/
public abstract class Decorator implements Human {

/**
* 被装饰的人
*/
public Human human;

public Decorator(Human human){
this.human = human;
}

/* (non-Javadoc)
* @see com.decorate.Human#printDescription()
*/
@Override
public void printDescription() {
human.printDescription();
}

}


然后我们开始进行具体的装饰,比如:你想把这个人装饰成黄皮肤的、长头发的,还是他吃饭、散步....  下面是三个具体的装饰者:

1.长头发

/**
*
*/
package com.decorate;

/**
*
@author Administrator
*
* 把human变成长头发
*/
public class HairstyleDecorator extends Decorator {

/**
*
@param human
*/
public HairstyleDecorator(Human human) {
super(human);
// TODO Auto-generated constructor stub
}

/* (non-Javadoc)
* @see com.decorate.Decorator#printDescription()
*/
public void printDescription(){
System.out.print("长头发-");
super.printDescription();
}

}

2.黄皮肤

/**
*
*/
package com.decorate;

/**
*
@author Administrator
* 把human变成黄皮肤
*/
public class YellowskinDecorator extends Decorator {

/**
*
@param human
*/
public YellowskinDecorator(Human human) {
super(human);
// TODO Auto-generated constructor stub
}

public void printDescription(){
System.out.print("黄皮肤-");
super.printDescription();
}

}

3.让human散步

/**
*
*/
package com.decorate;

/**
*
@author Administrator
* 使human在散步
*/
public class WalkingDecorator extends Decorator {

/**
*
@param human
*/
public WalkingDecorator(Human human) {
super(human);
// TODO Auto-generated constructor stub
}

public void printDescription(){
super.printDescription();
System.out.println("-在散步");
}

}

 

测试类:

/**
*
*/
package com.decorate;

/**
*
@author Administrator
*
*/
public class TestDecorator {

public static void main(String[] args) {
Human human = new Man();
human = new HairstyleDecorator(human);
human.printDescription();

human = new YellowskinDecorator(human);
human.printDescription();

human = new WalkingDecorator(human);
human.printDescription();

System.out.println("------------------------");

Human human1 = new Woman();
human1 = new YellowskinDecorator(human1);
human1.printDescription();

human1 = new HairstyleDecorator(human1);
human1.printDescription();

human1 = new WalkingDecorator(human1);
human1.printDescription();
}
}

运行结果:

长头发-男人
黄皮肤-长头发-男人
黄皮肤-长头发-男人
-在散步
------------------------
黄皮肤-女人
长头发-黄皮肤-女人
长头发-黄皮肤-女人
-在散步








你可能感兴趣的:(装饰者模式)