设计模式--装饰模式
来源:互联网 发布:淘宝收入排行榜 编辑:程序博客网 时间:2024/06/06 03:27
复习设计模式--装饰模式
装饰模式 在不修改已经存在的类的情况下,动态的添加新的功能,实现即插即用,开放关闭原则
装饰模式 在不修改已经存在的类的情况下,动态的添加新的功能,实现即插即用,开放关闭原则
public interface Man {public void killMonster();}public class BatMan implements Man {public String name;public BatMan(String name) {this.name = name;}@Overridepublic void killMonster() {System.out.println("蝙蝠侠" + name + "开始打怪兽!");}}public class XMan implements Man {@Overridepublic void killMonster() {System.out.println("X-Man 开始打怪兽!");}}//Decorator 添加武器 进行油漆装饰public abstract class Weapon implements Man {protected Man man;public void setMan(Man man) {this.man = man;}public Man getMan() {return man;}@Overridepublic void killMonster() {man.killMonster();}}public class Sword extends Weapon {private String swordName;public Sword(String swordName) {this.swordName = swordName;}@Overridepublic void killMonster() {System.out.println("获取一把剑,剑名:" + swordName);super.killMonster();}}public class Gun extends Weapon {public String gunName;private int bulletCount = 10;public Gun(String gunName) {this.gunName = gunName;}@Overridepublic void killMonster() {System.out.println("发放武器 " + gunName + ",装有子弹" + bulletCount + "发!");super.killMonster();}}public class GiveBullet extends Weapon {private int bulletCount;public GiveBullet(int bulletCount) {this.bulletCount = bulletCount;}@Overridepublic void killMonster() {System.out.println("额外发放子弹" + bulletCount + "发!");super.killMonster();}}public class DecoratorTest {public static void main(String[] args) {Man man = new BatMan("Wayne");//Man man = new XMan();//man.killMonster();Weapon weapon = new Gun("AK");//weapon.setMan(man);//weapon.killMonster();Weapon bullet = new GiveBullet(100);Weapon sword = new Sword("莫邪");//添加子弹bullet.setMan(man);//添加gunweapon.setMan(bullet);//添加剑sword.setMan(weapon);sword.killMonster();}}
0 0
- 设计模式---装饰模式
- 设计模式:装饰模式
- 设计模式------装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式 装饰模式
- 设计模式----装饰模式
- 设计模式-【装饰模式】
- 设计模式--装饰模式
- 设计模式装饰模式
- 设计模式 - 装饰模式
- 设计模式-----装饰模式.
- 设计模式-----装饰模式.
- [ 设计模式 ] 装饰模式!
- 设计模式:装饰模式
- 设计模式 - 装饰模式
- 设计模式 - 装饰模式
- Machine Learning(Andrew)Week7(下)
- 一道小的算法题
- 从输入 URL 到页面加载完的过程中都发生了什么事情 —— 网络优化篇
- 一个完整的类似ctrl+f的功能
- .NET断想
- 设计模式--装饰模式
- undefined reference to "...."
- beforeunload处理用户最后事件,提交数据,用户退出
- android4.4 事件传递机制
- 用ASP编程控制在IIS建立Web站点
- Linux下设置TOMCAT环境变量的三种方法
- 新西兰 working holiday visa
- 书画小说之古典艳情系列
- JavaScript 脚本文档之二(学习笔记)