Design mode Composite (combination)

xiaoxiao2021-04-05  270

Composite Definition: Tissue objects in a tree structure to achieve a "part-overall" hierarchy such that the client has consistency for a single object and combination objects.

Composite is easier to understand, thinking that Composite should think of the tree structure diagram. These objects have a common interface. When the method of the assembly is executed, Compositerator will traverse the entire tree structure, find the object that contains this method and implements the call execution. You can use it to take a hundred to describe it.

So the Composite mode is similar to the iTerator mode, and the CHAIN ​​OF RESPONSIBILITY mode is similar.

The benefits of Composite: 1. Make the client calls simple, the client can consistently use the combined structure or a single object, the user does not have to have a single object or the entire combination structure, which simplifies the client code. 2. It is easier to add object components in the combination. The client does not have to change the code because of the new object parts.

How to use Composite? First, an interface or abstract class is defined. This is a general mode of design mode. Other design patterns are not limited to the internal definition limitations, and Composite has a regulation, that is, to define an interior to define an interface for access and management The objects of the Composite assembly (or the part component ".

The following code is defined in an abstract class, generally try to use interface Interface.

public abstract class Equipment {private String name; // network prices public abstract double netPrice (); // discounted prices on public abstract double discountPrice (); // member increases method public boolean add (Equipment equipment) {return false;} // Delete component method public boolean remove (equipment equipment) {Return False;} // Note that here is provided here to access component methods for accessing the composition. Public iterator it () {return null;} public equipment (final string name) {this.name = name;}}

Abstract class Equipment is a component definition that represents several common methods in equipment, which define several common methods in equipment.

Public class disk (String name) {super (name);} // Define Disk network price is 1 public double netprice () {return 1 .;} // Define Disk discount price is 0.5 fold. Public double discountprice () {return .5;}}

Disk is an object in the combination, or called a part, this part is a single element (Primitive). There is also a possibility that a component is also a combination, that is, there is also 'son' below this part, which is usually the usual situation in the tree structure, which should be easier to understand. Now let's first define this assembly:

abstract class CompositeEquipment extends Equipment {private int i = 0; // Vector for storing a definition of 'son' private Lsit equipment = new ArrayList (); public CompositeEquipment (String name) {super (name);} public boolean add (Equipment {this.Equipment; return true;} public double netprice () {double netprice = 0 .; iterator it = equipment.iterator (); for (iter.hasnext ()) NetPrice = ((Equipment iter.next ()). Netprice (); return netprice;} public double discountprice () {double discountprice = 0 .; iterator iter = equipment.iterator (); for (ore.hasnext ()) discountprice = ((Equipment iter.next ()). Discountprice (); return discountprice;} // Note Here, here is provided for accessing parts methods within their own combination. // The reason why Disk is not because Disk is a single (primitive) element. Public iterator device () {return equipment.iterator (); {// Reserved Iterator method public boolean hasnext () {Return i

The above CompositeEquipment inherits Equipment, and provides an external access to the objects inside, and overloading the Iterator, Iterator is an interface of Java's Collection, which is an implementation of the Iterator mode.

Let's take a look at the two specific classes of CompositeEquiPment: Cap box CHASSIS and box Cabinet, there can be a lot of things in the box, such as bottom plates, power boxes, hard drive boxes, etc. Inside the box, some small devices can be placed, such as hard drive floppy drives. There is no doubt that both of these are all in the nature of the composition.

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 ( To this, we completed the entire Composite mode architecture.

We can look at the client calls composote code: Cabinet Cabinet = New Cabinet ("Tower"); chassis chassis = new chassis ("pc chassis"); // Put PC Chassis into Tower (put the box into the box Cabinet.add (chassis); // Put a 10GB hard disk to the PC Chassis (put the hard disk into the cart case) CHASSIS.ADD (New Disk "); // Call the NetPrice () method; System.out.println ("NetPrice =" Cabinet.netPrice ()); System.out.Println ("discountprice =" Cabinet.Discountprice ());

The method called above NetPrice () or discountprk (), actually Composite uses iTerator traverses the entire tree structure, finds an object that contains this method and implements call execution.

Composite is a very clever manifestation. In practical applications, if we touch the tree structure, we can try to use this mode.

Take the forum as an example. There are many posts in a version (forum), these posts have original stickers, there is a response to the original stickers, is a typical tree structure, then you can use Composite mode, then we entered JIVE See how it is realized.

JIVE anatomy in Jive, Forthread is the container container container. That is, FORUMTHREAD is similar to our compositeequipment. Its and Messages are as follows: [Thread] | - [Message] | | - [Message] | - [Message] | - [Message]

We see the following code in FORUMTHREAD:

Public interface foruMthread {.... public void addmessage (forummessage newmessage) throws unauthorizedException;

Public void deleteMessage (forummessage message) throws unauthorizedException;

Public iterator messages (); ....

}

Similar to CompositeEquiPment, part-component methods for accessing themselves: Add to delete traversal.

Combined with my other mode analysis, we have basically understood the framework of the Jive Forum system. If you don't understand the design pattern before, you will definitely understand the JIVE source code. You must not understand.

:)

转载请注明原文地址:https://www.9cbs.com/read-132088.html

New Post(0)