6.装饰模式
来源:互联网 发布:three.js 全景图切换 编辑:程序博客网 时间:2024/06/09 14:42
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();}}
阅读全文
0 0
- 6.装饰模式
- 设计模式--6.装饰模式
- 装饰模式-撤销装饰
- 大话设计模式--6.装饰模式
- 设计模式:6. 装饰器模式(Decorator)
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 5.依赖倒转原则
- 快速排序的三种实现方式以及非递归版本
- 个人学习总结的部分java小知识与面试题(三)
- Develop Universal Robot (UR) in ROS: From Install to Demo
- python virtualenv activate文件分析
- 6.装饰模式
- Java中递归实现复制文件夹和文件
- PHP解决网页乱码问题
- Java注解自定义注解
- 马农???(穷举+哈希)
- Android应用开发的基本流程
- log4j学习demo
- 安装django + Apach + mod_wsgi
- Oracle 10g RAC 安装之配置时间同步