zoukankan      html  css  js  c++  java
  • 装饰模式实例

    用户需求:

    设计思路:

         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功能:发短信
    " + "Apple功能:通话
    ";
                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功能:发短信
    " + "Mi功能:通话
    ";
                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()+"蓝牙模块:连接功能
    ";
                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 = "这里是宁波大学科学技术学院
    ";
                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() + "通话功能升级:视频通话
    ";
                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();
    
            }
        }
    } 

    运行结果:

         

    体会和感悟:

          优点:

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

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

         缺点:    

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

         适用场景:

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

  • 相关阅读:
    maven项目部署到tomcat中没有classe文件的问题汇总
    Tomcat远程调试模式及利用Eclipse远程链接调试
    FastDFS 常见问题
    Linux Crontab 定时任务 命令详解
    EChart 关于图标控件的简单实用
    java 通过zxing生成二维码
    Mybatis typeAliases别名
    Mybatis 实现手机管理系统的持久化数据访问层
    Mybatis 实现传入参数是表名
    Mybatis关于like的字符串模糊处理
  • 原文地址:https://www.cnblogs.com/134ylx/p/4995743.html
Copyright © 2011-2022 走看看