The combination mode __java of JAVA design pattern

Source: Internet
Author: User
Tags stub

Let's take a look at the definition of a combination pattern: " group objects into a tree structure to represent a ' partial-whole ' hierarchy." Combined mode enables users to have consistency in the use of individual objects and grouped objects. "

Let's take a haircut to analyze the issue of the card.

First of all, a card can be used in Headquarters, branch stores, franchise stores, then the headquarters can swipe card, the branch can also swipe card, franchise stores can also swipe, this attribute structure of the store-level relationship is clear.

Then, the head Office card consumption and branch card consumption is the same reason, then the head office and the branch card use also has consistency.

1. Examples of combinatorial patterns

Then the example of a combination pattern is as follows:

Import java.util.ArrayList;

Import java.util.List;

		public class Componentdemo {public abstract class Component {String name;

		public abstract void Add (Component c);

		public abstract void Remove (Component c);
	public abstract void Eachchild ();
			}//Composite part class public class Leaf extends Component {//leaf node does not have the ability to add, so do not implement @Override public void Add (Component c) {
		TODO auto-generated Method Stub System.out.println (""); }//leaf node does not have the ability to add necessarily also cannot delete @Override public void remove (Component c) {//TODO auto-generated method Stub syste
		M.out.println (""); The//leaf node has no child nodes so display its own execution results @Override public void Eachchild () {//TODO auto-generated method stub SYSTEM.OUT.P
		RINTLN (name + "executed"); }///group class public class composite extends Component {//Used to save node's subnodes list<component> List = new ARRAYLIST&L T

		Component> ();
		Add node add part @Override public void Add (Component c) {//TODO auto-generated Method stub List.add (c); }

		//Delete node deletion part @Override public void remove (Component c) {//TODO auto-generated Method stub list.remove (c); }//Traverse child node @Override public void Eachchild () {//TODO auto-generated Method Stub System.out.println (name +)
			Carried out ");
			for (Component c:list) {c.eachchild ();
		}} public static void Main (string[] args) {Componentdemo demo = new Componentdemo ();
		Construct root node Composite rootcomposite = Demo.new composite ();

		Rootcomposite.name = "root node";
		Left node Composite compositeleft = Demo.new composite ();

		Compositeleft.name = "Left node";
		Build right node, add two leaves several points, namely subassembly composite compositeright = demo.new composite ();
		Compositeright.name = "Right node";
		Leaf leaf1 = demo.new leaf ();
		Leaf1.name = "Right-sub node 1";
		Leaf leaf2 = demo.new leaf ();
		Leaf2.name = "Right-sub node 2";
		Compositeright.add (LEAF1);

		Compositeright.add (LEAF2);
		The left and right nodes join the root node Rootcomposite.add (compositeright);
		Rootcomposite.add (Compositeleft);
	Traversing the Assembly component Rootcomposite.eachchild ();
 }
}


The results of the implementation are as follows:

2. Apply the combination mode of membership card consumption

Then we will according to our membership card consumption, to simulate the combination of the implementation of the model. Let ' s go.

First of all:

1. Our parts have, head office, branch stores, franchise stores.

2. Common behavior of our parts is: Brush membership card

3. The level of the relationship between the components, that is, the level of store relationship is, the head office under a branch, branch stores can have a franchise.

With our several necessary conditions, my request is the current store activities when I swipe card in the head office, you can accumulate equivalent to all the subordinate store credit card points total, the design code is as follows:

Import java.util.ArrayList;

Import java.util.List;

		public class Paydemo {public abstract class Market {String name;

		public abstract void Add (Market m);

		public abstract void Remove (Market m);
	public abstract void Paybycard (); //branch below can be joined store public class Marketbranch extends Market {//Franchise list list<market> lists = new Arraylist<pay

		Demo.market> ();
		Public Marketbranch (String s) {this.name = s;
		@Override public void Add (Market m) {//TODO auto-generated method stub list.add (m);
		@Override public void Remove (Market m) {//TODO auto-generated method stub list.remove (m); After the purchase, the store under the franchise automatically accumulated integral @Override public void Paybycard () {//TODO auto-generated method stub System.out.pr
			INTLN (name + "consumption, points have been tired to join the membership card");
			for (Market m:list) {m.paybycard ();  There are no outlets and franchises below the affiliate store, the lowest public class Marketjoin extends Market {public marketjoin (String s) {this.name =

		S } @Override Public void Add (Market m) {//Todo auto-generated method stub} @Override public void Remove (Market m) {//TODO auto-generated method stub} @Override public void Paybycard () {//TODO auto-generated method stub SYSTEM.O
		UT.PRINTLN (name + "consumption, points have been tired to join the membership card");
		
		} public static void Main (string[] args) {Paydemo demo = new Paydemo ();
		Marketbranch rootbranch = demo.new marketbranch ("head Office");
		Marketbranch qhdbranch = demo.new marketbranch ("Qinhuangdao branch");
		Marketjoin Hgqjoin = demo.new marketjoin ("Qinhuangdao branch one harbor District franchise Shop");
		
		Marketjoin Btljoin = demo.new marketjoin ("Qinhuangdao branch two baitaling franchise shop");
		Qhdbranch.add (Hgqjoin);
		Qhdbranch.add (Btljoin);
		Rootbranch.add (Qhdbranch);
	Rootbranch.paybycard ();
 }
}


The results of the operation are as follows:

In this way in the accumulation of all sub-store points, you do not 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 the head Office card, or franchise card, can be correctly and effectively calculate the activity points.

3. Under what circumstances use the combination mode

Quote: " 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 mode when using all the objects in the composite structure uniformly." "

Author: jason0539

Micro Blog: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Related Article

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.