码迷,mamicode.com
首页 > 其他好文 > 详细

装饰模式实例

时间:2015-11-25 22:02:37      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

用户需求:

技术分享

 

设计思路:

     1.UML图

       技术分享

        2.采用装饰模式动态地给一个对象添加一些额外的功能,就扩展功能来说,提供了比继承更具弹性的替代方案。

具体代码实现:

       1.抽象组件

         MoilePhone类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public abstract class MobilePhone
    {
        public abstract void SendMessage();
        public abstract void Call();
        public abstract string Show();

    }
}  

       2.具体组件

       Apple 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public class Apple:MobilePhone
    {
        public override void SendMessage()
        {
            Console.WriteLine("Apple功能:发短信");
        }
        public override void Call()
        {
            Console.WriteLine("Apple功能:通话");
        }
        public override string Show()
        {
            string str = "Apple功能:发短信\n" + "Apple功能:通话\n";
            return str;

        }

    }
}

       Mi类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public class Mi:MobilePhone
    {
        public override void SendMessage()
        {
            Console.WriteLine("MI功能:发短信");
        }
        public override void Call()
        {
            Console.WriteLine("MI功能:通话");
        }
        public override string Show()
        {
            string str = "Mi功能:发短信\n" + "Mi功能:通话\n";
            return str;
        }
    }
}

        3.抽象装饰类

          Function类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public abstract class Function:MobilePhone
    {
        private MobilePhone mobilephone;
        public void Decorate(MobilePhone phone)
        {
            mobilephone = phone;
        }
        public override void SendMessage()
        {
            mobilephone.SendMessage();
        }
        public override void Call()
        {
            mobilephone.Call();
        }
        public override string Show()
        {
            string str=mobilephone.Show();
            return str;
            
        }

    }
}

       4.具体装饰类

       Bluetooth 类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public class Bluetooth:Function
    {
        public void connection()
        {
            Console.WriteLine("蓝牙模块:连接功能");
        }
        public override string Show()
        {
            string str=base.Show()+"蓝牙模块:连接功能\n";
            return str;
        }
    }
}

        GPS 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public class GPS:Function
    {
        public string Location;
        public override string Show()
        {
            Location = "这里是宁波大学科学技术学院\n";
            string str = base.Show() + "GPS模块定位功能:" + Location;
            return str;
            
        }
    }
}

        Cemera 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    public class Cemera:Function
    {
        public override void Call()
        {
            Console.WriteLine("通话功能升级:视频通话");
        }
        public override string Show()
        {
            string str = base.Show() + "通话功能升级:视频通话\n";
            return str;
          
        }
    }
}

       客户端 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ylx
{
    class Program
    {
        static void Main(string[] args)
        {
            MobilePhone apple = new Apple();
            MobilePhone mi = new Mi();

            Bluetooth bluetooth = new Bluetooth();
            bluetooth.Decorate(apple);
          

            GPS gps = new GPS();
            gps.Decorate(bluetooth);
           

            Cemera cemera = new Cemera();
            cemera.Decorate(gps);
            Console.WriteLine(cemera.Show());
     

            Bluetooth bluetooth1 = new Bluetooth();
            bluetooth1.Decorate(mi);
           

            Cemera cemera1 = new Cemera();
            cemera1.Decorate(bluetooth1);
            Console.WriteLine(cemera1.Show());

            Console.Read();

        }
    }
} 

 

运行结果:

 技术分享    

体会和感悟:

      优点:

      装饰模式可以提供比继承更多的灵活性,以一种动态地方式扩展一个对象的功能,并通过使用不同的具体装饰类以及这些装饰类的排列组合,创造出很多不同行为的组合。

     具体构造类与具体装饰类可以独立变化。

     缺点:    

     使用装饰模式时将产生很多小对象,且比继承更容易出错。

     适用场景:

     它适用于在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。当需要动态地给一个对象增加功能,这些功能也可以动态地被撤销。

装饰模式实例

标签:

原文地址:http://www.cnblogs.com/134ylx/p/4995743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!