当前位置:网站首页>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 .
边栏推荐
- Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
- Consul服务注册与发现
- There are applications related to web network request API in MATLAB (under update)
- pytorch(网络模型训练)
- 电机专用MCU芯片LCM32F037系列内容介绍
- Owasp-top10 in 2021
- Thinking about bad money expelling good money
- 423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
- 5分钟包你学会正则表达式
- 从新东方直播来探究下小程序音视频通话及互动直播
猜你喜欢

Uni app ceiling fixed style
Posting - don't get lost in the ocean of Technology

Leetcode114. 二叉树展开为链表

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

There are applications related to web network request API in MATLAB (under update)

AutowiredAnnotationBeanPostProcessor什么时候被实例化的?

How Navicat reuses the current connection information to another computer

【C语言】深度剖析数据在内存中的存储
![[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022](/img/64/5b2aec7a26c64c104c86e200f83b2d.png)
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022

【ARM】在NUC977上搭建基于boa的嵌入式web服务器
随机推荐
Bingc (inheritance)
[upsampling method opencv interpolation]
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
String类学习
Life is so fragile
LeetCode_ Binary search tree_ Simple_ 108. convert an ordered array to a binary search tree
Ribbon负载均衡服务调用
About abstact and virtual
[MySQL] MySQL million level data paging query method and its optimization
状态模式,身随心变
12 multithreading
无线网络存在的安全问题及现代化解决方案
Internship May 29, 2019
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
The use of loops in SQL syntax
Thinking about bad money expelling good money
Something about MariaDB
旧情书
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
When was the autowiredannotationbeanpostprocessor instantiated?