Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/oops/designPatterns/prototype/PrototypeDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
public class PrototypeDemo {

public static void main(String[] args) {
Registry registry = new Registry();
Movie movie = (Movie) registry.createItem("Movie");
Registry registry = Registry.getInstance();
Movie movie = (Movie) registry.createItem("ShortMovie");
movie.setTitle("The Prestige");

System.out.println(movie);
System.out.println(movie.getRuntime());
System.out.println(movie.getTitle());
System.out.println(movie.getUrl());

Movie anotherMovie = (Movie) registry.createItem("Movie");
Movie anotherMovie = (Movie) registry.createItem("ArtMovie");
anotherMovie.setTitle("Gang of Wassepur");

System.out.println(anotherMovie);
Expand Down
23 changes: 22 additions & 1 deletion src/oops/designPatterns/prototype/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,30 @@ public class Registry {

private Map<String, Item> items = new HashMap<String, Item>();

public Registry() {
private static volatile Registry instance;

private static Object mutex = new Object();

private Registry() {
loadItems();
}

public static Registry getInstance() {

Registry result = instance;

if (result == null) {
synchronized (mutex) {
result = instance;
if (result == null) {
instance = result = new Registry();
}
}
}

return result;
}


public Item createItem (String type) {
Item item = null;
Expand Down