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

边栏推荐
猜你喜欢

Introduction to canal deployment, principle and use

kolla-ansible部署openstack yoga版本

Logstash——使用throttle过滤器向钉钉发送预警消息
![Selective search for object recognition paper notes [image object segmentation]](/img/cf/d3b08d41083f37c164b26a96b989c9.png)
Selective search for object recognition paper notes [image object segmentation]

Tortoise and rabbit race example

消息队列-消息事务管理对比

University Information Management System

MySQL database-01 database overview

Class and object learning

Easy to understand from the IDE, and then talk about the applet IDE
随机推荐
numpy. frombuffer()
Machine learning 07: Interpretation of PCA and its sklearn source code
PyTorch使用多GPU并行训练及其原理和注意事项
SQL Server视图
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
"= =" difference from "equals"
ByteDance starts the employee's sudden wealth plan and buys back options with a large amount of money. Some people can earn up to 175%
Bingc (inheritance)
Soft power and hard power in program development
实时数仓方案如何选型和构建
The difference between overload method and override method
kolla-ansible部署openstack yoga版本
Tencent WXG internship experience (has offered), I hope it will help you!
NPM private server problem of peanut shell intranet penetration mapping
numpy.tile()
Handwritten background management framework template (I)
数据治理工作的几种推进套路
通俗易懂的从IDE说起,再谈谈小程序IDE
从新东方直播来探究下小程序音视频通话及互动直播
06. talk about the difference and coding between -is and = = again