• 有趣的设计模式——烟火气息中的装饰模式



    版权声明

    • 本文原创作者:谷哥的小弟
    • 作者博客地址:http://blog.csdn.net/lfdfhl

    装饰模式概述

    装饰模式定义:动态地给一个对象添加额外的职责和功能。也就是说:我们可以在不使用继承、不改变原有结构的基础上扩展新的内容。举个例子,我们都担心摔碎手机屏幕,所以我们可以在手机的外表面裹一层材料(比如钢化膜,手机套)从而使得手机具有了抗摔的新功能;并且手机原本的功能(打电话,拍照,上网)并没有受到任何影响。嗯哼,看完这个小例子,我们来瞅瞅装饰模式中的四个角色:

    • Component:被装饰的原始抽象(类或接口)组件
    • ConcreteComponent:Component的具体实现类
    • Decorator:抽象装饰者
    • ConcreteDecorator:Decorator的具体实现类

    烟火气息中的美食

    抚琴路夜市有两样美食:炒饭和炒面。而且,可以在它们中加鸡蛋或者培根。接下来,我就借用这些人间烟火来学习装饰设计模式。

    主食Food

    要点概述:

    • 1、Food类有名字name属性
    • 2、Food类有价格price属性
    • 3、Food类作为其它主食类的父类
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class Food {
    	private String name;
    	private double price;
    
        public Food() {}
    
        public Food(String name,double price) {
        	this.name = name;
            this.price = price;
        }
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
        
        public void setPrice(double price) {
            this.price = price;
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    炒饭FriedRice

    要点概述:

    • 1、FriedRice类继承自Food类
    • 2、Food类是FriedRice的父类
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class FriedRice extends Food {
    
        public FriedRice() {
            super("炒饭",12);
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    炒面FriedNoodle

    要点概述:

    • 1、FriedNoodle类继承自Food类
    • 2、Food类是FriedNoodle的父类
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class FriedNoodle extends Food {
    
      public FriedNoodle() {
          super("炒面",13);
      }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    配菜Garnish

    要点概述:

    • 1、Garnish类是一个抽象类,作为父类使用
    • 2、Garnish类具有name属性
    • 3、Garnish类具有price属性
    • 4、Garnish类具有(持有)Food属性
    • 5、Garnish类具有抽象方法desc()
    • 6、Garnish类具有抽象方法cost()
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public abstract class Garnish {
    	private String name;
    	private double price;
    	private Food food;
    	
    	public Garnish() {}
    	
    	public Garnish(String name, double price, Food food) {
    		super();
    		this.name = name;
    		this.price = price;
    		this.food = food;
    	}
    	
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public double getPrice() {
    		return price;
    	}
    
    	public void setPrice(double price) {
    		this.price = price;
    	}
    
    	public Food getFood() {
    		return food;
    	}
    
    	public void setFood(Food food) {
    		this.food = food;
    	}
    	
    	// 描述信息
    	public abstract String desc(); 
    
        //计算价格
        public abstract double cost();  
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    鸡蛋Egg

    要点概述:

    • 1、Egg类是Garnish的子类
    • 2、Egg类具有introduce属性
    • 3、Egg类具有discount属性
    • 4、Egg类重写父类的desc()方法
    • 5、Egg类重写父类的cost()方法
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class Egg extends Garnish {
    	// 宣传语
    	private String introduce = "美味可口";
    	// 折扣价
    	private int discount = 1;
    
        public Egg(Food food) {
           super("鸡蛋",2,food);
        }
    
        @Override
        public String desc() {
        	String desc = super.getName() + super.getFood().getName() + introduce;
            return desc;
        }
        
        @Override
        public double cost() {
        	double total = super.getPrice() + super.getFood().getPrice()-discount;
            return total;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    培根Bacon

    要点概述:

    • 1、Bacon类是Garnish的子类
    • 2、Bacon类重写父类的desc()方法
    • 3、Bacon类重写父类的cost()方法
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class Bacon extends Garnish {
    
        public Bacon(Food food) {
            super("培根",3,food);
        }
        
        @Override
        public String desc() {
            return super.getName() + super.getFood().getName();
        }
    
        @Override
        public double cost() {
            return super.getPrice() + super.getFood().getPrice();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    测试Test

    要点概述:

    • 1、在Test创建炒饭并获取相关信息
    • 2、在Test创建蛋炒饭并获取相关信息
    • 3、在Test创建培根炒面并获取相关信息
    package com.decoratorPattern03;
    /**
     * 本文作者:谷哥的小弟 
     * 博客地址:http://blog.csdn.net/lfdfhl
     */
    public class Test {
    
    	public static void main(String[] args) {
    		// 炒饭
    		FriedRice friedRice = new FriedRice();
    		System.out.println(friedRice.getName() + " " + friedRice.getPrice() + "元");
    		System.out.println("---------------------");
    		
    		// 鸡蛋炒饭
    		Egg egg = new Egg(new FriedRice());
    		System.out.println(egg.desc() + " " + egg.cost() + "元");
    		System.out.println("---------------------");
    		
    		// 培根炒面
    		Bacon bacon = new Bacon(new FriedNoodle());
    		System.out.println(bacon.desc()+ " " + bacon.cost() + "元");
    		System.out.println("---------------------");
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    jetson xvaier的使用,结合yolo4的感知整合,自动驾驶硬件配置详细过程总结
    TensorRT简介
    导出pdf高清
    【网工日常】Web网管及注意事项
    windows安装ElasticSearch
    YAML 快速上手
    阿里云付哲:边缘云技术创新 让“云”无处不在
    Oracle 给字段加注释SQL
    果蝇(FOA)优化算法(附完整Matlab代码,可直接复制)
    Qt安装教程
  • 原文地址:https://blog.csdn.net/lfdfhl/article/details/125922757