当前位置:网站首页>Prototype mode -- clone monster Army
Prototype mode -- clone monster Army
2022-06-24 20:34:00 【zhanyd】
List of articles
Introduction
Xiaoshuai works for a game company , Participate in the development of one RPG game , He is responsible for designing monsters in the game . Some big scenes need hundreds of monsters , If you use new To create each monster , There are many parameters that need to be initialized , It's going to take a lot of time , And it's troublesome .
Xiaoshuai decides to quickly clone monsters in prototype mode , Let the monster army quickly gather .
Archetypal model
Archetypal model : Using prototype instances to specify the kind of objects to create , And create new objects by copying these stereotypes .

Prototype is a creative design pattern , Enables you to copy objects , Even complex objects , And you don't have to make your code depend on the class they belong to .
Corresponding to our code , The class diagram is as follows :
Monsters :
/** * Monsters */
public class Monster implements Cloneable{
/** * name */
String name;
/** * aggressivity */
int attackPower;
/** * Health value */
int hp;
public Monster(String name, int attackPower, int hp) {
this.name = name;
this.attackPower = attackPower;
this.hp = hp;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return " Monster name :" + name + ", aggressivity :" + attackPower + ", Health value :" + hp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
}
Client class :
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
List<Monster> monsterList = new ArrayList<Monster>();
Monster monster = new Monster(" Flying dragon ", 200, 100);
for(int i = 0; i < 10; i++) {
monsterList.add((Monster)monster.clone());
}
monsterList.stream().forEach(f -> System.out.println(f));
}
}
Output :
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Monster name : Flying dragon , aggressivity :200, Health value :100
Notice the Monster Is to achieve Cloneable Interface to use clone() Method , If you put Cloneable If the interface is removed, an error will be reported :
Exception in thread "main" java.lang.CloneNotSupportedException: prototype.monster.normal.Monster
at java.lang.Object.clone(Native Method)
at prototype.monster.normal.Monster.clone(Monster.java:31)
at prototype.monster.normal.Client.main(Client.java:13)
Object Class clone() The method has been described :
Shallow copy and deep copy
If every monster has its own pet , Pets have their own names and skills , Let's take a look at the following example .
Shallow copy
Monsters :
/** * Monsters */
public class Monster implements Cloneable{
/** * name */
String name;
/** * aggressivity */
int attackPower;
/** * Health value */
int hp;
/** * Pets */
Pet pet;
public Monster(String name, int attackPower, int hp, Pet pet) {
this.name = name;
this.attackPower = attackPower;
this.hp = hp;
this.pet = pet;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return " Monster name :" + name + ", aggressivity :" + attackPower + ", Health value :" + hp + ", Pet name :" + pet.name + ", Skill :" + pet.skill;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
}
Pet class :
/** * Pet class */
public class Pet {
/** * name */
String name;
/** * Skill */
String skill;
public Pet(String name, String skill) {
this.name = name;
this.skill = skill;
}
@Override
public String toString() {
return " Pet name :" + name + ", Skill :" + skill;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
}
Client class :
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
// Pets
Pet pet = new Pet(" Little stone man ", " Flying stone ");
// Monster
Monster monster = new Monster(" Mountain Giant ", 300, 500, pet);
// Monster copy
Monster monsterClone = (Monster)monster.clone();
System.out.println("monster :" + monster);
System.out.println("monsterClone :" + monsterClone);
System.out.println("----------------------------------------------------------------------------------------------");
// Just modify the pet attribute of the monster copy
monsterClone.pet.setName(" The camellia ");
monsterClone.pet.setSkill(" Dive ");
System.out.println("monster :" + monster);
System.out.println("monsterClone :" + monsterClone);
}
}
Output :
monster : Monster name : Mountain Giant , aggressivity :300, Health value :500, Pet name : Little stone man , Skill : Flying stone
monsterClone : Monster name : Mountain Giant , aggressivity :300, Health value :500, Pet name : Little stone man , Skill : Flying stone
----------------------------------------------------------------------------------------------
monster : Monster name : Mountain Giant , aggressivity :300, Health value :500, Pet name : The camellia , Skill : Dive
monsterClone : Monster name : Mountain Giant , aggressivity :300, Health value :500, Pet name : The camellia , Skill : Dive
As you can see from the above example , The copied monster object monsterClone Modified your pet pet Properties of , Also changed the pet attribute of the prototype monster .
This is because in the Java In language ,Object Class clone() Method executes the shallow copy of the above . It only copies the data of the basic data type in the object ( such as ,int、long), And reference objects (pet) Memory address of , The reference object itself is not copied recursively .
therefore ,monster and monsterClone Object refers to the same pet object .
Deep copy
Let's take a look at the example of deep copy :
Pet class :
Client class :
Output :
You can see , The copied monster just modified its pet , The pet of the prototype monster has not changed .
Deep copy is to copy all the objects in the object one by one , All data in each object has an independent copy .
The following two figures describe the difference between light copy and deep copy :

Another way to implement deep copy is serialization and deserialization :
public Object deepCopy(Object object) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(object);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return oi.readObject();
}
summary
If the creation cost of an object is large , There is little difference between different objects of the same class ( Most of the fields are the same ), under these circumstances , We can take advantage of existing objects ( Prototype ) replicate ( Copy 、 clone ) The way , To create new objects , In order to save creation time .
The prototype pattern is to copy objects at the memory binary level , More than direct new The performance of an object is much better , Especially when a large number of objects are generated in a cycle , Prototype patterns can be more efficient .
This is the prototype pattern , Is it simple ?
Last , Let's look at the advantages and disadvantages of the prototype pattern :
advantage
- You can clone objects , Without coupling with the specific class to which they belong .
- Pre generated prototypes can be cloned , Avoid running initialization code repeatedly .
- It is more convenient to generate complex objects .
- Different configurations of complex objects can be handled in ways other than inheritance .
shortcoming
- Cloning complex objects that contain circular references can be cumbersome .
边栏推荐
- 16 excellent business process management tools
- 用手机摄像头就能捕捉指纹?!准确度堪比签字画押,专家:你们在加剧歧视
- [multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
- Accurate calculation of task progress bar of lol mobile game
- C语言实现扫雷(简易版)
- [suggested collection] time series prediction application and paper summary
- [cann document express issue 05] let you know what operators are
- 京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
- 苹果、微软、谷歌不再掐架,今年要合力干一件大事
- [performance tuning basics] performance tuning standards
猜你喜欢

基于QT+MySQL的相机租赁管理系统

JMeter environment deployment

VMware virtual machine setting static IP

When querying the database with Gorm, reflect: reflect flag. mustBeAssignable using unaddressable value

Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!

Mapstacks: data normalization and layered color layer loading

Bytebase joins Alibaba cloud polardb open source database community

天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?

Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year

Bean lifecycle flowchart
随机推荐
lol手游之任务进度条精准计算
网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”
得物多活架构设计之路由服务设计
[performance tuning basics] performance tuning standards
Open programmable infrastructure (OPI) project, redefining dpu/ipu
C langage pour le déminage (version simplifiée)
Mapstacks: data normalization and layered color layer loading
Bytebase加入阿里云PolarDB开源数据库社区
Get to know the data structure of redis - hash
VMware virtual machine setting static IP
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
情绪识别AI竟「心怀鬼胎」,微软决定封杀它!
gateway
maptalks:数据归一化处理与分层设色图层加载
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
物联网?快来看 Arduino 上云啦
顺序栈1.0版本
C language to realize mine sweeping (simple version)
Showcase是什么?Showcase需要注意什么?
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?