Design Patterns combined mode

Combined mode
    the model objects into a hierarchy tree that represents - Relations "integral part", and
    gives the user a consistent accessibility to the individual objects and composite objects.

The most critical areas to achieve a combination of modes are:

    simple objects and complex objects must implement the same interface.
    This is combined mode can be a combination of simple objects and objects consistent treatment of the cause.



Abstract component (Component) role:

its main role is as a member of leaves and twigs members declared public interface, and to achieve their default behavior.
Leaf member (Leaf) role:
is a leaf node objects in the group, it does not have child nodes that may enable a common interface abstraction component role in the declaration.
Branch member (Composite) Role:
is a branch node objects in the group, it has child nodes.
It implements the interface declared abstract member role, its main role is to store and manage sub-components,
typically comprising Add (), Remove (), GetChild () method and the like.

1. transparent way:
      In this embodiment, since the abstract component declares all the methods in all subclasses , 
so the client need not distinguish leaves objects and container objects, the client is transparent.
But its disadvantages are: leaf members had no Add (), Remove () and GetChild () method,
have to implement them (empty implementation or throw an exception), it will bring some security issues.
 1 import java.util.ArrayList;
 2 
 3 public class Composit {
 4     public static void main(String[] args) {
 5         Composite one = new Composite();
 6         Composite two = new Composite();
 7         one.add(two);
 8         Leaf aa = new Leaf("夏明");
 9         Leaf bb = new Leaf("小红");
10         Leaf cc = new Leaf("李华");
11         one.add(aa);
12         two.add(bb);
13         two.add(cc);
14         one.operation();
15         two.operation();
16     }
17 }
18 
19 interface Component {
20     public void add(Component c);
21 
22     public void remove(Component c);
23 
24     public Component getChild(int i);
25 
26     public void operation();
27 }
28 
29 class Composite implements Component {
30 
31     private ArrayList<Component> st = new ArrayList<>();
32 
33     @Override
34     public void add(Component c) {
35         st.add(c);
36     }
37 
38     @Override
39     public void remove(Component c) {
40         st.remove(c);
41     }
42 
43     @Override
44     public Component getChild(int i) {
45         return st.get(i);
46     }
47 
48     @Override
49     public void operation() {
50         for (int i = 0; i < st.size(); i++) {
51             Component component = st.get(i);
52             component.operation();
53         }
54     }
55 }
56 
57 class Leaf implements Component {
58 
59     private String name;
60 
61     public Leaf(String name) {
62         this.name = name;
63     }
64 
65     @Override
66     public void add(Component c) {
67     }
68 
69     @Override
70     public void remove(Component c) {
71     }
72 
73     @Override
74     public Component getChild(int i) {
75         return null;
76     }
77 
78     @Override
79     public void operation() {
80         System.out.println(name);
81     }
82 }

2. Security Mode formula composition
  not declared Add and Remove the Component methods,

  So subclass Leaf will not need to implement it, but to declare all the methods used to manage sub-class objects in Composit.

 

Combination patterns of usage scenarios

  In the following cases should consider using a combination of modes:

    1. As part we would like to express the object - when the hierarchy as a whole.
    2. Different users want to ignore the combination of objects with a single object, the user will be unified access to all objects in the composite structure while.
 

Guess you like

Origin www.cnblogs.com/loveer/p/11279099.html