当前位置:网站首页>Combined mode, transparent mode and secure mode

Combined mode, transparent mode and secure mode

2022-06-26 05:45:00 Green water monster 12138

What is a combination pattern

Portfolio model (composite): Combine objects into a tree structure to represent “ part - whole ” The hierarchy of . Make the use of single object and composite object consistent .

UML chart

 Insert picture description here

Use scenarios for composite patterns

Source blog
1、 If you need to implement a tree object structure , Consider using a combination pattern .
The composite pattern provides you with two basic element types that share a common interface : Simple leaf nodes and complex containers . Containers can contain leaf nodes and other containers . This allows you to build a tree nested recursive object structure .
2、 If you want client code to handle simple and complex elements in the same way , You can use this mode .
All elements defined in the composite schema share the same interface . With the help of this interface , The client doesn't have to care about the specific class of the object it uses .

Small exercise

Implement a commodity tree structure

 Insert picture description here

package composite;

public class test {
    
	public static void main(String[] args) {
    
		incGoods incGoods1=new incGoods(" clothing ");
		incGoods incGoods2=new incGoods(" men's wear ");
		incGoods2.add(new speGoods(" shirt "));
		incGoods2.add(new speGoods(" The jacket "));
		incGoods1.add(incGoods2);
		incGoods2=new incGoods(" Women's wear ");
		incGoods2.add(new speGoods(" skirt "));
		incGoods2.add(new speGoods(" suit "));
		incGoods1.add(incGoods2);
		incGoods1.display(1);
	}
}
package composite;

public abstract class Goods {
    
	protected String name;
	public Goods(String name){
    
		this.name=name;
	}
	public abstract void add(Goods goods);
	public abstract void remove(Goods goods);
	public abstract void display(int depth);
}
package composite;

public class speGoods extends Goods{
    
	public speGoods(String name){
    
		super(name);
	}
	@Override
	public void add(Goods goods) {
    
		// TODO Auto-generated method stub
		System.out.println(" Cannot add item ");
	}

	@Override
	public void remove(Goods goods) {
    
		// TODO Auto-generated method stub
		System.out.println(" Cannot delete item ");
	}

	@Override
	public void display(int depth) {
    
		// TODO Auto-generated method stub
		System.out.println(name+"-"+depth);
	}
	

}
package composite;

import java.util.ArrayList;
import java.util.List;

public class incGoods extends Goods{
    
	List<Goods> goods;
	public incGoods(String name){
    
		super(name);
		goods=new ArrayList<Goods>();
	}
	@Override
	public void add(Goods goods) {
    
		// TODO Auto-generated method stub
		this.goods.add(goods);
	}

	@Override
	public void remove(Goods goods) {
    
		// TODO Auto-generated method stub
		goods.remove(goods);
	}

	@Override
	public void display(int depth) {
    
		// TODO Auto-generated method stub
		System.out.println(name+"-"+depth);
		for(Goods goodss:goods){
    
			goodss.display(depth+1);
		}
	}
	
	
}

Transparent way 、 safe mode

In combination mode UML In the picture we can see ,Ledf Class inherited Component Class add and remove Method , But these two methods are useful for leaf Class does not have to be implemented ( But it must be realized ), This method reduces the judgment of client object processing . It's called Transparent way
safe mode , take add and remove Method in Component Remove from , stay compisite Direct implementation in . In this way, specific judgment must be made during client object processing , for example :
In transparent mode ,
Component a=new Leaf();
Component b=new Composite();
b.add(a);
In a safe way ,
Leaf a=new Leaf();
Composite b=new Composite();
b.add(a);
How to choose , It needs to be based on the specific situation .

The relationship with other design patterns

From this blog

  • Bridging mode 、 Status mode and The strategy pattern ( To some extent, it includes the adapter pattern ) The interface of is very similar . actually , They are all based on composite patterns —— Delegate work to other objects , But they also solved different problems . Patterns are not just recipes for organizing code in a particular way , You can also use them to discuss problems solved by patterns with other developers .

  • You can use... When creating complex composite trees Builder pattern , Because this allows its construction steps to run recursively .

  • The responsibility chain model is often used in conjunction with the composite model . under these circumstances , After the leaf component receives the request , The request can be passed along the chain containing all the parent components to the bottom of the object tree .

  • You can use the iterator pattern to traverse the composite tree .

  • You can use visitor mode to perform operations on the entire composite tree .

  • You can use the shared element mode to realize the shared leaf nodes of the composite tree to save memory .

  • Combination and Decoration mode The structure diagram of is very similar , Because both rely on recursive composition to organize an unlimited number of objects .

    • Decoration is similar to combination , But it has only one sub component . There is another obvious difference : Decoration adds additional responsibilities to the encapsulated object , Only the results of its child nodes are combined “ Sum up ”.

    • however , Patterns can also cooperate with each other : You can use decoration to extend the behavior of specific objects in the composition tree .

  • Designs that make extensive use of composition and decoration often benefit from the use of prototype patterns . You can use this pattern to copy complex structures , Instead of rebuilding from scratch .

原网站

版权声明
本文为[Green water monster 12138]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260536206011.html