当前位置:网站首页>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 .
边栏推荐
- Openvino2022 dev tools installation and use
- [cloud resident co creation] ModelBox draws your own painting across the air
- 【CANN文档速递06期】初识TBE DSL算子开发
- Simulation lottery and probability statistics experiment of the top 16 Champions League
- 苹果不差钱,但做内容“没底气”
- [cann document express issue 04] unveiling the development of shengteng cann operator
- Where is 5g really powerful? What is the difference with 4G?
- Leetcode(135)——分发糖果
- 基于QT+MySQL的相机租赁管理系统
- 物聯網?快來看 Arduino 上雲啦
猜你喜欢

大一女生废话编程爆火!懂不懂编程的看完都拴Q了

Bytebase 加入阿裏雲 PolarDB 開源數據庫社區

对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿

The Network Security Review Office launched a network security review on HowNet, saying that it "has a large amount of important data and sensitive information"

Berkeley, MIT, Cambridge, deepmind and other industry leaders' online lectures: towards safe, reliable and controllable AI

Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?

实现基于Socket自定义的redis简单客户端

Error in Android connection database query statement

Stackoverflow annual report 2022: what are developers' favorite databases?

伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
随机推荐
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
用手机摄像头就能捕捉指纹?!准确度堪比签字画押,专家:你们在加剧歧视
JVM tuning
微信小程序中使用vant组件
Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month
Fuzzy background of unity (take you to appreciate the hazy beauty of women)
畅直播|针对直播痛点的关键技术解析
[cann document express issue 06] first knowledge of tbe DSL operator development
物聯網?快來看 Arduino 上雲啦
Leetcode(146)——LRU 缓存
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
C语言实现扫雷(简易版)
图像PANR
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
[cloud resident co creation] ModelBox draws your own painting across the air
1、 Downloading and installing appium
基于QT+MySQL的相机租赁管理系统
Bean lifecycle flowchart
微信小程序自定义tabBar
Two fellow countrymen from Hunan have jointly launched a 10 billion yuan IPO