栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

设计模式之装饰模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式之装饰模式

//定义人(物件)
public class Person {
	private String name;
	public Person(){
		
	}
	public Person(String name){
		this.name = name;
	}
	
	public void Show() {
		System.out.println("装饰" + this.name);
	}
}
//装饰抽象类
//需继承对象,从外类拓展对象类功能
public class Finery extends Person {
	protected Person component;
	//打扮
	public void Decorate(Person component){
		this.component = component;
	}
	
	@Override
	public void Show(){
		if(component!=null){
			component.Show();
		}
	} 
}
//给对象添加职责功能
public class Suit extends Finery {

	@Override
	public void Show() {
		// TODO Auto-generated method stub
		System.out.println("西装");
		super.Show();
		//System.out.println("西装");
	}

}
。。。。。。
//主类,先定义对象,然后定义各种装饰类,装饰类中依次加入对象,最后展示类。
public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person person =new Person("Tom");
		System.out.println("第一种装饰");
		Sneakers sneakers = new Sneakers();
		TBigTrouser tBigTrouser =new TBigTrouser();
		TShirts tShirts = new TShirts();
		
		sneakers.Decorate(person);
		tBigTrouser.Decorate(sneakers);
		tShirts.Decorate(tBigTrouser);
		tShirts.Show();
		
		System.out.println("第二种装饰");
		LeatherShoes leatherShoes = new LeatherShoes();
		Tie tie = new Tie();
		Suit suit = new Suit();
		
		leatherShoes.Decorate(person);
		tie.Decorate(leatherShoes);
		suit.Decorate(tie);
		suit.Show();
	}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号