The old monk tells the story to the young monk, which triggered the Java design pattern: combination pattern

Example

There is a drawing system that can draw various graphics. Suppose you can now draw lines, rectangles, and circles.
Define an abstract class. All graphics inherit this class to complete the drawing:

public abstract class Graphics {
    
    

    /** 绘图 */
    public abstract void draw();
}

Line, rectangle, and circle implement the above abstract classes:

public class Line extends Graphics {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("画一条线");
    }
}
public class Rect extends Graphics {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("画一个矩形");
    }
}
public class Circle extends Graphics {
    
    
    @Override
    public void draw() {
    
    
        System.out.println("画一个圆形");
    }
}

Now you need to add the various graphics above to the drawing board, and then draw:

public class Picture extends Graphics {
    
    

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

    @Override
    public void draw() {
    
    
        for (Graphics g : list) {
    
    
            g.draw();
        }
    }

    /** 添加一个图形 */
    public void add(Graphics graphics) {
    
    
        list.add(graphics);
    }

    /** 移除一个图形 */
    public void remove(Graphics graphics) {
    
    
        list.remove(graphics);
    }

    /** 获取一个图形 */
    public Graphics getChild(int i) {
    
    
        return list.get(i);
    }
}

Test category:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Picture picture = new Picture();
        picture.add(new Line());
        picture.add(new Rect());
        picture.add(new Circle());
        picture.draw();
    }
}

Insert picture description here

Combination mode

definition

The combined mode is also called the part-whole mode. The composite mode organizes objects into a tree structure and can be used to describe the relationship between the whole and the part. The composite mode allows the client to treat simple elements and composite elements equally.

intention

Combine objects into a tree structure to represent a "part-whole" hierarchical structure. The combination mode enables users to use a single object and combined objects consistently

Mainly solve the problem

The client program can handle complex elements like simple elements, thereby decoupling the client program from the internal structure of complex elements

Pros and cons

advantage:

  • Simple calling of high-level modules
  • Nodes can be added freely

Disadvantages:
When using the combined mode, the declarations of its leaves and branches are implementation classes, not interfaces, which violates the principle of dependency inversion

Safe and transparent combination mode

The following figure omits the details of each role, and does not give
Insert picture description here
the roles involved in each of their methods :

  • Abstract construction (Component) role: It specifies an interface for the objects participating in the composition, and this role gives a common interface and default behavior
  • Leaf role: represents the leaf object participating in the combination, a leaf has no sub-objects of subordinates, and defines the behavior of the original object participating in the combination
  • Branch construction (Composite) role: Represents the objects with sub-objects participating in the combination, and gives the behavior of branch object construction

The Composite object can contain other Component type objects, that is, it can contain other branch objects and leaf objects
Insert picture description here

The structure of the safe synthetic mode

The methods required to manage aggregation appear only in the branch building class, not in the leaf building class, that is, the method of managing subclass objects is declared in the Composite class.
Insert picture description here
The roles involved:

  • Abstract construction (Component) role: It specifies an interface for the objects participating in the composition. This role gives a common interface and default behavior, which can be used to manage all sub-objects. In the safe composition mode, the construction role is not Define the method of managing sub-objects, this definition is given by the branch construction
  • Leaf role: represents the leaf object participating in the combination, a leaf has no sub-objects of subordinates, and defines the behavior of the original object participating in the combination
  • Branch construction (Composite) role: Represents the objects that have sub-objects participating in the combination, and gives the behavior of branch object construction. It will give all the methods of managing sub-objects, such as add, remove, etc.

The corresponding source code is as follows:

public interface Component {
    
    

    /** 返还自己的实例 */
    Composite getComposite();

    /** 某个商业方法 */
    void sampleOperation();
}
public class Composite implements Component {
    
    

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

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

    @Override
    public void sampleOperation() {
    
    
        compositeList.forEach(v -> {
    
    
            v.sampleOperation();
        });
    }

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

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

    public List<Component> components() {
    
    
        return compositeList;
    }
}
public class Leaf implements Component {
    
    
    @Override
    public Composite getComposite() {
    
    
        return null;
    }

    @Override
    public void sampleOperation() {
    
    

    }
}

Transparent composite mode structure

