package decoratorpattern;/** * 实现一个穿衣服的类 * * @author LiMing E-mail:1151143484@qq.com * @date 2017年6月11日 下午12:21:07 */public class Commonly {public static void main(String[] args) {person person = new person("小明");tuoxie tuoxie = new tuoxie();duanxiu duanxiu = new duanxiu();System.out.println("装扮如下:");tuoxie.Show();duanxiu.Show();person.Show();}}// 人class person {String name;public person() {}public person(String name) {this.name = name;}public void Show() {System.out.print("装扮的" + name);}}// 服饰抽象类abstract class Finey {public abstract void Show();}// 下边都是需要穿的服饰class tuoxie extends Finey {public void Show() {System.out.print("拖鞋  ");}}class pixie extends Finey {public void Show() {System.out.print("皮鞋  ");}}class duanxiu extends Finey {public void Show() {System.out.print("短袖  ");}}class xifu extends Finey {public void Show() {System.out.print("西服  ");}}

 

 

package decoratorpattern;/** * 用装饰模式来实现穿衣服 * 装饰模式:动态的给一个对象添加一些额外职责, * 就增加功能来说,装饰模式比生成子类更为灵活 *  * 把类中装饰功能搬出去 简化的原有的类 * 有效的把类的核心职责和装饰功能区分开了 而且可以去除相关类中重复的装饰逻辑 *  * 日常使用的时候装饰模式的顺序很重要     *  *  * @author LiMing E-mail:1151143484@qq.com * @date 2017年6月11日 下午12:30:35 */public class Decoratorpattern {public static void main(String[] args) {persons persons = new persons("小小明");tuoxies tuoxies = new tuoxies();duanxius duanxius = new duanxius();tuoxies.Decorate(persons);duanxius.Decorate(tuoxies);System.out.println("装饰如下:");duanxius.Show();}}// 人class persons {String name;public persons() {}public persons(String name) {this.name = name;}public void Show() {System.out.print("装扮的" + name);}}// 服饰类class fineys extends persons {protected persons component;public void Decorate(persons component) {this.component = component;}// 覆盖父类方法public void Show() {if (component != null) {component.Show();}}}class tuoxies extends fineys {public void Show() {System.out.print("拖鞋");super.Show();}}class pixies extends fineys {public void Show() {System.out.print("皮鞋");super.Show();}}class xifus extends fineys {public void Show() {System.out.print("西服");super.Show();}}class duanxius extends fineys {public void Show() {System.out.print("短袖");super.Show();}}

 

 

©著作权归作者所有:来自51CTO博客作者木子的昼夜的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 14.观察者模式
  2. 15.抽象工厂模式
  3. 7.代理模式
  4. 16.状态模式
  5. 18.备忘录模式
  6. 17.适配器模式
  7. 专用服务器模式(MTS)和共享服务器模式
  8. 【shell】shell脚本实战-awk工作模式讲解
  9. Linux下绑定网卡的操作记录

随机推荐

  1. PHP 服务器端处理跨域问题
  2. OWASP 维护的 PHP 安全配置速查表
  3. Javascript 到 PHP 加密通讯的简单实现
  4. PHP+Ajax如何实现上传文件进度条动态显示
  5. php统计文件中的代码行数
  6. 如何用PHP迭代器来实现一个斐波纳契数列
  7. PHP中常用的加密解密方法总结
  8. php获取当前执行的php文件的文件名
  9. 如何解决php Function split() is deprec
  10. php调取摄像头实现拍照功能的方法