设计模式——装饰模式

装饰模式:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
来源于大话设计模式

package decoration;

public interface Component {
	void show();
}

package decoration;
//concrete component
public class Person implements Component {
	private String name;
	public Person(String name) {
		this.name=name;
	}
	@Override
	public void show() {
		System.out.println("装扮的"+name);
	}

}
package decoration;
//decorator
public abstract class Finery implements Component {
	protected Component component;
	public Finery(Component component) {
		this.component=component;
	}
	@Override
	public void show() {
		if(component!=null) {
			component.show();
		}

	}

}
package decoration;

public class T_shirt extends Finery {
	
	public T_shirt(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void show() {
		super.show();
		System.out.println("T恤");
	}

}
package decoration;

public class XiZhuang extends Finery {

	public XiZhuang(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void show() {
		super.show();
		System.out.println("西装");
	}

}
package decoration;

public class BigTrouser extends Finery{
	public BigTrouser(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void show() {
		super.show();
		System.out.println("大垮裤");
	}

}

package decoration;

public class Sneakers extends Finery {

	public Sneakers(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void show() {
		super.show();
		System.out.println("球鞋");
	}

}
package decoration;

public class Client {

	public static void main(String[] args) {
		System.out.println("第一套穿搭");
		Component d1=new Person("小菜");
		Finery tx=new T_shirt(d1);
		Finery xz=new XiZhuang(tx);
		xz.show();
		System.out.println("第二套穿搭");
		Component a1=new Person("小白");
		Finery tx2=new T_shirt(a1);
		Finery kk=new BigTrouser(tx2);
		Finery qx=new Sneakers(kk);
		qx.show();
		
	}

}

在这里插入图片描述

发布了12 篇原创文章 · 获赞 0 · 访问量 370
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览