【设计模式】组合模式(Composite)

引入

  • 商品类别树的节点被分成两种,一种是容器节点,另一种是叶子节点。
  • 容器节点可以包含其他容器节点或者叶子节点

组合模式

  • 组合模式有时又叫做部分——整体模式(Part-Whole)
    。组合模式将对象组织到树结构中,可以用来描述整体与部分的关系。组合模式可以使客户端将单纯元素与复合元素同等看待。
  • 一个树结构由两种节点组成:树枝节点和树叶节点。树枝节点可以有子节点,而一个树叶节点不可以有子节点。除了根节点外,其它节点有且只有一个父节点。

模式结构

在这里插入图片描述

  • 抽象构件(Component)角色
    这是一个抽象角色,它给参与组合的对象规定一个接口。这个角色给出共有接口及其默认行为。
  • 树叶构件(Leaf) 角色
    代表参加组合的树叶对象。一个树叶对象没有下级子对象。
  • 树枝构件(Composite)角色
    代表参加组合的有子对象的对象,并给出树枝构件对象的行为。

示意性代码

//抽象节点类
    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);
    }

    //叶子节点类
    class Leaf:Component
    {
        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);
        }
    }

    //组合类
    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 Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + name);
            foreach(Component component in children)
            {
                component.Display(depth + 2);
            }
        }

        public override void Remove(Component c)
        {
            children.Remove(c);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Composite root = new Composite("root");
            root.Add(new Leaf("LeafA"));
            root.Add(new Leaf("LeafB"));

            Composite comp = new Composite("CompositeX");
            comp.Add(new Leaf("LeafXA"));
            comp.Add(new Leaf("LeafXB"));

            root.Add(comp);

            Composite comp2 = new Composite("CompositeXY");
            comp2.Add(new Leaf("LeafXYA"));
            comp2.Add(new Leaf("LeafXYB"));

            comp.Add(comp2);

            root.Add(new Leaf("LeafC"));

            Leaf leaf = new Leaf("LeafD");
            root.Add(leaf);
            root.Remove(leaf);

            root.Display(1);

            Console.Read();
        }
    }

认识组合模式

  • 组合模式的目的
    让客户端不再区分操作的是组合对象还是叶子对象,而是以一种统一的方式来操作
  • 对象树
    组合模式会组合出树形结构来,这也就意味着,所有可以使用对象树来描述或操作的功能,都可以考虑使用组合模式。比如读取XML文件,或是对语句进行语法分析等。
  • 组合模式的实现根据所实现接口的区别分为两种形式,分别称为安全模式和透明模式。组合模式可以不提供父对象的管理方法,但组合模式必须在合适的地方提供子对象的管理方法(诸如Fadd、remove、Display等)。

透明方式

  • 在Component里面声明所有的用来管理子类对象的方法,包括add ()、remove (),以及Display ()方法。
  • 优点:所有的构件类都有相同的接口。在客户端看来,树叶类对象组合类对象的区别起码在接口层次上消失了,客户端可以同等的对待所有的对象。这就是透明形式的组合模式。
  • 缺点:不够安全,因为树叶类对象和合成类对象在本质上是有区别的。树叶类对象不可能有下平个层次的对象,因此add()、remove()以及Display ()方法没有意义,在编译时期不会出错,而会在运行时期才会出错。

安全方式

  • 在Composite类里面声明所有的用来管理子类对象的方法
  • 优点:这样的做法是安全的做法,树叶类型的对象根本就没有管理子类对象的方法,因此,如果客户端对树叶类对象使用这些方法时,程序会在编译时期出错。
  • 缺点:不够透明,树叶类和合成类将具有不同的接口

使用情况

  • 需求中是体现部分与整体层次的结构时
  • 希望用户忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时。

优点

  • 定义了包含基本对象和组合对象的类层次结构
    基本对象可以组合成组合对象,组合对象又能组合成更复杂的组合对象,可以不断地递归组合下去,从而构成一个统一的组合对象的类层次结构
  • 统一了组合对象和叶子对象
  • 简化了客户端调用
    不用区分组合对象和叶子对象
  • 更容易扩展
    由于客户端是统一的面对Component来操作,因此,新定义的Composite或leaf子类能够很容易的与已有的结构一起工作,而不需改变客户端

缺点

很难限制组合中的组件类型
这是容易添加新的组件带来的问题,在需要检测组件类型的时候,使得我们不能依靠编译期的类型约束来完成,必须在运行期间动态检测

本质

同意叶子对象和组合对象

实例

ps:仅代表个人思路
【问题】构造商品类别树
在这里插入图片描述
【代码】

public abstract class Clothes {
	
	protected String name;
	
	public Clothes(String name) {
		this.name=name;
	}
	
	public abstract void add(Clothes c);
	public abstract void remove(Clothes c);
	public abstract void display();

}
import java.util.ArrayList;
import java.util.List;

public class ColthesType extends Clothes {
	
	private List<Clothes> clothes=new ArrayList<Clothes>();

	public ColthesType(String name) {
		super(name);
	} 

	@Override
	public void add(Clothes c) {
		clothes.add(c);
	}

	@Override
	public void remove(Clothes c) {
		clothes.remove(c);
	}

	@Override
	public void display() {
		System.out.println(super.name+":");
		for(Clothes a:clothes) {
			a.display();
		}
	}

}

public class ClothingSpecific extends Clothes {

	public ClothingSpecific(String name) {
		super(name);
	}

	@Override
	public void add(Clothes c) {
		System.out.println("Cannot add to a leaf!");

	}

	@Override
	public void remove(Clothes c) {
		System.out.println("Cannot remove from a leaf!");
	}

	@Override
	public void display() {
		System.out.print("-");
		System.out.println("-"+name);
	}

}

public class Client {

	public static void main(String[] args) {
		ColthesType clothes =new ColthesType("服装");
		ColthesType man=new ColthesType("男装");
		ClothingSpecific cy=new ClothingSpecific("衬衣");
		ClothingSpecific jk=new ClothingSpecific("夹克");
		man.add(cy);
		man.add(jk);
		clothes.add(man);
		
		ColthesType woman=new ColthesType("女装");
		ClothingSpecific cl1=new ClothingSpecific("裙子");
		ClothingSpecific cl2=new ClothingSpecific("套装");
		woman.add(cl1);
		woman.add(cl2);
		clothes.add(woman);
		
		clothes.display();
	}

}

【UML图】
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值