首页 > 其他 > 详细

装饰模式

时间:2015-12-30 21:47:47      阅读:219      评论:0      收藏:0      [点我收藏+]

技术分享

 public class ApplePhone:MobilePhone
    {
        public ApplePhone()
        { }

        public override void SendMessage()
        {
            Console.WriteLine("ApplePhone 现在已经具备了发送短信的功能");
        }

        public override void Call()
        {
            Console.WriteLine("ApplePhone 现在已经具备了普通通信的功能");
        }

    }

 

 

public class Bluetooth:Function
    {
        public Bluetooth(MobilePhone mobilephone)
            : base(mobilephone)
        {
            Console.WriteLine("请您稍等,系统现在正在安装蓝牙功能");
            Console.WriteLine("蓝牙功能添加成功");
        }

        public void Connect()
        {
            Console.WriteLine("蓝牙正在连接");
        }
    }

 

 public class Camera:Function
    {
        public Camera(MobilePhone mobilephone)
            : base(mobilephone)
        {
            Console.WriteLine("请您稍等,系统现在正在安装摄像头功能");
            Console.WriteLine("摄像头功能添加成功");
        }

        public override void Take()
        {
            Console.WriteLine("通信功能升级为带有视频");
        }
    }

 

public class Function:MobilePhone
    {
        private MobilePhone _mobilephone;

        public Function(MobilePhone mobilephone)
        {
            _mobilephone = mobilephone;
        }

        public override void SendMessage()
        {
            _mobilephone.SendMessage();
        }

        public override void Call()
        {
            _mobilephone.Call();
        }

    }

 

public class GPS:Function
    {
        public string Localtion { get; set; }
       
        public GPS(MobilePhone mobilephone)
            : base(mobilephone)
        {
           
        }
    }

 

public class MiPhone:MobilePhone
    {
        public MiPhone()
        { }

        public override void SendMessage()
        {
            Console.WriteLine("MiPhone 现在已经具备了发送短信的功能");
        }

        public override void Call()
        {
            Console.WriteLine("MiPhone 现在已经具备了普通通信的功能");
        }
     }

 

public abstract class MobilePhone
     {
         public MobilePhone()
         { }

         //private string _Location;                     //存储将来需要拓展的定位数据

         public abstract void SendMessage();  //发送短信
         public abstract void Call();         //打电话
     }

 

 class Program
    {
        static void Main(string[] args)
        {
            MobilePhone mobilephone = new ApplePhone();
            mobilephone.SendMessage();
            mobilephone.Call();

            Bluetooth bluetooth = new Bluetooth(mobilephone);
            bluetooth.Connect();

            GPS gps = new GPS(bluetooth);
            gps.Localtion = "(3,4)";

            Camera camera = new Camera(gps);

            camera.Call();

            Console.ReadLine();

        }
    }

装饰模式

原文:http://www.cnblogs.com/victorchi/p/5089989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!