前言
一、装饰模式是什么?
- 何时使用
在不想增加很多子类的情况下扩展类时 - 方法
将具体功能职责划分,同时继承装饰者模式 - 优点
装饰类和被装饰类可以独立发展,而不会相互耦合。它有效地把类的核心职责和装饰功能分开了
装饰模式是继承关系的一个替代方案
装饰模式可以动态地扩展一个实现类的功能 - 缺点
多层装饰比较复杂。比如我们现在有很多层装饰,出了问题,一层一层检查,最后发现是最里层的装饰出问题了,想想工作量都害怕 - 使用场景
需要扩展一个类的功能时
需要动态地给一个对象增加功能,并可以动态地撤销时
需要为一批的兄弟类进行改装或加装功能时 - 应用实例
旧机包装成新机,手机/电脑内部配件不变,只是换个外壳
换衣小游戏,人还是那个人,不断给她换衣服,还可以一层套一层的
孙悟空有72变,变成什么后就有了它的功能,但本质还是一只猴子
二、使用步骤
1.小菜撩妹
解释:
此题中Finnery作为装饰类
代码如下(示例):
class Person {
private String name;
public Person() {};
public Person(String name)
{
this.name=name;
}
public void show() {
System.out.println(name+"的装扮:");
}
}
class Finery extends Person{
protected Person component;
public void Decorate(Person component) {
this.component=component;
}
public void show() {
if(component!=null)
{
component.show();
}
}
}
class Tshirts extends Finery{
public void show() {
super.show();
System.out.print("体恤");
}
}
class Pqx extends Finery{
public void show() {
super.show();
System.out.print("皮鞋 ");
}
}
class Tires extends Finery{
public void show() {
super.show();
System.out.print("领带 ");
}
}
public class Zz {
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
Person xc=new Person("小菜");
Tshirts a=new Tshirts();
Tires b=new Tires();
Pqx c=new Pqx();
a.Decorate(xc);
b.Decorate(a);
c.Decorate(b);
c.show();
}
}
2.复杂的奖金计算
说明:
Component类作为一个抽象类,而Person是继承该抽象类的子类,因此在调用Component的函数pay()就是在调用Person类的pay()
代码如下(示例):
abstract class Component {
abstract double pay();
}
class Person extends Component {
private String name;
private String position;
public Person() {}
public Person(String name, String position) {
this.name = name;
this.position = position;
}
public double pay() {
System.out.print(position+name+"累计奖金为");
return 0;
}
}
class Wage extends Component {
protected Component component;
public void Decorator(Component component)
{this.component=component;
}
public double pay()
{if(component!=null)
return component.pay();
else
return 0;
}
}
class Month extends Wage {
private double money;
public Month() {
}
public Month(double money) {
this.money = money;
}
public double pay()//月奖金
{System.out.print("月奖金 "+money+"元 ");
return super.pay()+money;
}
}
class Increase extends Wage{
private double yewue;
public Increase() {
}
public Increase(double yewue) {
this.yewue = yewue;
}
public double pay()
{System.out.print("个人业务增长奖金 "+yewue*0.2+"元 ");
return super.pay()+yewue*0.2;
}
}
class Team extends Wage {
private double money;
public Team() {
}
public Team(double money) {
this.money = money;
}
public double pay()
{System.out.print("团队增长奖金"+money+"元 ");
return super.pay()+money;
}
}
class TeamIncrease extends Wage {
private double zengzhang;
public TeamIncrease() {
}
public TeamIncrease(double zengzhang) {
this.zengzhang = zengzhang;
}
public double pay()
{System.out.print("团队增长奖金"+zengzhang*0.5+"元 ");
return super.pay()+zengzhang*0.5;
}
}
public class Zz {
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
//https://blog.csdn.net/qq_39467629/article/details/83621286
Person xc=new Person("老马","28岁的程序员");
Person xd=new Person("祝大爷","快退休的团队经理");
Month mo=new Month(4000);
Increase in=new Increase(5800);
mo.Decorator(xc);
in.Decorator(mo);
Team tm=new Team(5000);
TeamIncrease ti=new TeamIncrease(2500);
tm.Decorator(xd);
ti.Decorator(tm);
System.out.println(in.pay()+"元");
System.out.println(ti.pay()+"元");
}
}
总结
- 装饰模式有点想递归的过程
- 装饰类模板
class Zs{
protect Component component;
public void Decorator(Component component)
{
this.component=component;
}
public void show(){
if(component!=null)
{
System.out.print(component.pay());//
}