装饰模式(Decorator)
来源:互联网 发布:哪个论坛源码好 编辑:程序博客网 时间:2024/05/17 21:44
- GitHub地址
- 装饰模式概述
- 题目
- UML类图
- 代码
- 运行结果
GitHub地址
https://github.com/YEN-GitHub/PatternDemo/tree/master/src/StructuralPattern/DecoratorPattern
装饰模式概述
Provide a surrogate or placeholder for another object to control access to it.
为其他对象提供一种代理以控制对这个对象的访问。
题目
请用装饰模式(Decorator)为某游戏角色设计换装功能,具体描述如下:
现阶段游戏包含人类(Human)和兽人(Orc)两种角色(Charactor),在将来可能会增加新角色。
现阶段服装(Finery)包含T恤(TShirt),牛仔裤(Jean),运动鞋(Sneaker),西装(Suit),领带(Tie),皮鞋(LeatherShoe)六种,在将来可能会增加新的服装,并且角色服装可以依据用户喜爱自由穿戴。
并搭配出:
绅士风的人类(西装,领带,皮鞋);
休闲风的兽人(T恤,牛仔裤,运动鞋);
混搭风的兽人(西装,领带,牛仔裤,运动鞋)。
题目中角色和服装只需要通过输出语句表示即可,比如:混搭风的兽人(西装,领带,牛仔裤,运动鞋),将输出“运动鞋 牛仔裤 领带 西装 装扮的兽人XXX(名字)”。
UML类图
代码
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 12:59 * Abstract:角色 */public class Charactor { public void show(){ }}
package StructuralPattern.DecoratorPattern.demo1;import StructuralPattern.DecoratorPattern.demo1.Charactor;/** * Created by yangenneng on 2017-05-03 12:59 * Abstract:人类 */public class Human extends Charactor { private String name; public Human(String name){ this.name=name; } public void show(){ System.out.println("--人类:"+name); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 12:59 * Abstract:兽人 */public class Orc extends Charactor { private String name; public Orc(String name){ this.name=name; } public void show(){ System.out.println("--兽人:"+name); }}
package StructuralPattern.DecoratorPattern.demo1;import StructuralPattern.DecoratorPattern.demo1.Charactor;/** * Created by yangenneng on 2017-05-03 13:00 * Abstract:服装 */public class Finery extends Charactor { protected Charactor charactor; public Finery(Charactor charactor){ this.charactor=charactor; } public void show(){ this.charactor.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:02 * Abstract:T恤 */public class TShirt extends Finery { public TShirt(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return charactor; } public void show(){ System.out.print("T恤 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:03 * Abstract:领带 */public class Tie extends Finery { public Tie(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return this.charactor; } public void show(){ System.out.print("领带 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:03 * Abstract:西装 */public class Suit extends Finery { public Suit(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return charactor; } public void show(){ System.out.print("西装 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:03 * Abstract:运动鞋 */public class Sneaker extends Finery { public Sneaker(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return charactor; } public void show(){ System.out.print("运动鞋 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:03 * Abstract:皮鞋 */public class LeatherShoe extends Finery { public LeatherShoe(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return charactor; } public void show(){ System.out.print("皮鞋 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:02 * Abstract:牛仔裤 */public class Jean extends Finery { public Jean(Charactor charactor) { super(charactor); } public Charactor decorator(Charactor charactor){ this.charactor=charactor; return charactor; } public void show(){ System.out.print("牛仔裤 "); super.show(); }}
package StructuralPattern.DecoratorPattern.demo1;/** * Created by yangenneng on 2017-05-03 13:02 * Abstract:客户端测试 */public class Client { public static void main(String[] args) { //人类 Human human=new Human("绅士风"); Orc orc1=new Orc("休闲风"); Orc orc2=new Orc("混搭风"); //装饰服饰 //绅士风的人类(西装,领带,皮鞋) Suit suit=new Suit(human);//西装 Tie tie=new Tie(suit);//领带 LeatherShoe leatherShoe=new LeatherShoe(tie);//皮鞋 leatherShoe.show(); //休闲风的兽人(T恤,牛仔裤,运动鞋) TShirt tShirt1=new TShirt(orc1); Jean jean1=new Jean(tShirt1); Sneaker sneaker1=new Sneaker(jean1); sneaker1.show(); //混搭风的兽人(西装,领带,牛仔裤,运动鞋) Suit suit2=new Suit(orc2); Tie tie2=new Tie(suit2); Sneaker sneaker2=new Sneaker(tie2); sneaker2.show(); }}
运行结果
1 0
- 装饰(Decorator)模式
- 装饰模式(Decorator)
- 装饰模式(Decorator)
- DECORATOR (装饰)模式
- Decorator(装饰)模式
- 装饰模式(Decorator)
- Decorator(装饰模式)
- 装饰模式(Decorator)
- 装饰模式(Decorator)
- 装饰模式(Decorator)
- Decorator(装饰)模式
- 装饰模式(Decorator)
- 装饰模式(Decorator)
- Decorator(装饰模式)
- Decorator(装饰)模式
- Decorator(装饰模式)
- 装饰模式(Decorator)
- 装饰模式(Decorator)
- HDOJ 2013 蟠桃记
- shell skills
- 2017 GDOI
- Oracle 查找、删除重复数据
- Android Studio多渠道批量打包及代码混淆
- 装饰模式(Decorator)
- TCP/IP学习笔记
- python 读取中文路径
- JBoss7.1的JNDI使用--EJB
- 周志华教授gcForest(多粒度级联森林)算法预测股指期货涨跌
- 拦截
- Tensorflow安装与测试
- php三元运算符
- springmvc常用注解标签详解