当前位置:网站首页>GoF23—原型模式
GoF23—原型模式
2022-06-26 06:03:00 【酷小亚】
原型模式:
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式

代码如下:
package prototype.Demo1;
import java.util.Date;
/** * 1、实现一个接口 Cloneable * 2、重写一个方法 * */
public class Video implements Cloneable {
private String name;
private Date createTime;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Video() {
}
public Video(String name, Date createTime) {
this.name = name;
this.createTime = createTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Video{" +
"name='" + name + '\'' +
", createTime=" + createTime +
'}';
}
}
package prototype.Demo1;
import javax.xml.crypto.Data;
import java.util.Date;
/** * 客户端:克隆 */
public class Bilibli {
public static void main(String[] args) throws CloneNotSupportedException {
//原型对象
Date date = new Date();
Video v1 = new Video("酷小", date);
System.out.println("v1=>"+v1);
System.out.println("v1=>hash"+v1.hashCode());
//让v2克隆v1
Video v2 = (Video) v1.clone();
System.out.println("v2=>"+v2);
System.out.println("v2=>hash"+v2.hashCode());
}
}

边栏推荐
- Five solutions across domains
- 从新东方直播来探究下小程序音视频通话及互动直播
- 5 minutes to learn regular expressions
- 5分钟包你学会正则表达式
- Unicloud cloud development obtains applet user openid
- PyTorch混合精度原理及如何开启该方法
- 去哪儿网BI平台建设演进史
- Spark source code analysis (I): RDD collection data - partition data allocation
- Logstash - logstash pushes data to redis
- Soft power and hard power in program development
猜你喜欢

Basic construction of SSM framework

电商借助小程序技术发力寻找增长突破口

Library management system

Class and object learning
![Operator priority, associativity, and whether to control the evaluation order [detailed explanation]](/img/c3/a646a7c7cb82e00746923f7b023058.jpg)
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]

家庭记账程序(第一版)

通俗易懂的从IDE说起,再谈谈小程序IDE

Redis multithreading and ACL

MySQL-08

421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)
随机推荐
Logstash——使用throttle过滤器向钉钉发送预警消息
Redis multithreading and ACL
Machine learning 05: nonlinear support vector machines
numpy.exp()
EFK昇級到ClickHouse的日志存儲實戰
事务与消息语义
Machine learning 07: Interpretation of PCA and its sklearn source code
Bubble sort
Redis underlying data structure
Detailed explanation of serial port communication principle 232, 422, 485
How to associate wechat applet QR code to realize two code aggregation
Record how to modify the control across threads
302. minimum rectangular BFS with all black pixels
The difference between overload method and override method
Factory method pattern, abstract factory pattern
PyTorch混合精度原理及如何开启该方法
MySQL-08
SQL server functions
kolla-ansible部署openstack yoga版本
Typora activation method