Combination of design patterns

Source: Internet
Author: User
1. Why use the combined design mode?

Sometimes we encounter a hierarchical relationship between objects (note that this refers to the object), as shown in figure

So, do we need to write classes for each object separately? Of course there is no need, so this is actually a tree-type object structure. The composite design pattern is created for this structure.

Leaf and composite are derived from component, which are called Leaf nodes and supporting nodes respectively. Leaf nodes do not have the ability to add or remove nodes.

2. What is a combination design model?

The combination design mode combines objects into a tree structure to represent the "part-whole" Relationship

3. How to Use the combination design mode

UML class diagram

The rudder master serves as the support node here, and the Left and Right Guards act as the leaf nodes.

Specific Code:

Member. Interface

Public interface member {public void add member (member C); Public void remove member C); Public void show member function ();}

Rudder master. Class

Members of implements of the public class rudder master {public final static int total rudder master = 0; public final static int points rudder master = 1; private string name; arraylist <member> List = new arraylist <member> (); Public rudder master (INT type) throws exception {If (type <0 | type> 1) {Throw new exception ("type error");} switch (type) {Case rudder master. MASTER: name = "master"; break; Case master. sub-Pilot: Name = "sub-pilot"; break; default: break; }}@ overridepublic void add member (member c) {// todo auto-generated Method s Tublist. add (c) ;}@ overridepublic void reduce Member (member c) {// todo auto-generated method stublist. remove (c) ;}@ overridepublic void display member function () {// todo auto-generated method stubsystem. out. print ("I am" + name + "\ n"); For (member C: List) {If (! (C instanceof) {C. Show member functions () ;}} system. Out. Print ("--------------" + "\ n ");}}

Left protection method. Class

Public class left guide implements member {// leaf node cannot be added or removed @ overridepublic void (member c) {// todo auto-generated method stubsystem. out. print ("the protection method cannot add \ n");} // The leaf node cannot add or remove the node @ overridepublic void (member C) {// todo auto-generated method stubsystem. out. print ("protection method cannot reduce Member \ n");} @ overridepublic void display member function () {// todo auto-generated method stubsystem. out. print ("Left guard protects the rudder \ n ");}}

Right Protection Method. Class

Public class right protection implements member {// leaf node cannot be added or removed @ overridepublic void (member c) {// todo auto-generated method stubsystem. out. print ("the protection method cannot add \ n");} // The leaf node cannot add or remove the node @ overridepublic void (member C) {// todo auto-generated method stubsystem. out. print ("protection method cannot reduce Member \ n");} @ overridepublic void display member function () {// todo auto-generated method stubsystem. out. print ("right shield protects the rudder \ n ");}}

Main. Class

Public class main {public static void main (string [] ARGs) throws exception {// todo auto-generated method stub = new new (. total rudder master); rudder master points rudder master 1 = new rudder master (rudder master. points the rudder master); the rudder master points the rudder master 2 = the new rudder master (the rudder master. divided by the rudder master); the total rudder master. add members (divided by rudder master 1); Total rudder master. add members (New Left guard (); Total rudder owners. add members (New Right Protection Method (); submitter 1. add members (sub-rudder master 2); sub-rudder master 1. add members (New Left guard (); Subscribe to rudder owner 1. add members (New Right Protection Method (); divide the rudder master 2. add members (New Left guard (); distribute members to the rudder owner. add members (New Right Guard (); Total rudder owners. show member functions (); submitter 1. show member functions (); divided by the rudder master 2. show member functions ();}}

Final output

I am the master of the ship.
Left guard protects the rudder
Right shield protects the rudder
--------------
I am the submitter
Left guard protects the rudder
Right shield protects the rudder
--------------
I am the submitter
Left guard protects the rudder
Right shield protects the rudder
--------------

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.