- 浏览: 31365 次
- 性别:
- 来自: 杭州
最近访客 更多访客>>
最新评论
-
bbym010:
通俗易懂,不错
Build模式的理解 -
0000ps:
今天在MSDN上看到一段内容:
Value Object. S ...
Value Object vs. Data Transfer Object (VO vs. DTO)
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
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
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
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); } }
发表评论
-
Facade vs Proxy
2010-04-02 08:36 1185Question: i feel there is not m ... -
UML中关联(association)和依赖(dependency)的区别
2010-03-30 08:56 3211这个问题不仅我们这些小喽罗会迷惑,很多大拿们也没有统一的认识, ... -
Aggregation v.s. Composition
2010-03-30 08:13 1348Aggregation Aggregation is a k ... -
UML中几种类间关系:继承、实现、依赖、关联、聚合、组合
2009-12-06 23:13 1189继承,泛化(Generalization) 指的是一个类( ... -
Build模式的理解
2009-11-26 23:23 2148Builder模式 是为了将构建复杂对象的组装过程和它的创建部 ... -
设计模式简图
2009-11-26 22:00 856... -
Proxy, Builder and Decoration 异同点
2009-11-09 13:52 0晚上写分打发打发法 -
面向对象设计原则
2009-11-07 09:43 706正如牛顿三大定律 ... -
Flyweight Pattern 享元模式
2009-09-29 16:07 867GOF:运用共享技术有效地支持大量细粒度的对象。 解释一 ... -
Is a Java Immutable Class Always final?
2009-09-27 15:10 1484In response to my recent blog p ... -
Defensive copying
2009-09-27 14:46 970A mutable object is simply an o ... -
Copy constructors
2009-09-27 14:29 781Copy constructors : provi ... -
Immutable objects
2009-09-27 14:13 848Immutable objects are simply ob ... -
Java Immutable Class
2009-09-27 14:09 1982Java Immutable Class[ From ] ... -
Value Object vs. Data Transfer Object (VO vs. DTO)
2009-09-18 10:31 1625The pattern which is known toda ...
相关推荐
What You Will Learn ...Clone objects using the prototype pattern Use the adapter pattern to make incompatible interfaces compatible Employ the strategy pattern to dynamically choose an algorithm
Jlink-clone解决办法,替换文件
“the emulator is JLink-Clone, the segger software only support orginal segger device” 然后闪退,IDE崩溃关闭! 解决方案: 1.升级压缩包里的固件(该固件将SN修改为默认的-1)。 2.进入J-Link Commander,...
Caffe2 Windows third_party(git clone失败或速度慢时可用)
I accomplished the animation of the firemonkey and the moving ground bar by just using two frames and doing TBitmap.Assign() between them which is not at all the best way to do it but it works for a ...
The core is decoupled from the GUI using the observer pattern, so it is easy to pull the code, extract the core and then build a new GUI around it. 1. Download and install Qt ...
解决最近升级iar出现的问题,J-Link弹出The connected J-Link is defective,将这几个dll复制粘贴替换掉jLink安装目录(.../Segger/JlinkARMV.../)下的dll,实测可行
本人用的JLINK仿真器.在使用新版KEIL时,提示要升级固件,升级后就出现JLINK is Clone的提示。在网上找了许多关于修复的资料,都觉得不是很好。经过本人反复试验,总算找到比较好的解决方案,操作步骤如下
- FIX: The TFlexPanel.FindControlAtPoint method maked virtual to realize RealTime-capability when on mouse cursor moving the flex-object search not occurs. - FIX: After deleting the selected points ...
Quickly place objects on the floor or terrain using volume based grounding. With the new multi selection support, ground hundreds of objects with a single click! ‣ Nudges Fold open the numeric ...
The assembly code clone search problem belongs to the field of software engineering. However, it strongly depends on practical nearest neighbor search techniques in data mining and databases. By ...
当一个类大部分都是相同的只有部分是不同的时候,如果需要大量这个类的对象,每次都重复实例化那些相同的部分是开销很大的,而如果clone之前建立对象的那些相同的部分,就可以节约开销。 针对php的一种实现方式就是_...
在windows下安装完后提示: Fatal error: Trying to clone an uncloneable object of class Imagick in C:\www\hx\pdf_to_png.php on line 17 使用IIS和Apache均会有这个提示。经多次测试后,发现两种解决方法: 1....
EBS 11i Creating a Clone using Oracle Application Manager (OAM Clone) (Doc ID 398619.1)
The core is decoupled from the GUI using the observer pattern, so it is easy to pull the code, extract the core and then build a new GUI around it. Screenshot -- ![Alt text]...
You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that ...
At the end of the book, you will learn how to make custom pages to manage data by creating a feedback module and how to create the order total using a tips module. In Getting Started with OpenCart ...
MS Paint Clone using JavaScript with Free Source Code
* Adjust the dir-iterator API and apply it to the local clone optimization codepath. * We have been trying out a few language features outside c89; the coding guidelines document did not talk ...