Using the Prototype pattern to clone objects

Prototype pattern is one of the creational patterns that concentrate on duplicating objects if needed. Assuming that we are in process of creating a template. Most of the times, we copy an existing template, do some changes in it and then will use it. Technically, we make a copy of the source, make some changes and will use according to the requirements. This is the core concept of the prototype pattern.

For example, assuming that we are creating an Online Leave Application, where someone is asked to fill-in the reason for the leave, start and end date of his/her leave along with details of the approver. A smart thing is for the very first time, we can ask the person to fill in the details and for the subsequent time we can provide an option to copy the first template, do some changes (like changing the start and end date) and allow him to submit.

One important thing that has to be considered in designing prototype pattern is regarding the depth to which we want the object to be copied. In Java terms, whether we want shallow copying or deep copying. In shallow copying, only the primitive properties of the outer object will be copied during the cloning operation, and not the object references. It means that changes made to the target object will be reflected back to the original source object. Whereas, in the case of deep copying, all the primitive and the object references will be copied bit-by-bit, so that it can be ensured that changes done to the target object is not reflected back to the source.

Now, let us see an example for the Prototype pattern what we have discussed till now. Given below is the complete example,

LeaveApplication.java

import java.text.SimpleDateFormat;
import java.util.Date;

public class LeaveApplication implements Cloneable {

private String reason;
private Date startDate;
private Date endDate;
private Approver approver;

public LeaveApplication(
String reason, Date startDate, Date endDate, Approver approver){

this.reason = reason;
this.startDate = startDate;
this.endDate = endDate;
this.approver = approver;
}

public LeaveApplication clone(){

Approver copyApprover = new Approver(
approver.getName(), approver.getDesignation());
LeaveApplication copyApplication = new LeaveApplication(
reason, ((Date)startDate.clone()), ((Date)endDate.clone()), copyApprover);
return copyApplication;
}

public String toString(){
return "[Leave Application:" + reason + "," + toString(startDate)
+ "," + toString(endDate) + approver + "]";
}

private String toString(Date date){

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy");
return format.format(date);
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

public Date getEndDate() {
return endDate;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

public Approver getApprover() {
return approver;
}

public void setApprover(Approver approver) {
this.approver = approver;
}
}


This LeaveApplication is the class that we want to clone. It has 3 simple properties namely reason, startDate and endDate and 1 composite property called Approver. Later on we see the class definition for Approver class. Now, let us have a look over the clone() method. Within this method, we create a new instance for the LeaveApplication class and set its properties to the value of the properties of the original object. In this way, we are making a deep cloning of the object and not a shallow cloning. Given below is the class definition for the Approver class,

Approver.java

package tips.pattern.prototype;

public class Approver {

private String name;
private String designation;

public Approver(String name, String designation){
this.name = name;
this.designation = designation;
}

public String toString(){
return "[Approver: " + name + "," + designation + "]";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}
}

Then, we define the main class PrototypeTest for testing the prototype pattern. It creates a new instance of the LeaveApplication class for setting the sickLeave properties. After that, it makes a clone of the sickLeave instance as a casualLeave and modifies some of its properties like the reason and the dates.

PrototypeTest.java

package tips.pattern.prototype;

import java.util.Date;

public class PrototypeTest {

public static void main(String[] args) {

Approver manager = new Approver("Johny", "manager");
LeaveApplication sickLeave =
new LeaveApplication("Fever", new Date(2007, 3, 20), new Date(2007, 3, 22), manager);
System.out.println(sickLeave);

LeaveApplication casualLeave = sickLeave.clone();
casualLeave.setReason("Vacation");
casualLeave.setStartDate(new Date(2007, 10, 10));
casualLeave.setEndDate(new Date(2007, 10, 20));
System.out.println(casualLeave);
}
}
【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器的建模与仿真展开,重点介绍了基于Matlab的飞行器动力学模型构建与控制系统设计方法。通过对四轴飞行器非线性运动方程的推导,建立其在三维空间中的姿态与位置动态模型,并采用数值仿真手段实现飞行器在复杂环境下的行为模拟。文中详细阐述了系统状态方程的构建、控制输入设计以及仿真参数设置,并结合具体代码实现展示了如何对飞行器进行稳定控制与轨迹跟踪。此外,文章还提到了多种优化与控制策略的应用背景,如模型预测控制、PID控制等,突出了Matlab工具在无人机系统仿真中的强大功能。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程师;尤其适合从事飞行器建模、控制算法研究及相关领域研究的专业人士。; 使用场景及目标:①用于四轴飞行器非线性动力学建模的教学与科研实践;②为无人机控制系统设计(如姿态控制、轨迹跟踪)提供仿真验证平台;③支持高级控制算法(如MPC、LQR、PID)的研究与对比分析; 阅读建议:建议读者结合文中提到的Matlab代码与仿真模型,动手实践飞行器建模与控制流程,重点关注动力学方程的实现与控制器参数调优,同时可拓展至多自由度或复杂环境下的飞行仿真研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值