Design mode Composite (combination)

zhaozj2021-02-16  55

Composite Definition: The object is organized in a tree structure to achieve a "part-overall" hierarchy such that the user has consistency to the use of individual objects and combined 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, Composite will traverse the entire tree structure, find the object that contains this method and implements 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.

Benefits: 1. Use the user call, the user can consistently use a combined structure or a single object, the user does not have to do with a single object or the entire combination structure, which simplifies the user-end code. 2. It is easier to add object components in the combination. The user does not have to change the code because of the new item components.

How to use Composite? First define a interface or abstract category, which is a generic way of design mode. Other design patterns are not limited to the internal definitions of the interface, but composite has a regulation, that is, to define a for access and management within the interior. The objects of the Composite assembly (or the component component ".

The following code is defined in an abstract category, 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;} / / Remove Parts Method PUBLIC Boolean Remove (Equipment Equipment) {Return False;} // Note Here, a component method for accessing the composite category is provided here. Public iterator it () {return null;} public equipment (final string name) {this.name = name;}}

Abstract category 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 an element of a primitive. Public iterator it () {return equipment.iterator (); {// Reserved Iterator method public boolean Hasnext () {returni i

Let's take a look at the two specific categories of CompositeEquit: 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 .; inserts some small equipment, such as hard drive floppy Wait. 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 thismo, we have completed the entire Composite mode architecture.

We can look at the user call composote code: Cabinet Cabinet = New Cabinet ("Tower"); chassis chassis = new chassis ("pc chassis"); // Put PC Chassis into Tower (put the box to the box Cabinet.add (chassis); // Put a 10GB hard disk to PC Chassis (put hard disk into the tray) CHASSIS.ADD (New Disk "); // Call NetPrice () Method; system.out.println ("NetPrice =" Cabinet.Netprice ()); System.out.Println ("discountprice =" Cabinet.Discountprice ());

The method of the above call NetPrice () or discountprice (), 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 (Composite). That is, FORUMTHREAD is similar to CompositeEquipment in our above. It and Messages are as follows: [thread] | - [Message] | - [Message] | - [Message] |

We see the following code in FORUMTHREAD:

public interface ForumThread {.... public void addMessage (ForumMessage parentMessage, ForumMessage newMessage) throws UnauthorizedException; public void deleteMessage (ForumMessage message) throws UnauthorizedException; public Iterator messages (); ....} Similarly CompositeEquipment, for providing access to your Component Ways: Increase Delete Traverse.

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-20610.html

New Post(0)