Java教程

关于装饰模式的一次实践

本文主要是介绍关于装饰模式的一次实践,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

我在这里展示的装饰模式只是简单的构造了一些函数,并没有真正的开发出实体软件,所以代码及其简单,就不进行注释。这里仅供参考,转载请@原作者

题目

 某咖啡店的店员在卖咖啡时,可以根据客户需求添加各种配料,并根据配料计算总费用。

咖啡

价格

配料

价格

浓缩咖啡(espresso)

25

摩卡

10

混合咖啡(House Blend)

30

奶泡

8

重烘培咖啡(Dark Roast)

20

牛奶

6

输出结果如下

饮料类代码如下

abstract class Beverage

    {

        public abstract string GetDescription();

        public abstract int GetCost();

    }

首先我们先用WPS的流程图或者Visio等软件画出此题程序的UML图

然后开始写以代码

抽象构件

//Beverage.cs
namespace Decorator
{
    abstract class Beverage
    {
        public abstract string GetDescription();
        public abstract int GetCost();
    }
}

具体构件

//Espresso.cs
namespace Decorator
{
    class Espresso : Beverage
    {
        string name = "浓缩咖啡";
        int price = 25;
        public override int GetCost()
        {
            return price;
        }

        public override string GetDescription()
        {
            return name;
        }
    }
}

//House_Blend.cs
namespace Decorator
{
    class House_Blend : Beverage
    {
        int price = 30;
        string name = "混合咖啡";
        public override int GetCost()
        {
            return price;
        }

        public override string GetDescription()
        {
            return name;
        }
    }
}

//Dark_Roast.cs
namespace Decorator
{
    class Dark_Roast : Beverage
    {
        string name = "重烘培咖啡";
        int price = 20;
        public override int GetCost()
        {
            return price;
        }

        public override string GetDescription()
        {
            return name;
        }
    }
}

抽象装饰类

//Batching.cs
namespace Decorator
{
    abstract class Batching:Beverage
    {
        private Beverage batching;
        private int price;
        private int  a= 0;
        private string name;
        private string b = "";
        public Batching(Beverage batching)
        {
            this.batching = batching;
        }
        public override int GetCost()
        {
            this.price = batching.GetCost();
            this.price += a;
            return this.price;
        }
        public override string GetDescription()
        {
            this.name = batching.GetDescription();
            this.name += b;
            return this.name;
        }
    }
}

具体装饰类

//Mocha.cs
namespace Decorator
{
    class Mocha : Batching
    {
        public Mocha(Beverage batching)
            :base(batching)
        { }
        int price = 10;
        string name = "摩卡";
        public override int GetCost()
        {
            this.price += base.GetCost();
            return this.price;
        }
        public override string GetDescription()
        {
            this.name = base.GetDescription() + ',' + this.name;
            return this.name;
        }
    }
}

//Whip.cs
namespace Decorator
{
    class Whip:Batching
    {
        public Whip(Beverage batching)
            : base(batching)
        { }
        int price = 8;
        string name = "奶泡";
        public override int GetCost()
        {
            this.price += base.GetCost();
            return this.price;
        }
        public override string GetDescription()
        {
            this.name = base.GetDescription() + ',' + this.name;
            return this.name;
        }
    }
}

//Milk.cs
namespace Decorator
{
    class Milk:Batching
    {
        public Milk(Beverage batching)
            : base(batching)
        { }
        int price = 6;
        string name = "牛奶";
        public override int GetCost()
        {
            this.price += base.GetCost();
            return this.price;
        }
        public override string GetDescription()
        {
            this.name = base.GetDescription() + ',' + this.name;
            return this.name;
        }
    }
}

Program

using System;

namespace Decorator
{
    class Program
    {
        public static int price;
        public static string name;
        static void Main(string[] args)
        {
            
            Beverage batching1, batching2, batching3;
            batching1 = new Espresso();
            batching2 = new Mocha(batching1);
            batching3 = new Milk(batching2);
            price = batching3.GetCost();
            name = batching3.GetDescription();
            Console.WriteLine(name + "  ¥" + price);
            Console.Read();
        }
    }
}

运行结果与题目中要求的结果一摸一样就不在展示

这篇关于关于装饰模式的一次实践的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!