• 设计模式学习笔记--装饰模式


     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 22:56:57 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Beverage说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public abstract class Beverage
    12     {
    13         protected string description = "Unknown Beverage";
    14 
    15         public virtual string GetDescription()
    16         {
    17             return description;
    18         }
    19 
    20         public abstract double Cost();
    21     }
    22 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 23:02:25 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Espresso说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Espresso:Beverage
    12     {
    13         public Espresso()
    14         {
    15             description = "Espresso";
    16         }
    17 
    18         public override double Cost()
    19         {
    20             return 1.99;
    21         }
    22     }
    23 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 23:00:21 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// CondimentDecorator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public abstract class CondimentDecorator:Beverage
    12     {
    13         private Beverage beverage;
    14 
    15         public CondimentDecorator(Beverage beverage)
    16         {
    17             this.beverage = beverage;
    18         }
    19 
    20         public override string GetDescription()
    21         {
    22             return beverage.GetDescription();
    23         }
    24 
    25         public override double Cost()
    26         {
    27             return beverage.Cost();
    28         }
    29     }
    30 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 23:07:18 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Mocha说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Mocha : CondimentDecorator
    12     {
    13         public Mocha(Beverage beverage)
    14             : base(beverage)
    15         { }
    16 
    17         public override string GetDescription()
    18         {
    19             return base.GetDescription() + "+ Mocha";
    20         }
    21 
    22         public override double Cost()
    23         {
    24             return base.Cost() + 0.20;
    25         }
    26     }
    27 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 23:10:42 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Soy说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Soy : CondimentDecorator
    12     {
    13         public Soy(Beverage beverage)
    14             : base(beverage)
    15         { }
    16 
    17         public override string GetDescription()
    18         {
    19             return base.GetDescription() + "+ Soy";
    20         }
    21 
    22         public override double Cost()
    23         {
    24             return base.Cost() + 0.30;
    25         }
    26     }
    27 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/21 23:13:05 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Whip说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Whip : CondimentDecorator
    12     {
    13         public Whip(Beverage beverage)
    14             : base(beverage)
    15         { }
    16 
    17         public override string GetDescription()
    18         {
    19             return base.GetDescription() + "+ Whip";
    20         }
    21 
    22         public override double Cost()
    23         {        
    24             return base.Cost() + 0.60;
    25         }
    26     }
    27 }
    View Code
     1 using System;
     2 
     3 namespace Decorator
     4 {
     5     class Program
     6     {
     7         static void Main(string[] args)
     8         {
     9             Espresso beverage1 = new Espresso();
    10             Mocha mocha = new Mocha(beverage1);
    11             Soy soy = new Soy(mocha);
    12             Whip whip = new Whip(soy);
    13 
    14             Console.WriteLine(whip.GetDescription()+"  "+whip.Cost());
    15         }
    16     }
    17 }
    View Code
  • 相关阅读:
    day77 vue对象提供的属性功能
    day76 作业
    day76 vue框架入门
    day75 bbs项目☞后台管理+修改头像
    day74 bbs项目☞点赞与评论
    day73 bbs项目☞基本功能实现
    day72 bbs项目☞登录注册
    练习题00
    雇1个人工作7天,你有1根金条可以分成7份,只能切2刀,如何保证每天都得到1份金条
    Python正课143 —— DRF 进阶4 权限、频率、过滤、排序
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5516173.html
Copyright © 2020-2023  润新知