当前位置:网站首页>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

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

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
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 .
边栏推荐
- Red team scoring method statistics
- Navicat如何将当前连接信息复用另一台电脑
- 【 langage c】 stockage des données d'analyse approfondie en mémoire
- 原型模式,咩咩乱叫
- Overloading and overriding
- Leetcode513.找出树的左下角的值
- CMakeLists. txt Template
- SDN based DDoS attack mitigation
- Lesson 4 serial port and clock
- cross entropy loss = log softmax + nll loss
猜你喜欢

kolla-ansible部署openstack yoga版本
![[red team] what preparations should be made to join the red team?](/img/03/f246f18f8925167dbd5e9d63912faa.png)
[red team] what preparations should be made to join the red team?
转帖——不要迷失在技术的海洋中

423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

12 multithreading

Leetcode513.找出树的左下角的值

怎么把平板作为电脑的第二扩展屏幕

421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
![[arm] add desktop application for buildreoot of rk3568 development board](/img/9a/28015cdea7362261c39ffc7f6e13a9.png)
[arm] add desktop application for buildreoot of rk3568 development board

原型模式,咩咩乱叫
随机推荐
There are applications related to web network request API in MATLAB (under update)
FindControl的源代码
pytorch(环境、tensorboard、transforms、torchvision、dataloader)
Thinking about bad money expelling good money
RIA想法
Cyclic displacement
电机专用MCU芯片LCM32F037系列内容介绍
kolla-ansible部署openstack yoga版本
小小面试题之GET和POST的区别
The most refined language interprets the event dispatcher (also known as the event scheduler)
旧情书
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
Sql语法中循环的使用
How Navicat reuses the current connection information to another computer
10 set
Command line interface of alluxio
工厂方法模式、抽象工厂模式
What management systems (Updates) for things like this
Old love letters
Internship May 29, 2019