zoukankan      html  css  js  c++  java
  • 三十二.设计模式总结结构型模式

    设计模式总结

    二.结构型模式

    1,适配器模式

           将一个第三方类方法,转换到另一个类中的调用的方法。

           优点:使原本接口不兼容而不能工作的类一起工作,复用方法

           缺点:改动源类会带来麻烦

           总结:多用于后期维护,修改时,复用类似类,方法或控件时使用。

           Class Translator//适配器

           {

    private F f=new F();

           Pulic override void Add()       {f.加();}

    }

    2,桥接模式

           将抽象部分与它的实现部分分离,使他们都可以独立地变化

           优点:可以对不同抽象接口和实现部分进行组合,并分别扩充。

                   对抽象的实现部分的修改对客户端不产生影响

           总结:复杂了桥接提取部分,可以随意组合。对客户端不影响。

                   多用于在客户端调用实现部分有不同组合时使用。

           class Abstraction//提取

        {

           protected Implementor implementor;

           public void SetImplementor(Implementor implementor)

           {this.implementor = implementor; }

           public virtual void Operation(){implementor.Operation();}

        }

    3,组合模式

           组合成树形结构表示对象的部分-整体层次结构。

           优点:是用户对单个对象和组合对象的使用一致,忽略单个和整体区别

           缺点:使用时必须符合部分-整体的情况。分清叶和节点的区别。

           总结:组合模式将复杂逻辑放置于内部,减少客户端使用压力。

                   特点较明显,树形结构,组合明显的系统中使用。

                   使用时比较叶和节点,很好理解。

        class Leaf:Component//在组合中表示叶节点对象,叶节点没有子节点

        {

           public Leaf(string name) : base(name) { }

           public override void Add(Component c) {Console.WriteLine("Cannotadd to a leaf");}

           public override void Remove(Component c) { ("Cannot remove from aleaf");}

           public override void Display(int depth)

           {Console.WriteLine(new string('-',depth)+name); }

    }

        class Composite:Component//定义有枝节点行为,用来存储子部件

        {

           private List<Component> children = new List<Component>();

           public Composite(string name) : base(name) { }

           public override void Add(Component c) {children.Add(c); }

           public override void Remove(Component c) {children.Remove(c); }

           public override void Display(int depth)

           {

                Console.WriteLine(new string('-',depth) + name);

               foreach (Component co in children) {co.Display(depth+2); }

           }

    }

    4,装饰模式

           为已有功能动态的添加更多功能。

           优点:动态添加功能。实例化的所有继承与装饰类,都可以相互添加功能。

           总结:首先所有的实现类都是继承于同一个装饰类。

                   这些实现类的实现方法需要相互添加。满足这两条件时使用。

        abstract class Decorator:Component//装饰类

        {

            protected Component component;

            public void SetComponent(Componentcomponent) {this.component = component; }       

    public override voidOperation(){if (component != null) {component.Operation();}}

        }

    5,外观模式

           封装子系统所提供的一组接口,方便程序调用。

           优点:封装一些列复杂的接口方法调用,使用时更简洁,不必关心更多

           缺点:封装一组复杂接口时,被封死,不易改动,不了解内部机制

    总结:外观模式高内聚,低耦合。但封装内部一组接口时,不易改动。内部方法逻

    辑稳定时使用

       class Home//外观模式

        {

           Key key; Door door;

           public Home(){key = new Key();door = new Door();}

           public void InHome(){key.InKey();door.OpenDoor();}

           public void OutHome(){door.CloseDoor();key.OutKey();}

        }

    6,享元模式

           运用共享技术有效地支持大量细粒度的对象

           优点:减少大量对象的使用,将对象的大多数状态放到外部

           缺点:除去外部状态,对象要稳定一直,因为是共享的

           总结:在大量使用类似对象时使用。比如围棋中的棋子,可以将坐标状态放到外部。

           UsingSystem.Collections;

           classWebSiteFactory//网站工厂

        {

           private Hashtable flyweights = new Hashtable();

           public WebSite GetWebSiteCategory(stringkey) //获得网站分类

           {

               if (!flyweights.ContainsKey(key))

               {flyweights.Add(key,new ConcreteWebSite(key)); }

               return ((WebSite)flyweights[key]);

           }

           public int GetWebSiteCount()//获取网站总数{return flyweights.Count; }

        }

    7,代理模式

           隐藏真实模块。为其他对象提供一种代理以控制对这个对象的访问

           优点:隐藏真实模块

           总结:类似于适配器,不过只是类名不同,方法名相同

       class Proxy : Subject//代理类

        {

           RealSubject realsubject;

           public override void Request()

           {

               if (realsubject == null) {realsubject = new RealSubject();}

               realsubject.Request();

           }

    }

  • 相关阅读:
    Openfire 调试信息
    ejabberd分析(三)启动流程
    Openfire 离线消息的处理机制
    OpenFire SSLSocketFactory 编译报错解决
    用消息队列和消息应用状态表来消除分布式事务
    XMPP文件传输过程
    ejabberd分析(一)
    erlang(1)
    Openfire 好友状态的发送(用户登录)
    ejabberd分析(二) 用户注册
  • 原文地址:https://www.cnblogs.com/yaoge/p/1815271.html
Copyright © 2011-2022 走看看