【设计模式】——如何做到一步步装饰一个类——装饰模式的实现


前言

一、装饰模式是什么?

  1. 何时使用
    在不想增加很多子类的情况下扩展类时
  2. 方法
    将具体功能职责划分,同时继承装饰者模式
  3. 优点
    装饰类和被装饰类可以独立发展,而不会相互耦合。它有效地把类的核心职责和装饰功能分开了
    装饰模式是继承关系的一个替代方案
    装饰模式可以动态地扩展一个实现类的功能
  4. 缺点
    多层装饰比较复杂。比如我们现在有很多层装饰,出了问题,一层一层检查,最后发现是最里层的装饰出问题了,想想工作量都害怕
  5. 使用场景
    需要扩展一个类的功能时
    需要动态地给一个对象增加功能,并可以动态地撤销时
    需要为一批的兄弟类进行改装或加装功能时
  6. 应用实例
    旧机包装成新机,手机/电脑内部配件不变,只是换个外壳
    换衣小游戏,人还是那个人,不断给她换衣服,还可以一层套一层的
    孙悟空有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()+"元");

	}
}



总结

  1. 装饰模式有点想递归的过程
  2. 装饰类模板
class Zs{
protect Component component;
public void Decorator(Component component)
{
  this.component=component;
}
public void show(){
if(component!=null)
{
 System.out.print(component.pay());//
 }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉梦昂志️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值