• 《大话设计模式》读书笔记(六)


    写在前面

    本文上接:《大话设计模式》读书笔记(五),这次来学习备忘录模式、组合模式、迭代器模式。

    13.备忘录模式(Memento Pattern)

    解决的问题

    备忘录模式可以在不破坏封装性的前提下,捕获一个对象的内部状态,并且在对象之外保存这个状态,这样以后就可将该对象恢复到原先保存的状态。

    类图结构

    image-20201218221620925

    Originator表示发起人,负责创建一个备忘录,用来记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Memento负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,CareTaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。CareTaker为管理者,负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。

    Originator:

    class Originator{
        private String state;
        public String state{
            get{return state;}
            set {state = value;}
        }
        public Memento createMemento(){
            return (new Memento(state));
        }
        public void setMemento(Memento memento){
            state = memento.state;
        }
        public void show(){
            Console.WriteLine("State = " + state);
        }
    }
    

    Memento:

    class Memento{
        private string state;
        public Memento(string state){
            this.state = state;
        }
        public string State{
            get {return state;}
        }
    }
    

    CareTaker:

    class CareTaker{
        private Memento memento;
        
        public Memento memento{
            get {return memento;}
            set {memento = value;}
        }
    }
    

    好处与坏处

    备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类。或者需要保存的属性只是众多属性中的一小部分的时候,可以根据保存的备忘录信息恢复到前一状态。缺点是比较消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。

    14.组合模式(Composite Pattern)

    解决的问题

    将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

    类图结构

    image-20201218223000294

    Component为组合中的对象声明接口,实现所有类共同接口的默认行为。声明一个接口用于访问和管理Component的子部件。

    abstract class Component{
        protected string name;
        
        public Component(string name){
            this.name = name;
        }
        
        public abstract void Add(Component c);
        public abstract void Remove(Component c);
        public abstract void Display(int depth);
    }
    

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

    class Leaf : Compoent{
        public Leaf(string name)
            : base(name)
        {}
        public override void Add(Component c){
            Console.WriteLine("Cannot add to a leaf");
        }
        public override void Remove(Component c){
            Console.WriteLine("Cannot remove from a leaf");
        }
        
        public override void Display(int depth){
            Console.WriteLine(new String("-",depth) + name);
        }
    }
    

    Composite定义为有枝的节点,用来存储子部件。

    class Composite : Compoent{
        private List<Compoent> 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(Compoent component in children){
                component.Display(depth + 2);
            }
        }
    }
    

    好处与坏处

    组合模式定义了包含着基本对象和复杂的组合对象,组合对象又可以被组合,这样不断递归,就可以很好的实现功能了。但组合模式不容易限制组合中的构件。

    15.迭代器模式(Iterator)

    解决的问题

    迭代器模式提供了一种方法顺序访问一个聚合对象中的各个元素,而不暴露该对象的内部表示。

    类图结构

    image-20201218224141728

    这里就不再上代码了,因为这个模式在很多语言中都有对应的实现。

    好处与坏处

    首先它支持以不同的方式遍历一个聚合对象,且简化了聚合类。但在一定程度上会增加系统的复杂度。

    总结

    总的来说,主要学习了三个设计模式。

  • 相关阅读:
    五种提高 SQL 性能的方法
    join 使用详解方式
    关于MagicAjax的用法
    收藏几段SQL Server语句和存储过程
    ubuntu nfs配置 以及mount.nfs:access denied by server while mounting问题解决
    Hisi开发板上 SQLite3.3.8移植
    父进程非阻塞回收子进程(适用LINUX下C语言的clientserver模型)
    busybox asm/page.h: No such find.
    ubuntu11.10 samba服务器配置
    errno定义
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/14157512.html
Copyright © 2020-2023  润新知