Java design Mode _ (structure) _ Combination mode __java

Source: Internet
Author: User

Quote Encyclopedia

Combination mode, which combines objects into a tree structure to represent a "partial-whole" hierarchy, which makes the user consistent in the use of individual objects and composite objects. The key to mastering the combinatorial pattern is to understand the meaning of "part/whole" as well as "single object" and "combination object". Sometimes called the partial-whole pattern, which makes our tree-structured problems obscure the concept of simple elements and complex elements, client programs can handle complex elements like simple elements, thus decoupling the client program from the internal structure of complex elements.
Involved Roles 1.Component is the object declaration interface in a combination that, when appropriate, implements the default behavior of all classes common interfaces. Declares an interface for accessing and managing the component child parts. 2.Leaf represents the leaf node object in the combination, the leaf node does not have the child knot point. 3.Composite defines the behavior of a side point, which is used to store the child parts, and to implement operations related to the child parts in the component interface, such as add (add) and delete (remove), etc. Applicability under the following conditions of the combination mode: 1. You want to represent the part of the object-the overall hierarchy 2. You want users to ignore the difference between a grouped object and a single object, and the user will uniformly use all of the objects in the composite structure.


such as: There is a demand for a supermarket chain user membership card integral calculation, chain supermarkets that in addition to the head office there are all over the branch, users in any store to pay consumption will be the resulting points applied to all supermarket stores.



Concrete Implementation



Related code:

1. Define component abstract class Marketcommon

The supermarket membership card integral abstract class public abstract class
Marketcommon {
	private String name;
	Integral
	private long cardscore;
	
	public void Add (Marketcommon m) {} public void-
	remove (Marketcommon m) {} public
	void Pay () {} public
	
	String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public long Getcardscore () {return
		cardscore;
	}
	public void Setcardscore (long cardscore) {
		this.cardscore = Cardscore;
	}
}

2. Create Concrete implementation

public class Marketbranchscore extends Marketcommon {public
	marketbranchscore (String name) {
		Super.setname ( name);
	Record each branch information
	private list<marketcommon> branchlist = new arraylist<marketcommon> ();
	
	public void Add (Marketcommon m) {
		branchlist.add (m);
	}

	public void Remove (Marketcommon m) {
		branchlist.remove (m);
	}

	public void Pay () {for
		(Marketcommon card:branchlist) {
			System.out.println (card.getname () + "Consumer Cumulative integral:" + Card.getcardscore ());}}


3, the client string connection test

public class Client {

	private static void Pay () {
		//Create head office (one of the head office is also a branch)
		marketbranchscore leadermark = new Market Branchscore ("head Office");
		Create Branch
		Marketbranchscore gzmark = new Marketbranchscore ("Guangzhou branch");
		Marketbranchscore Shmark = new Marketbranchscore ("Shanghai Branch");
		Consumption
		Gzmark.setcardscore (m);
		Shmark.setcardscore ();

		Leadermark.add (Gzmark);
		Leadermark.add (Shmark);
		Credit card payment in each branch accumulated integral
		leadermark.pay ();
	}

	public static void Main (string[] args) {
		pay ();
	}
}

Through the above code simple implementation of the combination mode of application, running output

Guangzhou branch consumption cumulative score: 100
Shanghai Branch consumption cumulative score: 500


So when all the sub store points are accumulated, there is no need to care about the number of stores, also do not have the relationship between the leaf node or the combination of nodes, that is, whether it is the head office consumption, or branch store consumption, that is: the head office and all the combination of stores as a whole, can be correctly and effectively calculate the activity points


Conclusion:

"When you discover that requirements are part and whole hierarchies, and you want users to be able to ignore the differences between the grouped objects and the individual objects, you should consider the combination pattern for implementation when you use all the objects in the composite structure uniformly."



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.