装饰模式实例

用户需求:

 

设计思路:

     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();

        }
    }
} 

 

运行结果:

     

体会和感悟:

      优点:

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

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

     缺点:    

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

     适用场景:

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

转载于:https://www.cnblogs.com/134ylx/p/4995743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值