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

边栏推荐
- String类学习
- 5分钟包你学会正则表达式
- Tencent WXG internship experience (has offered), I hope it will help you!
- Gram matrix
- Logstash - logstash sends an alarm email to email
- 数据可视化实战:实验报告
- ES6的搭配环境
- The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
- String class learning
- Interface oriented programming
猜你喜欢

Redis underlying data structure

canal部署、原理和使用介绍

423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)

Bingc (inheritance)

Logstash——使用throttle过滤器向钉钉发送预警消息

MySQL 索引底层原理

How to associate wechat applet QR code to realize two code aggregation

如何设计好的技术方案

Getting started with Python

跨域的五种解决方案
随机推荐
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
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)
Household accounting procedures (First Edition)
Bingc (inheritance)
numpy. frombuffer()
302. minimum rectangular BFS with all black pixels
SQL Server 函数
Prometheus和Zabbix的对比
Library management system
"= =" difference from "equals"
The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
SQL Server view
Detailed explanation of serial port communication principle 232, 422, 485
Implementation of third-party wechat authorized login for applet
Logstash - logstash pushes data to redis
Cython入门
Interface oriented programming
06. talk about the difference and coding between -is and = = again
【Spark】Spark SQL 字段血缘如何实现
numpy.log