Composite Definition
:
Organize objects in a tree structure to achieve "partial-whole"
Makes the client consistent with the use of a single object and a composite object.
Composite is easier to understand.
Composite should come up with a tree structure. These objects in the composite have common interfaces. When the method of an object in the composite is called and executed, composite traverses
(Iterator) the entire tree structure, find the object that also contains this method and implement the call execution. It can be described as a hundred moves.
Therefore, the composite mode is used
The iterator mode is similar to the chain of responsibility mode.
Benefits of Composite
:
1. Make the client simple to call. The client can use a combination structure or
For a single object, the user does not have to deal with a single object or the entire combination structure, which simplifies the client code.
2. It is easier to add object parts to the composite body.
The client does not have to change the code because it has added new object parts.
How to Use composite?
First, define an interface or abstract class. This is a common design mode.
The mode has few restrictions on the internal definition of the interface, but composite has a rule, that is, to define an object (or component) inside the interface for accessing and managing the composite combination.
Component ).
The following code is defined as an abstract class. Generally, the interface is used whenever possible,
Public Abstract class equipment {
Private string name;
//
Network price
Public abstract double Netprice ();
//
Discount price
Public Abstract double discountprice ();
//
Add part Method
Public Boolean add (Equipment Equipment) {return false ;}
//
Part deletion method
Public Boolean Remove (equipment) {return false ;}
//
Note that here we provide a component method for accessing the Assembly class.
Public Iterator ITER () {return NULL ;}
Public equipment (final string Name) {This. Name = Name ;} }
|
The abstract class equipment is
The component definition represents the objects of the composite class. Equipment defines several common methods.
Public Class disk extends Equipment {
Public Disk (string name) {super (name );}
//
Definition Disk
The network price is 1
Public Double netprice () {return 1 .;}
//
Defined Disk
Discount price is 0.5
Discount.
Public double discountprice (){ Return. 5 ;} }
|
Disk is an object in a combination, or a part.
The component is a separate element (primitive ).
Another possibility is that a component is also a combination, that is, there is a 'son' under this component, which is the common condition in the tree structure.
It should be easier to understand. Now we need to define this combination:
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 ){ 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 that here we provide a method to access the parts in the body of your group. // Above Disk This is because disk is a single (primitive) element. Public iterator ITER () { Return equipment. iterator (); { // Reload the iterator Method Public Boolean hasnext () {return I <equipment. Size ();} // Reload the iterator Method Public object next () { If (hasnext ()) Return Equipment. elementat (I ++ ); Else Throw new Nosuchelementexception (); } }
|
Above compositeequipment
It inherits equipment and provides external access methods for the objects in it. It reloads iterator, which is a collection object of Java.
An interface is implemented in the iterator mode.
Let's take a look at compositeequipment.
Two specific categories: the disk box chassis and the box cabinet. There are many things in the box, such as the baseboard, power box, and hard disk box. There are some small devices in the disk box, such as hard drive.
Undoubtedly, both of them belong to the combination.
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 ();} }
|
Now we have completed the entire composite model.
Architecture.
Let's look at the client call
Composote code:
Cabinet
Cabinet = new Cabinet ("Tower ");
Chassis chassis = new chassis ("PC
Chassis ");
// Install PC chassis into the tower (bind the disk to the box)
Cabinet. Add (chassis );
//
Attach a 10 Gb hard disk to PC chassis (attach the hard disk to the disk box)
Chassis. Add (new disk ("10 Gb "));
//
Call the netprice () method;
System. Out. println ("netprice =" + Cabinet. netprice ());
System. Out. println ("discountprice =" + Cabinet. discountprice ());
The method netprice () or
Discountprice (). In fact, composite uses iterator to traverse the entire tree structure, find the object that also contains this method, and implement invocation.
Composite is a smart model.
In the application, if we encounter a tree structure, we can try whether this mode can be used.
Taking the Forum as an example, there is a lot
Multi-post (Message), these posts have original posts and response stickers for the original post, which is a typical tree structure, so of course you can use the composite mode, then we enter the jive
See how it is implemented.
Jive anatomy
In jive
Forumthread is the container iner of forummessages. That is to say, forumthread is similar
Compositeequipment. Its relationship with messages
[Thread]
|-[Message]
|-[Message]
|-[Message]
|-[Message]
|-
[Message]
We can see the following generation in forumthread:
Code:
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,
Provides a method to access the parts in the self-assembled body: Add Delete traversal.
Combined with the analysis of jive in other modes
You have basically understood the framework of the jive forum system. If you do not understand the design mode before, but directly go to the jive source code, you certainly cannot understand it.