zoukankan      html  css  js  c++  java
  • Head First 设计模式----DecoratorPattern

    装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public abstract class Beverage
        {
            public String description = "Unknow beverage";
    
            public virtual String getDescription()
            {
                return description;
            }
    
            public abstract Double cost();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public abstract class CondimentDecorator:Beverage
        {
            public abstract String getDescription();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class DarkRoast:Beverage
        {
            public DarkRoast() {
                description = "DarkRoast Coffe";
            }
           
            public override Double cost()
            {
                return 0.99;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public abstract class Decaf:Beverage
        {
    
            public Decaf() {
                description = "Decaf Coffee";
            }
    
            public override Double cost()
            {
                return 1.05;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class Espresso:Beverage
        {
            public Espresso() {
                description = "Espresso Coffee";
            }
    
            public override Double cost()
            {
                return 1.99;        
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class HouseBlend:Beverage
        {
            public HouseBlend() {
                description = "House Blend Coffee";
            }
    
            public override Double cost()
            {
                return 0.89;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class Milk :Beverage
        {
            Beverage beverage;
    
            public Milk(Beverage beverage)
            {
                this.beverage = beverage;
            }
    
            public override String getDescription() {
                return beverage.getDescription() + ",Milk";
            }
    
            public override Double cost()
            {
                return 0.10 + beverage.cost();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class Mocha : Beverage
        {
            Beverage beverage;
    
            public Mocha(Beverage beverage) {
                this.beverage = beverage;
            }
    
            public override String getDescription()
            {
                return beverage.getDescription() + ",Mocha";
            }
    
            public override Double cost() {
                return 0.20 + beverage.cost();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class Soy : Beverage
        {
            Beverage beverage;
    
            public Soy(Beverage beverage)
            {
                this.beverage = beverage;
            }
    
            public override String getDescription()
            {
                return beverage.getDescription() + ",Soy";
            }
    
            public override Double cost()
            {
                return 0.15+beverage.cost();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeadFirst.Decorator
    {
        public class Whip : Beverage
        {
            Beverage beverage;
    
            public Whip(Beverage beverage){
                this.beverage = beverage;
            }
    
            public override String getDescription()
            {
                return this.beverage.getDescription() + ",Whip";
            }
    
            public override Double cost()
            {
                return 0.10 + beverage.cost();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using HeadFirst.Decorator;
    
    
    namespace HeadFirst
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Decorator装饰者模式
                Beverage beverage = new Espresso();
                Console.Write(beverage.getDescription() + ":" + beverage.cost());
                Console.ReadLine(); 
            }
        }
    }
  • 相关阅读:
    [CodeForces
    [CodeChef]RIN(最小割)
    [Bzoj3894]文理分科(最小割)
    [Poj3469]Dual Core CPU(最小割)
    MySQL- 锁(3)
    MySQL- 锁(1)
    MySQL- 锁(2)
    MySQL-中文全文检索
    Solr
    多线程编程-之并发编程:同步容器
  • 原文地址:https://www.cnblogs.com/SmileX/p/4189234.html
Copyright © 2011-2022 走看看