当前位置:网站首页>Prototype mode, Baa Baa
Prototype mode, Baa Baa
2022-06-26 05:45:00 【Green water monster 12138】
What is a prototype pattern
Archetypal model (prototype): Using prototype instances to specify the kind of objects to create , And create new objects by copying these stereotypes
UML chart

Example code
@Override
public Prototype clone() {
Prototype clone=null;
try {
clone = (Prototype) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
public class ConcretePrototype1 extends Prototype{
public ConcretePrototype1(String id) {
super(id);
}
@Override
public Prototype clone() {
return super.clone();
}
}
public class ConcretePrototype extends Prototype {
public ConcretePrototype(String id) {
super(id);
}
@Override
public Prototype clone() {
return super.clone();
}
}
It is worth noting that in java Use in clone Methods must implement Clonable Interface , Otherwise it will throw CloneNotSupportedException.
At the same time, we should pay attention to the difference between deep copy and shallow copy , Both shallow and deep copies will open up a space for us to store objects , Shallow copies replicate data of the basic data type , For data of object data type, just copy its pointer , This kind of copy clone Function can help us complete . Deep copy requires us to create our own space to store object data types .
The following is from this website
Use scenario of prototype mode
If you need to copy some objects , At the same time, you want the code to be independent of the specific class to which these objects belong , Prototype patterns can be used .
- This consideration usually occurs when the code needs to deal with objects passed by third-party code through the interface . Even without considering code coupling , Nor can your code rely on the specific classes to which these objects belong , Because you don't know their specific information .
- The prototype pattern provides a common interface for client code , The client code can interact with all the cloned objects through this interface , It also makes the client code independent of its cloned object concrete classes .
If subclasses differ only in how their objects are initialized , Then you can use this pattern to reduce the number of subclasses . Others may create these subclasses to create specific types of objects .
- In prototype mode , You can use a series of pre generated 、 Various types of objects as prototypes .
The client does not have to instantiate subclasses according to requirements , Just find the right prototype and clone it .
- In prototype mode , You can use a series of pre generated 、 Various types of objects as prototypes .
Relationship with other design patterns
In the early stages of many design work Factory method Pattern ( It's simpler , And it's easier to customize through subclasses ), Then evolved to use Abstract factory Pattern 、 Prototype mode or builder Pattern ( More flexible but more complex ).
Abstract factory patterns are usually based on a set of factory methods , But you can also use prototype patterns to generate methods for these classes .
Prototypes can be used to keep a history of command patterns .
A lot of use Portfolio model and Decoration mode The design of often benefits from the use of prototypes . You can use this pattern to copy complex structures , Instead of rebuilding from scratch .
Sometimes prototypes can be used as a simplified version of the memo pattern , The condition is that the state of the object you need to store in the history is relatively simple , No need to link to other external resources , Or links can be easily rebuilt .
Abstract factory 、 Both the builder and the prototype can be implemented in singleton mode .
边栏推荐
- Old love letters
- ZigBee learning in simple terms Lecture 1
- Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports
- String类学习
- Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
- ZigBee explain in simple terms lesson 2 hardware related and IO operation
- Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
- RIA想法
- FindControl的源代码
- Talk 5 wireless communication
猜你喜欢

Leetcode114. 二叉树展开为链表

MySQL database-01 database overview

【C語言】深度剖析數據在內存中的存儲

REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)

Overloading and overriding

Redis discovery bloom filter

【 langage c】 stockage des données d'analyse approfondie en mémoire
![[arm] build boa based embedded web server on nuc977](/img/fb/7dc1898e35ed78b417770216b05286.png)
[arm] build boa based embedded web server on nuc977

Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal

Uni app ceiling fixed style
随机推荐
[C language] deep analysis of data storage in memory
MySQL source code reading (II) login connection debugging
【C語言】深度剖析數據在內存中的存儲
pytorch(网络模型训练)
[MySQL] MySQL million level data paging query method and its optimization
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
[arm] add desktop application for buildreoot of rk3568 development board
Pre-Sale Analysis
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
There are applications related to web network request API in MATLAB (under update)
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
Introduction to lcm32f037 series of MCU chip for motor
项目中止
The news of thunderbolt
Bingc (inheritance)
9 common classes
ZigBee learning in simple terms lesson 3 external interruption
Introduction to GUI programming to game practice (II)
The use of loops in SQL syntax