Repository navigation
Expand file tree
/
Copy path05-PrototypePattern.java
More file actions
152 lines (127 loc) · 5.46 KB
/
Copy path05-PrototypePattern.java
File metadata and controls
152 lines (127 loc) · 5.46 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Prototype Design Pattern Example
*
* This file demonstrates:
* 1. Bad Example - Not using Prototype pattern (direct creation)
* 2. Good Example - Using Prototype pattern (cloning pre-configured objects)
*/
// ------------------- BAD EXAMPLE -------------------
// Problem: Creating complex objects from scratch each time
class EmployeeBad {
private String name;
private String role;
private String department;
private String[] permissions;
private String emailSignature;
// Creating a fully configured object from scratch each time is inefficient
public EmployeeBad(String name, String role, String department) {
System.out.println("Creating employee from scratch (expensive operation)");
this.name = name;
this.role = role;
this.department = department;
// Expensive operations for each new object
this.setupDefaultPermissions();
this.generateEmailSignature();
}
private void setupDefaultPermissions() {
// Simulating a complex, expensive operation
System.out.println(" - Setting up default permissions for " + role);
if (role.equals("Manager")) {
this.permissions = new String[] {"READ", "WRITE", "UPDATE", "DELETE", "APPROVE"};
} else if (role.equals("Developer")) {
this.permissions = new String[] {"READ", "WRITE", "UPDATE"};
}
}
private void generateEmailSignature() {
// Simulating another complex operation
System.out.println(" - Generating email signature with company logo");
this.emailSignature = name + " | " + role + " | " + department + " Department";
}
public void display() {
System.out.println(name + " - " + role + " in " + department);
}
}
// ------------------- GOOD EXAMPLE -------------------
// Solution: Create pre-configured objects once, then clone them
class Employee implements Cloneable {
private String name;
private String role;
private String department;
private String[] permissions;
private String emailSignature;
// Constructor for creating a fully configured prototype
public Employee(String role, String department) {
System.out.println("Creating " + role + " prototype (done once)");
this.name = "Default";
this.role = role;
this.department = department;
// Do the expensive setup once per prototype
this.setupDefaultPermissions();
this.generateEmailSignature();
}
private void setupDefaultPermissions() {
// Complex operation - only done once per role
System.out.println(" - Setting up default permissions for " + role);
if (role.equals("Manager")) {
this.permissions = new String[] {"READ", "WRITE", "UPDATE", "DELETE", "APPROVE"};
} else if (role.equals("Developer")) {
this.permissions = new String[] {"READ", "WRITE", "UPDATE"};
}
}
private void generateEmailSignature() {
// Another complex operation - only done once per role
System.out.println(" - Generating email signature with company logo");
this.emailSignature = name + " | " + role + " | " + department + " Department";
}
public void setName(String name) {
this.name = name;
// Just update the name in the signature, not regenerating the whole thing
this.emailSignature = name + " | " + role + " | " + department + " Department";
}
public void display() {
System.out.println(name + " - " + role + " in " + department);
}
// Clone method - copies the fully configured object quickly
@Override
public Employee clone() {
System.out.println("Cloning " + role + " (fast operation)");
Employee clone = null;
try {
clone = (Employee) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
// Client code
public class PrototypePattern {
public static void main(String[] args) {
// BAD EXAMPLE - Creating objects from scratch each time
System.out.println("----- Bad Example -----");
// Each creation runs all the expensive setup
EmployeeBad manager1 = new EmployeeBad("John", "Manager", "Sales");
EmployeeBad manager2 = new EmployeeBad("Jane", "Manager", "Marketing");
EmployeeBad dev1 = new EmployeeBad("Bob", "Developer", "IT");
manager1.display();
manager2.display();
dev1.display();
// GOOD EXAMPLE - Create prototypes once, then clone
System.out.println("\n----- Good Example -----");
// Create fully configured prototypes (done once per role)
Employee managerPrototype = new Employee("Manager", "Default");
Employee developerPrototype = new Employee("Developer", "Default");
// Clone the fully configured prototypes (fast operation)
System.out.println("\nCreating actual employees by cloning:");
Employee salesManager = managerPrototype.clone();
salesManager.setName("John");
Employee marketingManager = managerPrototype.clone();
marketingManager.setName("Jane");
Employee developer = developerPrototype.clone();
developer.setName("Bob");
// Display the results
salesManager.display();
marketingManager.display();
developer.display();
}
}