This is the 14th day of my participation in the First Challenge 2022
Portfolio model
concept
Composite Pattern, also called partial whole Pattern, is mainly used to treat a group of similar objects as a single object. The composite pattern combines objects based on a tree structure, which is used to represent both part and whole hierarchies. This type of design pattern is a structural pattern that creates a tree structure of groups of objects.
This pattern creates a class that contains its own group of objects. This class provides a way to modify the same object group.
The combination mode is generally used to represent the relationship between whole and part, organizing objects into a tree structure. The topmost node is also called the root node, which contains sub-trees and leaf nodes. The sub-tree includes sub-trees and leaf nodes.
It can be seen from the figure above that the root node and the subtree are all of the same data type, while the leaf node and the subtree are not of the same type by definition. However, in the combined mode, the root node, the subtree and the leaf node are all regarded as the same data type or the same interface definition.
Application scenarios
-
hierarchy
-
Organizational structure of department
-
You want the client to ignore the difference between a composite object and a single object
-
Object hierarchy has the relationship of whole and part, and presents a tree structure
Transparent composition pattern writing
The combination mode generally has three roles, namely abstract root node role, leaf component role (leaf node) and branch component role (branch node). The concepts are as follows:
-
Composite: defines the common methods and properties of the system song hierarchy object. It can define some default behaviors and properties in advance. It is the object declaration interface of the Composite and realizes the default behavior of the common interface of all classes.
-
Composite: Defines the behavior of Composite parts with branch node parts, stores sub-parts, and combines branch and leaf nodes to form a tree structure. The main thing is to implement operations related to child widgets in the Component interface.
-
Leaf role (Leaf) : A Leaf role is a Leaf node, which has no other branches below and is the smallest level of system convenience.
In general, the combination mode can be divided into two types in the specific implementation of code. One is transparent combination mode, and the other is always safe combination mode. This topic focuses on transparent composition patterns.
The transparent Composite pattern defines all the common methods in the Composite, so that there is no need to distinguish between the leaf nodes and the branch nodes, which have the same interface type.
- Abstract Root node (Composite)
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract String operation(a);
public boolean addChild(Component component) {
throw new UnsupportedOperationException("addChild not supported!");
}
public boolean removeChild(Component component) {
throw new UnsupportedOperationException("removeChild not supported!");
}
public Component getChild(int index) {
throw new UnsupportedOperationException("getChild not supported!"); }}Copy the code
- Branch member Composite
public class Composite extends Component {
private List<Component> mComponents;
public Composite(String name) {
super(name);
this.mComponents = new ArrayList<Component>();
}
@Override
public String operation(a) {
StringBuilder builder = new StringBuilder(this.name);
for (Component component : this.mComponents) {
builder.append("\n");
builder.append(component.operation());
}
return builder.toString();
}
@Override
public boolean addChild(Component component) {
return this.mComponents.add(component);
}
@Override
public boolean removeChild(Component component) {
return this.mComponents.remove(component);
}
@Override
public Component getChild(int index) {
return this.mComponents.get(index); }}Copy the code
- Leaf Component Role (Leaf) Leaf node
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public String operation(a) {
return this.name; }}Copy the code
- UML diagrams
The transparent composition pattern adds all methods to the Component so that the client doesn’t need to know about leaf and branch nodes, but it violates one of the seven principles of the design pattern: the single responsibility principle, the least known principle, The leaf node then inherits methods it doesn’t need (ways to manage other operations), which violates the design pattern principle of interface isolation. So there’s another safe way to write it, the safe combination mode.
Safe combination mode writing
The secure composition pattern is to specify only the most basic consistent behavior at each level of the system, and to put the composition of its own methods into itself.
- Component abstracts the root node
public abstract class Component {
protected String name;
public Component(String name) {
this.name = name;
}
public abstract String operation(a);
}
Copy the code
- Composite (Branch assembly)
public class Composite extends Component {
private List<Component> mComponents;
public Composite(String name) {
super(name);
this.mComponents = new ArrayList<Component>();
}
@Override
public String operation(a) {
StringBuilder builder = new StringBuilder(this.name);
for (Component component : this.mComponents) {
builder.append("\n");
builder.append(component.operation());
}
return builder.toString();
}
public boolean addChild(Component component) {
return this.mComponents.add(component);
}
public boolean removeChild(Component component) {
return this.mComponents.remove(component);
}
public Component getChild(int index) {
return this.mComponents.get(index); }}Copy the code
- Leaf node
public class Leaf extends Component
public Leaf(String name) {
super(name);
}
@Override
public String operation(a) {
return this.name; }}Copy the code
- UML diagrams
Through the above code and UML diagrams, can know the security portfolio model is to make the interface definition accused clear, at the same time, comply with the design pattern of single responsibility principle and the interface segregation principle, although the security portfolio model conforms to the design principles, but also has shortcomings, mainly is the customer need to distinguish between the branch node and leaf nodes, to correctly handle operation between all levels, Customers cannot rely on abstractions, which violates the dependency inversion principle of design patterns.
Both the transparent combination mode and the secure combination mode have their own advantages and disadvantages. Choose one of the two combination methods according to the details of your business scenario.
The pros and cons of the composite model
advantages
-
High-level modules (clients) are easy to call. The composite pattern simplifies client code by allowing it to work consistently with both single and composite objects, without caring whether it is working with a single object or a composite object
-
The nodes are added freely, and it is easier to add new objects into the composition body. The client will not change the source code because of adding new objects, which meets the “open and closed principle”.
disadvantages
-
When the composite pattern is used, its leaves and branches are declared as implementation classes, not interfaces, violating the dependency inversion principle
-
The design is more complex, and the client needs to spend more time sorting out the hierarchical relationships between classes
-
It is not easy to restrict components in containers
-
It is not easy to add new functionality to components by inheritance