• 设计模式之组合实体模式


    一.组合实体模式

    定义:对象的组成类似于树型结构,组成了"部分-整体"的结构,组成的最小单元有相似性,在更高层次的抽象上。

    结构:

    1.Component(抽象组件),为叶子节点和树节点提供统一的抽象。

    2.Composite(容器构建),在组合对象中表示节点对象,在叶子节点之外的都是该类型的结点。

    3.Leaf(叶子构件),在组合对象中表示叶子节点。

    类图为:

     Component类

    import java.util.ArrayList;
    
    public abstract class Component {
        public  abstract void Opertion(); //操作
        public  abstract void Add(Component Child);//添加子节点
        public  abstract void Remove(int index);//删除节点
        public  abstract ArrayList<Component> getChild(int index);//获取子节点
    }

    Leaf类

    import java.util.ArrayList;
    
    public class Leaf extends Component {
    
        private String leafName =null;
    
        public Leaf(String leafName) {
            this.leafName = leafName;
        }
    
        @Override
        public void Opertion() {
            System.out.println(leafName+"正在迎风摇摆");
        }
    
        @Override
        public void Add(Component Child) {
    
        }
    
        @Override
        public void Remove(int index) {
    
        }
    
        @Override
        public ArrayList<Component> getChild(int index) {
            return null;
        }
    
        public String getLeafName() {
            return leafName;
        }
    
        public void setLeafName(String leafName) {
            this.leafName = leafName;
        }
    }

    Composite类---->Trunk

    import java.util.ArrayList;
    
    public class Trunk extends  Component {
    
        private String TrunkName;
    
        private ArrayList<Component> components =new ArrayList<>(); //多个子节点
    
        public Trunk(String TrunkName) {
            this.TrunkName = TrunkName;
        }
    
        @Override
        public void Opertion() {
            System.out.println(TrunkName+"树干正在抵御秋风");
            for (Component component: components) {
                component.Opertion();
            }
        }
    
        @Override
        public void Add(Component Child) {
            components.add(Child);
        }
    
        @Override
        public void Remove(int index) {
            components.remove(index);
        }
    
        @Override
        public ArrayList<Component>  getChild(int index) {
            return components;
        }
    
        public String getTrunkName() {
            return TrunkName;
        }
    }

    最后运行Main

    public class Main {
    
        public static void main(String[] args) {
            Trunk root = new Trunk("树根");
    
            Trunk trunk = new Trunk("主干");
    
            Trunk t1 = new Trunk("第一分支");
            Trunk t2 = new Trunk("主分支");
            Trunk t3 = new Trunk("第二分支");
    
            Leaf l1 = new Leaf("分叶1");
            Leaf l2 = new Leaf("分叶2");
            Leaf l3 = new Leaf("分叶3");
            Leaf l4 = new Leaf("分叶4");
            Leaf l5 = new Leaf("分叶5");
    
            root.Add(t2);
    
            t2.Add(t1);
            t2.Add(t3);
    
            t1.Add(l1);
            t1.Add(l3);
            t1.Add(l5);
    
            t3.Add(l2);
            t3.Add(l4);
    
            root.Opertion();
        }
    }

    总结:类似于树型结构,使其部分与整体具有结构上的一致性。

    优点:结构简便,使其内部结构透明,对于用户公开

    缺点:根节点部分函数未使用。

  • 相关阅读:
    使用cookie来做身份认证
    讲一下Asp.net core MVC2.1 里面的 ApiControllerAttribute
    不要使用Resource Owner Password Credentials
    nginx部署dotnet core站点
    这可能是最low的发布dotnet core站点到centos7教程
    记一个常见的ms sql server中取第N条记录的方法
    MVC基本开发介绍 (1)列表展示
    WCF 入门(29)
    WCF 入门(25,26,27,28)
    使用 Visual Studio Code 搭建 C/C++ 开发和调试环境
  • 原文地址:https://www.cnblogs.com/ad-zhou/p/11761196.html
Copyright © 2020-2023  润新知