Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f0ee0e

Browse files
committed
Prototype pattern
1 parent d649d11 commit 6f0ee0e

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

‎README.md‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ __Java Design Patterns__ are divived into tree parts : *Creational*, *Structural
2828
4. Prototype Pattern
2929

3030
### Pattern Singleton
31+
3132
Pattern Singleton: > One Class, one Instance.
3233
Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category.
3334
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, driver objects, caching and thread pool. Singleton design pattern is also used in other design patterns like __Abstract Factory__, __Builder__, __Prototype__, __Facade__ etc. Singleton design pattern is used in core java classes also, for example __java.lang.Runtime__ , __java.awt.Desktop__.
@@ -102,7 +103,7 @@ Factory design pattern is used when we have a super class with multiple sub-clas
102103
* java.util.Calendar, ResourceBundle() and NumberFormat getInstance();
103104
* valueOf() method in wrapper classes like Boolean , Integer etc.
104105

105-
# Abstract Factory
106+
### Abstract Factory
106107

107108
This is one of the Creational Pattern and almost similar to Factory Pattern except the fact that it's most like
108109
Factory of factories.
@@ -126,7 +127,7 @@ Factory of factories.
126127
```
127128

128129

129-
# Pattern Builder
130+
### Pattern Builder
130131

131132
Builder pattern is a creational design pattern as Factory Pattern and Abstract Factory Pattern. This pattern was introduced to solve some of the problems with Factory and Abstract Factory patterns when the Object contains a lot of attributes. This pattern deals with a static nested class and then copy all the arguments from the outer class to the Builder class.
132133
The sample code where we have a Computer class and ComputerBuilder to build it are available in the package `com.builder.Computer`.
@@ -148,4 +149,70 @@ public class TestBuilderPattenr{
148149

149150
}
150151
```
151-
There are really various implementations of this pattern in JDK.
152+
There are really various implementations of this pattern in JDK : java.lang.StringBuilder#append() (unsynchronized) java.lang.StringBuffer#append() (synchronized) .
153+
154+
### Pattern Prototype
155+
156+
Prototype pattern is one of the Creational Design pattern, so it provides a mechanism of object creation. Prototype pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. So this pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. This pattern uses java cloning to copy the object.
157+
158+
```java
159+
160+
import java.util.ArrayList;
161+
import java.util.List;
162+
public class Users implements Cloneable{
163+
private List<String> empList;
164+
165+
public Users(){
166+
167+
empList = new ArrayList<>();
168+
}
169+
170+
public Users(List<String> list){
171+
this.empList=list;
172+
}
173+
174+
//read some data from the database.
175+
public void loadData(){
176+
empList.add("japak");
177+
empList.add("King");
178+
empList.add("David");
179+
empList.add("Romeo");
180+
}
181+
182+
public List<String> getEmpList() {
183+
return empList;
184+
}
185+
186+
@Override
187+
public Object clone() throws CloneNotSupportedException{
188+
List<String> temp = new ArrayList<String>();
189+
for(String s : this.getEmpList()){
190+
temp.add(s);
191+
}
192+
return new Users(temp);
193+
}
194+
}
195+
196+
//Notice that the clone method is overridden to provide a deep copy of the users list.
197+
```
198+
199+
Here's the program that will show the benefit of the Prototype pattern usage.
200+
201+
```java
202+
public class PrototypePatternTest {
203+
public static void main(String[] args) throws CloneNotSupportedException {
204+
Users usr = new Users();
205+
user.loadData();
206+
//Use the clone method to get the User object
207+
Users useNew = (Users) user.clone();
208+
Users useNew1 = (Users) user.clone();
209+
List<String> list = useNew.getEmpList();
210+
list.add("John");
211+
212+
List<String> list1 = usersNew1.getEmpList();
213+
list1.remove("Pankaj");
214+
System.out.println("users List: "+users.getEmpList());
215+
System.out.println("users New List: "+list);
216+
System.out.println("users New1 List: "+list1);
217+
}
218+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /