• 装饰模式


    1.Decorrator中保存有一个Component,然后ConcoreteComponent中就可以给ConcoreteComponent添加一些职责

    image

    2.场景:我们现在用的手机功能很多,我就用Decorator模式实现一下对某个手机的GSP和蓝牙功能扩展

    3.代码:

    手机:

    public abstract class AbstractCellPhone
    
        {
    
            public abstract string CallNumber();
    
            public abstract string SendMessage();
    
    }

    Nokia和Moto的手机:

    public class NokiaPhone : AbstractCellPhone
    
        {
    
            public override string CallNumber()
    
            {
    
                return "NokiaPhone call sombody";
    
            }
    
     
    
            public override string SendMessage()
    
            {
    
                return "NokiaPhone send a message to somebody";
    
            }
    
        }
    
     
    
        public class MotoPhone : AbstractCellPhone
    
        {
    
            public override string CallNumber()
    
            {
    
                return "MotoPhone call sombody";
    
            }
    
     
    
            public override string SendMessage()
    
            {
    
                return "MotoPhone send a message to somebody";
    
            }
    
     }

    装饰类:

    public abstract class Decorator:AbstractCellPhone
    
        {
    
            AbstractCellPhone _phone;
    
     
    
            public Decorator(AbstractCellPhone phone)
    
            {
    
                _phone = phone;
    
            }
    
     
    
            public override string CallNumber()
    
            {
    
                return _phone.CallNumber();
    
            }
    
     
    
            public override string SendMessage()
    
            {
    
                return _phone.SendMessage();
    
            }
    
      }

    GSP和蓝牙的职责扩展

    public class DecoratorGPS : Decorator
    
        {
    
            public DecoratorGPS(AbstractCellPhone phone)
    
                : base(phone)
    
            { }
    
     
    
            public override string CallNumber()
    
            {
    
                return base.CallNumber() + " with GPS";
    
            }
    
     
    
            public override string SendMessage()
    
            {
    
                return base.SendMessage() + " with GPS";
    
            }
    
        }
    
     
    
        public class DecoratorBlueTooth : Decorator
    
        {
    
            public DecoratorBlueTooth(AbstractCellPhone phone)
    
                : base(phone)
    
            { }
    
     
    
            public override string CallNumber()
    
            {
    
                return base.CallNumber() + " with BlueTooth";
    
            }
    
     
    
            public override string SendMessage()
    
            {
    
                return base.SendMessage() + " with BlueTooth";
    
            }
    
     }

    客户端调用:

    static void Main(string[] args)
    
            {
    
                 AbstractCellPhone phone = new NokiaPhone();
    
                Console.WriteLine(phone.CallNumber());
    
                Console.WriteLine(phone.SendMessage());
    
                DecoratorGPS gps = new DecoratorGPS(phone);     //add GSP
    
                Console.WriteLine(gps.CallNumber());
    
                Console.WriteLine(gps.SendMessage());
    
                DecoratorBlueTooth bluetooth = new DecoratorBlueTooth(gps); //add GSP and bluetooth
    
                Console.WriteLine(bluetooth.CallNumber());
    
                Console.WriteLine(bluetooth.SendMessage());
    
                Console.Read();
    
         }

    参考文章:http://www.cnblogs.com/kid-li/archive/2006/06/26/435966.html

  • 相关阅读:
    Windows 科研软件推荐
    有关Python 包 (package) 的基本知识
    《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记
    Coursera助学金申请模板
    《Using Databases with Python》 Week2 Basic Structured Query Language 课堂笔记
    Jupyter 解决单个变量输出问题
    解决 pandas 中打印 DataFrame 行列显示不全的问题
    《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
    缓存击穿及解决方案
    jvm垃圾收集器
  • 原文地址:https://www.cnblogs.com/ccjcjc/p/3423389.html
Copyright © 2020-2023  润新知