The transparent synthesis mode requires that all specific construction classes, whether branches or leaves, must implement a fixed interface
Insert picture description here
. Roles involved:

  • Abstract construction (Component) role: It specifies an interface for the objects participating in the combination. This role gives a common interface and default behavior. It can be used to manage all sub-objects. It will give all methods for managing sub-objects. Such as add, remove, etc.
  • Leaf role: represents the leaf object participating in the combination, a leaf has no sub-objects of subordinates, and defines the behavior of the original object participating in the combination
  • Branch construction (Composite) role: Represents the objects with sub-objects participating in the combination, and gives the behavior of branch object construction

The corresponding source code is as follows:

public interface Component {
    
    

    /** 返还自己的实例 */
    Composite getComposite();

    /** 某个商业方法 */
    void sampleOperation();

    /** 增加一个子构建对象 */
    void add(Component component);

    /** 删除一个子构建对象 */
    void remove(Component component);

    /** 返回所有的构建对象 */
    List<Component> components();
}
public class Composite implements Component {
    
    

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

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

    @Override
    public void sampleOperation() {
    
    
        compositeList.forEach(v -> {
    
    
            v.sampleOperation();
        });
    }

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

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

    @Override
    public List<Component> components() {
    
    
        return compositeList;
    }
}
public class Leaf implements Component {
    
    
    @Override
    public Composite getComposite() {
    
    
        return null;
    }

    @Override
    public void sampleOperation() {
    
    

    }

    @Override
    public void add(Component component) {
    
    

    }

    @Override
    public void remove(Component component) {
    
    

    }

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

The story of the old monk and the young monk

There used to be a mountain and a temple in the mountain. There was an old monk in the temple who was telling a story to the little monk. The story is about a mountain and a temple in the mountain. There was an old monk in the temple who was telling a story to the little monk. …
The story here is the above-mentioned branch construction, which includes mountains, temples, and monks, while mountains, temples, and monks are constructed from leaves. There are no internal characters
Insert picture description here
to see. The story objects include mountain objects, temple objects, and monk objects. , Story object, and so on. The
Insert picture description here
corresponding sample code is as follows:
The interface for storytelling, that is, the story is an abstract construction role:

public interface StoryComponent {
    
    

    /** 讲故事 */
    void tellStory();
}

The following is the role that leaves build, that is, the story:

public class StoryComposite implements StoryComponent {
    
    

    private List<StoryComponent> componentList = new ArrayList<>();

    @Override
    public void tellStory() {
    
    
        for (StoryComponent s : componentList) {
    
    
            s.tellStory();
            if (s.getClass().getName().contains("MonkLeaf")) {
    
    
                tellStory();
            }
        }
    }

    public void add(StoryComponent component) {
    
    
        componentList.add(component);
    }

    public void remove(StoryComponent component) {
    
    
        componentList.remove(component);
    }

    public List<StoryComponent> getChild() {
    
    
        return componentList;
    }
}

The following are mountains, temples, and monks, with no child objects:

public class MountainLeaf implements StoryComponent {
    
    
    @Override
    public void tellStory() {
    
    
        System.out.println("从前有座山");
    }
}
public class TempleLeaf implements StoryComponent {
    
    
    @Override
    public void tellStory() {
    
    
        System.out.println("山里有个庙");
    }
}
public class MonkLeaf implements StoryComponent {
    
    
    @Override
    public void tellStory() {
    
    
        System.out.println("庙里有个老和尚,老和尚再给小和尚讲故事,讲的什么故事呢?");
        System.out.println("讲的是:");
        System.out.println("----------------------------------------------------------------------");
    }
}

Started to tell the story:

public class StoryTest {
    
    

    public static void main(String[] args) {
    
    
        //故事
        StoryComposite story = new StoryComposite();
        //叶子:山、庙、道士
        StoryComponent mountain = new MountainLeaf();
        StoryComponent temple = new TempleLeaf();
        StoryComponent monk = new MonkLeaf();

        //添加子构建对象
        story.add(mountain);
        story.add(temple);
        story.add(monk);
        //开始讲故事
        story.tellStory();
    }
}

Insert picture description here
Then you will find that the old monk is exhausted, haha. . .
Insert picture description here
Class Diagram:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_34365173/article/details/108171979