Prototype pattern implements a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly.
The Prototype pattern allows an object to create customized objects without knowing their class or any details of how to create them. The difference between Factory method and Prototype pattern is that Prototype doesn’t require subclassing, but it does require an “initialize” operation. Factory Method requires subclassing, but doesn’t require Initialize.
Implementation
Step 1
create an abstract class TrackingAgent which will implement Cloneable interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package net.tecbar.designpattern.prototype; public abstract class TrackingAgent implements Cloneable { protected String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void track(String trackingNum); public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } } |
Step 2
create TrackingAgent subclasses FedexTrackingAgent, DhlTrackingAgent and UpsTrackingAgent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package net.tecbar.designpattern.prototype; public class DhlTrackingAgent extends TrackingAgent { public DhlTrackingAgent() { name = "DHL"; System.out.println("create DhlTrackingAgent"); // add some extra work to assume the create a new object will spend some time } public void track(String trackingNum) { System.out.println("track DHL tracking # " + trackingNum); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package net.tecbar.designpattern.prototype; public class FedexTrackingAgent extends TrackingAgent { public FedexTrackingAgent() { name = "FEDEX"; System.out.println("create FedexTrackingAgent"); } public void track(String trackingNum) { System.out.println("track FedEx tracking # " + trackingNum); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package net.tecbar.designpattern.prototype; public class UpsTrackingAgent extends TrackingAgent { public UpsTrackingAgent() { name = "UPS"; System.out.println("create UpsTrackingAgent"); } public void track(String trackingNum) { System.out.println("track UPS tracking # " + trackingNum); } } |
Step 3
Create a map or hashtable to save a instance of each tracking agent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package net.tecbar.designpattern.prototype; import java.util.HashMap; import java.util.Map; public class AgentCache { private static Map<String, TrackingAgent> agentMap = new HashMap<String, TrackingAgent>(); public static void init() { DhlTrackingAgent dhlAgent = new DhlTrackingAgent(); agentMap.put(dhlAgent.getName(), dhlAgent); FedexTrackingAgent fedexAgent = new FedexTrackingAgent(); agentMap.put(fedexAgent.getName(), fedexAgent); UpsTrackingAgent upsAgent = new UpsTrackingAgent(); agentMap.put(upsAgent.getName(), upsAgent); } public static TrackingAgent getAgent(String name) { return (TrackingAgent)agentMap.get(name).clone(); } } |
Step 4
Write a client to demo how to get a clone copy that was saved in the AgentAche
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package net.tecbar.designpattern.prototype; public class PrototypeClient { public static void main(String[] args) { AgentCache.init(); TrackingAgent agentClone1 = AgentCache.getAgent("FEDEX"); agentClone1.track("64543543654654654");; TrackingAgent agentClone2 = AgentCache.getAgent("DHL"); agentClone2.track("64543543");; TrackingAgent agentClone3 = AgentCache.getAgent("UPS"); agentClone3.track("1Z849564364543543");; agentClone1 = AgentCache.getAgent("FEDEX"); agentClone1.track("5675453543654654656");; } } |
Output
1 2 3 4 5 6 7 8 9 |
create DhlTrackingAgent create FedexTrackingAgent create UpsTrackingAgent track FedEx tracking # 64543543654654654 track DHL tracking # 64543543 track UPS tracking # 1Z849564364543543 track FedEx tracking # 5675453543654654656 |
Pingback: Software Design Patterns
不错的文章,