当前位置:网站首页>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 .
边栏推荐
- [performance tuning basics] performance tuning standards
- IDEA Dashboard
- Design of routing service for multi Activity Architecture Design
- 顺序栈遍历二叉树
- 伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
- Docker deploy mysql5.7
- [cann document express issue 05] let you know what operators are
- Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable
- Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
猜你喜欢

16个优秀业务流程管理工具
![[performance tuning basics] performance tuning strategy](/img/83/be41a6a0c5c186d3fb3a120043c53f.jpg)
[performance tuning basics] performance tuning strategy

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

在Dialog中使用透明的【X】叉叉按钮图片

Bytebase joins Alibaba cloud polardb open source database community

Basic concepts and definitions of Graphs

Grating diffraction

Nodered has no return value after successfully inserting into the database (the request cannot be ended)

建立自己的网站(14)

JMeter environment deployment
随机推荐
【建议收藏】时间序列预测应用、paper汇总
[cann document express issue 04] unveiling the development of shengteng cann operator
伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
Bytebase joins Alibaba cloud polardb open source database community
C语言实现扫雷(简易版)
Image panr
得物多活架构设计之路由服务设计
年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
天天鉴宝暴雷背后:拖欠数千万、APP停摆,创始人预谋跑路?
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
Sequential stack traversal binary tree
图像PANR
Nodered has no return value after successfully inserting into the database (the request cannot be ended)
对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿
宅男救不了元宇宙
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI
DX12引擎开发课程进度-这个课程到底讲到哪里了