Java设计模式--合成模式

合成模式:合成模式把部分和整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象等同看待。

两种形式:根据所实现的接口分为安全式和透明式

合成模式可以不提供父对象的管理方法,但是合成模式必须在合适的地方提供子对象的管理方法。

安全式

安全式的合成模式要求管理聚集的方法只出现在树枝构件类中,而不出现在树叶构件类中。

角色

抽象构件角色:这是一个抽象角色,他给参加组合的对象定义出公共的接口和默认行为,可以用来管理所有子对象。合成对象通常把它包含的子对象当做类型为Component的对象。

树叶构件角色:树叶对象是没有下级子对象的对象,定义出参加组合的原始对象的行为。

树枝构件角色:代表参加组合的有下级子对象的对象。树枝构件类给出了管理子对象的方法。

Component

package com.design.composite.safe;

public interface Component {

    Composite getComposite();

    void operation();

}

Composite

package com.design.composite.safe;

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

public class Composite implements Component {

    private List<Component> list = new ArrayList<>();

    @Override
    public Composite getComposite() {
        return this;
    }

    /**
     * 某个商业方法
     */
    @Override
    public void operation() {
        // by yourself
        for (Component c : list){
            System.out.println(c.toString());
        }
    }

    public void add(Component component){
        list.add(component);
    }

    public void remove(Component component){
        list.remove(component);
    }

    public List components(){
        return list;
    }

}

Leaf

package com.design.composite.safe;

public class Leaf implements Component {

    /**
     * 返回自己的实例
     * @return
     */
    @Override
    public Composite getComposite() {
        return null;
    }

    /**
     * 商业方法
     */
    @Override
    public void operation() {

    }
}

透明式

结构图

与安全式不同的是,透明式的合成模式要求所有的构件类,无论是树枝还是树叶,均符合一个固定的接口。

角色

抽象构件角色:他给参加组合的对象规定一个接口,规范共有的接口和默认行为。这个接口可以管理所有的子对象。有add,remove之类的方法。

树叶构件角色:代表参加组合的树叶对象,定义出参加组合的原始对象的行为。给出add,remove方法平庸的实现。

树枝构件角色:代表参加组合的有子对象的对象,定义出这样对象的行为。

Component

package com.design.composite.transparent;

import java.util.List;

public interface Component {

    Composite getComposite();

    void operation();

    void add(Component component);

    void remove(Component component);

    List components();

}

Composite

package com.design.composite.transparent;

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

public class Composite implements Component {

    private List<Component> list = new ArrayList<>();

    @Override
    public Composite getComposite() {
        return this;
    }

    @Override
    public void operation() {
        for (Component c : list){
            System.out.println(c.toString());
        }
    }

    @Override
    public void add(Component component) {
        list.add(component);
    }

    @Override
    public void remove(Component component) {
        list.remove(component);
    }

    @Override
    public List components() {
        return list;
    }
}

Leaf

package com.design.composite.transparent;

import java.util.List;

public class Leaf implements Component {
    @Override
    public Composite getComposite() {
        return null;
    }

    @Override
    public void operation() {

    }

    @Override
    public void add(Component component) {

    }

    @Override
    public void remove(Component component) {

    }

    @Override
    public List components() {
        return null;
    }
}

一个画图实例

Picture是树枝,Line、Circle、Trangle是树叶。此示例用的安全式合成模式。

Graphics

package com.design.composite.graphics;

public interface Graphics {

    void draw();

}

Line

package com.design.composite.graphics;

public class Line implements Graphics {

    private String color;

    public Line(String color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println("画线-" + color);
    }
}

  

Circle

package com.design.composite.graphics;

public class Circle implements Graphics {

    private String color;

    public Circle(String color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println("画圆-" + color);
    }
}

  

Trangle

package com.design.composite.graphics;

public class Trangle implements Graphics {

    private String color;

    public Trangle(String color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println("画三角形-" + color);
    }
}

  

Picture

package com.design.composite.graphics;

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

public class Picture implements Graphics {

    private List<Graphics> list = new ArrayList<>();

    @Override
    public void draw() {
        System.out.println("Picture contains ...");
        for (Graphics g : list){
            g.draw();
        }
        System.out.println("all of the above");
    }

    public void add(Graphics g){
        list.add(g);
    }

    public void remove(Graphics g){
        list.remove(g);
    }

    public Graphics getChild(int i){
        return list.get(i);
    }

}

  

测试

package com.design.composite.graphics;

public class Client {

    public static void main(String[] args){
        Line line = new Line("白色");
        Circle circle = new Circle("蓝色");
        Trangle trangle = new Trangle("红色");

        Picture picture = new Picture();
        picture.add(line);
        picture.add(circle);
        picture.add(trangle);

        picture.draw();

        System.out.println("---------------");

        Graphics g = picture.getChild(0);
        g.draw();
    }
}

  

结果:

原文地址:https://www.cnblogs.com/LUA123/p/7825006.html