zoukankan      html  css  js  c++  java
  • C# 设计模式(8)桥接模式

    桥接模式

    1.解决多维度问题

    2.变化封装

    代码实现:

    组合 变化封装类  减少子类的数量

    手机系统桥接

    namespace BridgePattern.Bridge
    {
        public interface ISystem
        {
            public string GetSystem();
            public string GetVersion();
        }
        class IOS:ISystem
        {
            public string GetSystem()
            {
                return "IOS";
            }
    
            public string GetVersion()
            {
                return "14.1";
            }
        }
        public class SmartisanOS:ISystem
        {
            public string GetSystem()
            {
                return "Smartisan_OS";
            }
    
            public string GetVersion()
            {
                return "7.0";
            }
        }
    }
    

    手机品牌:

    namespace BridgePattern
    {
        public abstract class BasePhone
        {
            public ISystem PhoneSystem;
            public abstract void UseCall();
            public abstract void UseText();
        }
        public class Iphone:BasePhone
        {
            public override void UseCall()
            {
                Console.WriteLine($"Use {this.GetType().Name} {this.PhoneSystem.GetSystem()} {this.PhoneSystem.GetVersion()} Call Somebody");
            }
    
            public override void UseText()
            {
                Console.WriteLine($"Use {this.GetType().Name} {this.PhoneSystem.GetSystem()} {this.PhoneSystem.GetVersion()} Send Text to Somebody");
            }
        }
        public class Smartisan:BasePhone
        {
            public override void UseCall()
            {
                Console.WriteLine($"Use {this.GetType().Name} {this.PhoneSystem.GetSystem()} {this.PhoneSystem.GetVersion()} Call Somebody");
            }
    
            public override void UseText()
            {
                Console.WriteLine($"Use {this.GetType().Name} {this.PhoneSystem.GetSystem()} {this.PhoneSystem.GetVersion()} Send Text to Somebody");
            }
        }
    }
    

    代码调用:

        class Program
        {
            static void Main(string[] args)
            {
                ISystem iOS = new IOS();
                ISystem smartisanOS = new SmartisanOS();
    
    
                BasePhone iPhone = new Iphone();
                iPhone.PhoneSystem = iOS;
                iPhone.UseCall();
                iPhone.UseText();
    
                BasePhone smartisan = new Smartisan();
                smartisan.PhoneSystem = smartisanOS;
                smartisan.UseCall();
                smartisan.UseText();
    
    
                BasePhone iPhoneSmartisanOS = new Iphone();
                iPhoneSmartisanOS.PhoneSystem = smartisanOS;
                iPhoneSmartisanOS.UseCall();
                iPhoneSmartisanOS.UseText();
    
                BasePhone smartisanIOS = new Smartisan();
                smartisanIOS.PhoneSystem = iOS;
                smartisanIOS.UseCall();
                smartisanIOS.UseText();
            }
        }
    

    结果:

  • 相关阅读:
    ENode框架Conference案例分析系列之
    ENode框架Conference案例分析系列之
    ENode框架Conference案例分析系列之
    ENode 2.6 架构与设计简介以及全新案例分享
    C#分布式消息队列 EQueue 2.0 发布啦
    EQueue 2.0 性能测试报告
    EQueue文件持久化消息关键点设计思路
    213.家庭账务管理信息系统
    212.基于DCT变换的水印算法模拟
    211.哈希表实现活期储蓄账目管理系统
  • 原文地址:https://www.cnblogs.com/YourDirection/p/14072879.html
Copyright © 2011-2022 走看看