23种设计模式——装饰模式
目录
引入——小菜穿衣
装饰模式
模式组成及UML图
理解
装饰模式优缺点
装饰模式VS策略模式
装饰模式与AOP
实例
小菜穿衣的第二种写法
复杂的奖金发放
引入——小菜穿衣

public abstract class Component {public abstract void show();
}
public class Person extends Component{private String name;public Person() {super();// TODO Auto-generated constructor stub}public Person(String name) {super();this.name = name;}@Overridepublic void show() {System.out.println("装扮的"+name);}}
public class Finery extends Component{protected Component component;public void decorate(Component component) {this.component=component;}public void show() {if(component!=null)component.show();}}
public class BigTrouser extends Finery{public void show() {System.out.println("垮裤");super.show();}}public class LeatherShoes extends Finery{public void show() {System.out.println("皮鞋");super.show();}
}public class Sneakers extends Finery{public void show() {System.out.println("球鞋");super.show();}
}public class Suit extends Finery{public void show() {System.out.println("西装");super.show();}
}public class Tie extends Finery{public void show() {System.out.println("领带");super.show();}
}public class TShirts extends Finery{public void show() {System.out.println("T恤");super.show();}}
public class Test {public static void main(String[] args) {Person p1=new Person("小菜");System.out.println("第一次装扮:");Sneakers s=new Sneakers();BigTrouser bt=new BigTrouser();TShirts ts=new TShirts();s.decorate(p1);bt.decorate(s);ts.decorate(bt);ts.show();Person p2=new Person("小李");System.out.println("第二次装扮:");LeatherShoes ls=new LeatherShoes();Tie id=new Tie();Suit xz=new Suit();ls.decorate(p2);id.decorate(ls);xz.decorate(id);xz.show();}}
装饰模式
本质:动态组合,动态是手段,组合是目的
又称包裹模式,每一个具体装饰类都将下一个具体装饰类或者具体构件类包裹起来。
目的:动态的给一个对象添加一些额外的职责。就增加功能来说,装饰模式比继承更灵活。
功能:能够实现动态地为对象添加功能。从一个对象外部来给对象增加功能,相当于是改变了对象的外观。
被装饰的对象不一定是最原始的那个对象,也可能是被其他装饰器装饰过的对象,反正都是实现的同一接口。
模式组成及UML图

抽象构件(Component)角色:
抽象类,制定规范(方法)。
具体构件(Concrete Component)角色:
被装饰的对象。
装饰(Decorator)角色:
持有一个Component的的实例。
具体装饰(Concrete Decorator)角色:
给Concrete Component加上额外的责任。
理解
装饰模式主要用到了:继承和多态
所有组件都是Component 的子类,由Component 规划行为(如,public void show();),装饰类Decorator 与被装饰类Person 都是Component的子类,具有相同的行为。
装饰类Decorator 持有一个Component的的实例,可以操作Component 的所有子类。Concrete Decorator重写了Component 定义的方法,可以重写成自己想要的装饰。
装饰器和组件类的关系:
装饰器是用来装饰组件的,装饰器一定要实现和组件类一致的接口,保证他们是同一个类型,并具有同一个外观,这样组合完成的装饰才能够递归调用下去
装饰模式优缺点
优点:
都可以扩展对象的功能,但装饰模式更灵活。
通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。
缺点:
会产生很多细粒度对象。
装饰模式VS策略模式
- 策略模式改变的是对象的内核
- 装饰模式改变的是对象的外壳
- 策略模式是一层调用
- 装饰模式是递归调用
- 可以有机结合
装饰模式与AOP
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向方面编程。
面向对象开发中,考虑系统的角度通常是纵向的。
AOP主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
目的:将非业务逻辑代码与业务逻辑代码划分开,改变前者时,不影响业务逻辑的代码。
实例
小菜穿衣的第二种写法
将component和person类结合

public class Person {private String name;public Person() {}public Person(String name) {super();this.name = name;}public void show() {System.out.println(name);}}
public class Finery extends Person{protected Person component;public void decorate(Person component) {this.component=component;}public void show() {if(component!=null)component.show();}}
复杂的奖金发放
奖金分类,对于个人有当月奖金、个人累计奖金、个人业务增长奖金等;对于业务经理,除了个人奖金外,还有团队累积奖金、团队盈利奖金等。
计算公式不同,奖金基数也不同,奖金的计算方式会经常变化,要适于调整和修改。

public abstract class Component {public abstract void show();}public class Person extends Component{private String name;public Person() {}public Person(String name) {this.name = name;}@Overridepublic void show() {System.out.println(name+"是");}}public class Salary extends Component{protected Component component;public Salary(Component component) {super();this.component = component;}public Salary() {}@Overridepublic void show() {if(component!=null)component.show();}}
public class Worker extends Person{public Worker() {super();// TODO Auto-generated constructor stub}public Worker(String name) {super(name);// TODO Auto-generated constructor stub}public void show() {super.show();System.out.println("打工人");}}public class Manager extends Person{public Manager() {super();// TODO Auto-generated constructor stub}public Manager(String name) {super(name);// TODO Auto-generated constructor stub}public void show() {super.show();System.out.println("经理"); }}
public class MonthSalary extends Salary{public MonthSalary() {super();// TODO Auto-generated constructor stub}public MonthSalary(Component component) {super(component);// TODO Auto-generated constructor stub}public void show() {super.show();System.out.println("个人月奖金");}}public class PronfitSalary extends Salary{public PronfitSalary() {super();// TODO Auto-generated constructor stub}public PronfitSalary(Component component) {super(component);// TODO Auto-generated constructor stub}public void show() {super.show();System.out.println("团队盈利奖金");}}public class TotalSalary extends Salary{public TotalSalary() {super();// TODO Auto-generated constructor stub}public TotalSalary(Component component) {super(component);// TODO Auto-generated constructor stub}public void show() {super.show();System.out.println("个人累积奖金");}}
public class Test {public static void main(String[] args) {Component manager=new Manager("老张");Component worker=new Worker("小李");Salary s1=new MonthSalary(new PronfitSalary(manager));s1.show();Salary s2=new MonthSalary(new TotalSalary(worker));s2.show();}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
