Composite definition: organize objects in a tree structure to achieve a "part-total" hierarchy, so that the client has consistent use of individual objects and combined objects.
Composite is easier to understand. When you think of Composite, you should think of tree structure diagrams. These objects in the composition body have a common interface. When the method of an object in the composition body is called and executed, Composite will traverse the entire tree structure (Iterator), find objects that also contain this method and implement call execution. It can be described as pulling a hundred moves.
Therefore, the Composite mode uses Iterator mode, which is similar to the Chain of Responsibility mode.
Composite Benefits:
1. Make client calls simple, and the client can use a combination structure or a single object in a consistent manner. The user does not have to deal with whether the single object or the entire combination structure they are dealing with, which simplifies the client code.
2. It is easier to add object parts into the combination body. The client does not have to change the code because it has added a new object component.
How to use Composite
First, define an interface or abstract class. This is a common method of design patterns. Other design patterns do not have many restrictions on the internal definition of interfaces. However, Composite has a provision, that is, define an internal interface for accessing and managing Composite compositions. Objects (or component Component).
The following code is defined as an abstract class, and generally try to use interface as much as possible.
The code copy is as follows:
public abstract class Equipment{
private String name;
//Internet price
public abstract double netPrice();
//Discount price
public abstract double discountPrice();
//Add parts method
public boolean add(Equipment equipment) { return false; }
//Delete part method
public boolean remove(Equipment equipment) { return false; }
//Note here, here is a method for accessing component classes.
public Iterator iter() { return null; }
public Equipment(final String name) { this.name=name; }
}
The abstract class Equipment is the definition of Component, representing the objects of the composite class, and several common methods are defined in the Equipment.
The code copy is as follows:
public class Disk extends Equipment{
public Disk(String name) { super(name); }
//Define Disk network price to 1
public double netPrice() { return 1.; }
//Define the disk discount price is 0.5% off.
public double discountPrice() { return .5; }
}
Disk is an object, or a component, in the combination body, which is a single element (primitive).
Another possibility is that a component is also a combination, that is, there is a 'son' under this component. This is a common situation in a tree structure and should be easier to understand. Now we need to define this combination:
The code copy is as follows:
abstract class CompositeEquipment extends Equipment{
private int i=0;
//Define a Vector to store 'son'
private Lsit equipment=new ArrayList();
public CompositeEquipment(String name) { super(name); }
public boolean add(Equipment equipment) {
this.equipment.add(equipment);
return true;
}
public double netPrice(){
double netPrice=0.;
Iterator iter=equipment.iterator();
for(iter.hasNext())
netPrice+=((Equipment)iter.next()).netPrice();
return netPrice;
}
public double discountPrice(){
double discountPrice=0.;
Iterator iter=equipment.iterator();
for(iter.hasNext())
discountPrice+=((Equipment)iter.next()).discountPrice();
return discountPrice;
}
//Note here, here is a method for accessing components in your own combination.
//The reason why dIsk above does not exist is because Disk is a single element.
public Iterator iter(){
return equipment.iterator() ;
}
//Overload the Iterator method
public boolean hasNext() { return i<equipment.size(); }
//Overload the Iterator method
public Object next(){
if(hasNext())
return equipment.elementAt(i++);
else
throw new NoSuchElementException();
}
}
The above CompositeEquipment inherits the Equipment, and provides external access methods for the objects inside it, overloading Iterator. Iterator is an interface of Java Collection and an implementation of the Iterator pattern.
Let’s take a look at the two specific categories of CompositeEquipment: disk box Chassis and box Cabinet. There are many things in the box, such as base plate, power box, hard disk box, etc.; there are some small devices in the disk box, such as hard disk floppy drive, etc. Undoubtedly, both of these are of combination.
The code copy is as follows:
public class Chassis extends CompositeEquipment{
public Chassis(String name) { super(name); }
public double netPrice() { return 1.+super.netPrice(); }
public double discountPrice() { return .5+super.discountPrice(); }
}
public class Cabinet extends CompositeEquipment{
public Cabinet(String name) { super(name); }
public double netPrice() { return 1.+super.netPrice(); }
public double discountPrice() { return .5+super.discountPrice(); }
}
So far we have completed the architecture of the entire Composite model.
We can look at the code of the client calling Compose:
The code copy is as follows:
Cabinet cabinet=new Cabinet("Tower");
Chassis chassis=new Chassis("PC Chassis");
//Insert PC Chassis into Tower (pack the disk into the box)
cabinet.add(chassis);
//Insert a 10GB hard drive to PC Chassis (install the hard drive into the disk box)
chassis.add(new Disk("10 GB"));
//Calling the netPrice() method;
System.out.println("netPrice="+cabinet.netPrice());
System.out.println("discountPrice="+cabinet.discountPrice());
The methods called above are netPrice() or discountPrice(). In fact, Composite uses Iterator to traverse the entire tree structure, find objects that also contain this method and implement call execution.
Composite is a very clever model that embodies wisdom. In practical applications, if we encounter a tree structure, we can try whether we can use this model.
Taking the forum as an example, there are many posts (messages) in a version (forum). These posts have original posts and response posts to the original post. They are a typical tree structure. Of course, you can use the Composite mode, so we enter Jive Take a look at how it is implemented.
Jive anatomy
In Jive, ForumThread is a container container (combination) of ForumMessages. That is to say, ForumThread is similar to the CompositeEquipment in our previous example. Its relationship with messages is as follows:
The code copy is as follows:
[thread]
|- [message]
|- [message]
|- [message]
|- [message]
|- [message]
We see the following code in ForumThread:
The code copy is as follows:
public interface ForumThread {
....
public void addMessage(ForumMessage parentMessage, ForumMessage newMessage)
throws UnauthorizedException;
public void deleteMessage(ForumMessage message)
throws UnauthorizedException;
public Iterator messages();
....
}
Similar to CompositeEquipment, it provides methods for accessing components in your own combination body: adding, deleting, and traversing.
Based on my analysis of Jive in other models, we have basically understood the framework of the Jive forum system. If you didn’t understand the design pattern before and just looked at the Jive source code, you will definitely not be able to understand it.