Design Mode Combination Mode new

Source: Internet
Author: User

 

Combination Mode: allows you to combine objects into a tree structure to express the "whole/part" hierarchy. The combination allows the customer to process individual objects and object combinations in a consistent manner.

The combination mode solves the problem of overall and partial consistency.

Transparent mode: The leaf node and the branch node inherit from the same parent class, and the leaf and branch have the same interface. Advantage: the leaf node and the branch node have no difference to the outside world and can be called through a unified interface. disadvantage: The implementation of the parent class method in the leaf is meaningless.

Security method: the leaf interface does not implement the parent method. This method is transparent to the outside, and the judgment logic needs to be added during use. Not recommended.

Class diagram:

Case code:

Behavior method. java

 

Package combination mode; import java. util. arrayList; public abstract class behavior class {protected ArrayList <behavior class> mList = new ArrayList <behavior class> (); protected String name; public behavior class (String name) {this. name = name;} public abstract void add (behavior class component); public abstract void remove (behavior class component); public abstract void Display (String subTab );}

Branch class. java

 

 

Package combination mode; public class branch class extends behavior class {public branch class (String name) {super (name) ;}@ Overridepublic void add (behavior class component) {mList. add (component) ;}@ Overridepublic void remove (behavior class component) {mList. remove (component) ;}@ Overridepublic void Display (String subTab) {System. out. println (subTab + '+ name +'); for (behavior class B: mList) {B. display (subTab + -);}}}
Leaf class. java

 

 

Package combination mode; public class leaf class extends behavior class {public leaf class (String name) {super (name) ;}@ Overridepublic void add (behavior class component) {}@ Overridepublic void remove (behavior class component) {}@ Overridepublic void Display (String subTab) {System. out. println (subTab + '+ name + ');}}

Combined Mode Test

 

 

Package combination mode; /***** leaf a leaf B */* | * branch A branch B */* | * I am a big tree * @ author wanghl **/public class combination mode {public static void main (String args []) {branch tree = new branch class (I am A big tree); branch A = new branch class (branch A); branch B = new branch class (Branch B ); big tree. add (Branches A); big tree. add (branch B); leaf A1a = new leaf class (branch A1 leaf a); leaf class leaf A1b = new leaf class (branch A1 leaf B ); branch. add (leaf A1a); Branches. add (leaf A1b); big tree. display (-); System. out. println (-------------------------------------); branch type jujube tree = new branch class (I am a jujube tree); branch type branches = new branch class (jujube tree branches ); leaf jujube 1 = new leaf (jujube 1); leaf jujube 2 = new leaf (jujube 2); leaf jujube 3 = new leaf (jujube 3 ); branches. add (jujube 1); branches. add (jujube 2); branches. add (jujube 3); jujube tree. add (branches); jujube tree. display (-);}}

Print Output:
-'I am A big tree' -- 'Branch a '--- 'Branch A1 leaf a' --- 'Branch A1 leaf B' -- 'Branch B '------------------------------------------------' I am A big jujube tree '--' jujube Branches --- 'jujube 1' --- 'jujube 2' --- 'jujube 3'

 

 

Summary:

In the combination mode, you do not need to care about whether it is a leaf node or a branch node. You do not need to use a judgment statement for a combination of definitions.

 

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.