Skip to content

Commit 2ab1532

Browse files
committed
Added Prototype design pattern
1 parent 5172e49 commit 2ab1532

File tree

3 files changed

+178
-2
lines changed

3 files changed

+178
-2
lines changed

src/creational/builder/test/BuilderTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ public class BuilderTest {
1414

1515
public static void main(String[] args){
1616

17-
CokOzellikliNesne.CokOzellikliNesneBuilder build = new CokOzellikliNesne.CokOzellikliNesneBuilder(3 /* rastgele*/);
18-
CokOzellikliNesne nesne = build.setP2("Properties2").setP3(true).build();
17+
CokOzellikliNesne.CokOzellikliNesneBuilder builder = new CokOzellikliNesne.CokOzellikliNesneBuilder(3 /* rastgele*/);
18+
CokOzellikliNesne nesne = builder.setP2("Properties2").setP3(true).build();
19+
20+
// // OR
21+
// builder = builder.setP2("Properties2");
22+
// builder = builder.setP3(false);
23+
// CokOzellikliNesne nesne = builder.build();
24+
1925
System.out.println(nesne.toString());
2026

2127
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package creational.prototype.example;
6+
7+
import java.util.ArrayList;
8+
9+
/**
10+
*
11+
* @author https://github.com/cbozan
12+
*/
13+
public class UserData implements Cloneable {
14+
15+
ArrayList<String[]> userData;
16+
ArrayList<String[]> userMetaData;
17+
18+
public UserData() {
19+
userData = new ArrayList<>();
20+
userMetaData = new ArrayList<>();
21+
getUserDataFromDB();
22+
}
23+
24+
public UserData(ArrayList<String[]> userData, ArrayList<String[]> userMetaData){
25+
setUserData(userData);
26+
setUserMetaData(userMetaData);
27+
}
28+
29+
public void getUserDataFromDB() {
30+
31+
// DataBase operations for userData table
32+
// userData için veri tabanı işlemleri gerçekleştiriliyor (varsayılan)
33+
userData.add(new String[]{"Row1-col1", "Row1-col2", "Row1-col3"});
34+
userData.add(new String[]{"Row2-col1", "Row2-col2", "Row2-col3"});
35+
userData.add(new String[]{"Row3-col1", "Row3-col2", "Row3-col3"});
36+
37+
// DataBase operations for UserMetaData table
38+
// userMetaData için veri tabanı işlemleri gerçekleştiriliyor (varsayıyoruz)
39+
userMetaData.add(new String[]{"Row1-col1", "Row1-col2"});
40+
userMetaData.add(new String[]{"Row2-col1", "Row2-col2"});
41+
userMetaData.add(new String[]{"Row3-col1", "Row3-col2"});
42+
43+
}
44+
45+
@Override
46+
public Object clone() throws CloneNotSupportedException {
47+
ArrayList<String[]> tempUserData = new ArrayList<>();
48+
ArrayList<String[]> tempUserMetaData = new ArrayList<>();
49+
50+
// copy userData
51+
// userData verilerinin kopyalanması
52+
for(String[] row : getUserData()){
53+
tempUserData.add(new String[row.length]);
54+
for(int i = 0; i < row.length; i++){
55+
tempUserData.get(tempUserData.size() - 1)[i] = row[i];
56+
}
57+
}
58+
59+
// copy userMetaData
60+
// userMetaData verilerinin kopyalanması
61+
for(String[] row : getUserMetaData()){
62+
tempUserMetaData.add(new String[row.length]);
63+
for(int i = 0; i < row.length; i++){
64+
tempUserMetaData.get(tempUserMetaData.size() - 1)[i] = row[i];
65+
}
66+
}
67+
68+
// create new object without db connection
69+
// Veri tabanı bağlantısı olmadan yeni nesnesimizi oluşturuyoruz (mevcut verilerle)
70+
return new UserData(tempUserData, tempUserMetaData);
71+
}
72+
73+
public ArrayList<String[]> getUserData() {
74+
return userData;
75+
}
76+
77+
public void setUserData(ArrayList<String[]> userData) {
78+
this.userData = userData;
79+
}
80+
81+
public ArrayList<String[]> getUserMetaData() {
82+
return userMetaData;
83+
}
84+
85+
public void setUserMetaData(ArrayList<String[]> userMetaData) {
86+
this.userMetaData = userMetaData;
87+
}
88+
89+
90+
91+
92+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package creational.prototype.test;
6+
7+
import creational.prototype.example.UserData;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
11+
/**
12+
*
13+
* @author https://github.com/cbozan
14+
*/
15+
public class PrototypeTest {
16+
17+
18+
public static void main(String[] args) throws CloneNotSupportedException {
19+
20+
//ilk nesne oluşturulma aşaması veri tabanı bağlantısı sağlanacak
21+
// initial object creation (will use the db connection)
22+
UserData userDataOriginal = new UserData();
23+
24+
// clone original
25+
UserData userDataCopy1 = (UserData) userDataOriginal.clone();
26+
userDataCopy1.getUserData().add(new String[]{"added later copy1", "added later copy1"});
27+
userDataCopy1.getUserData().add(new String[]{"added later copy1", "added later", "added later", "added later"});
28+
29+
// clone copy1
30+
UserData userDataCopy2 = (UserData) userDataCopy1.clone();
31+
32+
// delete last element (array)
33+
// listedeki son elemanı (diziyi) sil
34+
userDataCopy2.getUserData().remove(userDataCopy2.getUserData().size() - 1);
35+
userDataCopy2.getUserMetaData().add(new String[]{"added later copy2", "akdsjfkj"});
36+
37+
System.out.println("Original data\n");
38+
myPrint(userDataOriginal);
39+
40+
System.out.println("\n\nCopy-1 data\n\n");
41+
myPrint(userDataCopy1);
42+
43+
System.out.println("\n\nCopy-2 data\n\n");
44+
myPrint(userDataCopy2);
45+
46+
System.out.println("\n\n");
47+
48+
}
49+
50+
public static void myPrint(UserData userData){
51+
52+
System.out.println("\tuserData\n");
53+
for(String[] row : userData.getUserData()){
54+
System.out.print("\t\t");
55+
for(int col = 0; col < row.length; col++){
56+
System.out.print(row[col] + "\t");
57+
}
58+
System.out.println();
59+
}
60+
61+
System.out.println();
62+
63+
System.out.println("\tuserMetaData\n");
64+
for(String[] row : userData.getUserMetaData()){
65+
System.out.print("\t\t");
66+
for(int col = 0; col < row.length; col++){
67+
System.out.print(row[col] + "\t");
68+
}
69+
System.out.println();
70+
}
71+
72+
}
73+
74+
75+
76+
77+
78+
}

0 commit comments

Comments
 (0)