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 0eeb614

Browse files
committed
New Prototype example.
1 parent 2ab1532 commit 0eeb614

File tree

11 files changed

+233
-128
lines changed

11 files changed

+233
-128
lines changed

‎nbproject/project.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jlink.additionalmodules=
7373
jlink.additionalparam=
7474
jlink.launcher=true
7575
jlink.launcher.name=design_patterns_exercises_with_java
76-
main.class=main.Main
76+
main.class=creational.singleton.test.SingletonTest
7777
manifest.file=manifest.mf
7878
meta.inf.dir=${src.dir}/META-INF
7979
mkdist.disabled=false
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
8+
*
9+
* @author https://github.com/cbozan
10+
*/
11+
public abstract class Data {
12+
13+
public int dataNo, dataCategory;
14+
15+
public Data(){}
16+
17+
public Data(int dataNo, int dataCategory){
18+
this.dataCategory = dataCategory;
19+
this.dataNo = dataNo;
20+
}
21+
22+
public Data(Data data){
23+
this(data.dataNo, data.dataCategory);
24+
}
25+
26+
public abstract Data clone();
27+
28+
@Override
29+
public String toString() {
30+
return "Data{" + "dataNo=" + dataNo + ", dataCategory=" + dataCategory + '}';
31+
}
32+
33+
34+
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
8+
*
9+
* @author https://github.com/cbozan
10+
*/
11+
public class SuperUserData extends Data{
12+
13+
int superUserId, branchId;
14+
15+
public SuperUserData(){}
16+
17+
public SuperUserData(int superUserId, int branchId){
18+
this.superUserId = superUserId;
19+
this.branchId = branchId;
20+
}
21+
22+
public SuperUserData(SuperUserData superUserData){
23+
super(superUserData);
24+
this.branchId = superUserData.branchId;
25+
this.superUserId = superUserData.superUserId;
26+
}
27+
28+
@Override
29+
public Data clone() {
30+
return new SuperUserData(this);
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return super.toString() + " | SuperUserData{" + "superUserId=" + superUserId + ", branchId=" + branchId + '}';
36+
}
37+
38+
39+
40+
}

‎src/creational/prototype/example/UserData.java‎

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,89 +4,35 @@
44
*/
55
package creational.prototype.example;
66

7-
import java.util.ArrayList;
8-
97
/**
108
*
119
* @author https://github.com/cbozan
1210
*/
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-
}
11+
public class UserData extends Data{
12+
13+
int normalUserId, taskId;
2314

24-
public UserData(ArrayList<String[]> userData, ArrayList<String[]> userMetaData){
25-
setUserData(userData);
26-
setUserMetaData(userMetaData);
15+
public UserData(){}
16+
17+
public UserData(int normaluserId, int taskId){
18+
this.normalUserId = normaluserId;
19+
this.taskId = taskId;
2720
}
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-
21+
22+
public UserData(UserData userData){
23+
super(userData);
24+
this.normalUserId = userData.normalUserId;
25+
this.taskId = userData.taskId;
4326
}
44-
27+
4528
@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);
29+
public Data clone() {
30+
return new UserData(this);
7131
}
7232

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;
33+
@Override
34+
public String toString() {
35+
return super.toString() + " | UserData{" + "normalUserId=" + normalUserId + ", taskId=" + taskId + '}';
8736
}
88-
89-
90-
9137

9238
}

‎src/creational/prototype/test/PrototypeTest.java‎

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package creational.prototype.test;
66

7+
import creational.prototype.example.SuperUserData;
78
import creational.prototype.example.UserData;
89
import java.util.logging.Level;
910
import java.util.logging.Logger;
@@ -14,65 +15,25 @@
1415
*/
1516
public class PrototypeTest {
1617

17-
1818
public static void main(String[] args) throws CloneNotSupportedException {
1919

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);
20+
SuperUserData sud = new SuperUserData(1, 1);
21+
sud.dataCategory = 2;
22+
sud.dataNo = 2;
4523

46-
System.out.println("\n\n");
24+
SuperUserDatasudClone = (SuperUserData) sud.clone();
4725

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-
}
26+
UserData ud = new UserData(3, 3);
27+
ud.dataCategory = 4;
28+
ud.dataNo = 4;
6029

61-
System.out.println();
30+
UserData udClone = (UserData) ud.clone();
31+
32+
System.out.println("sud object : " + sud + " and hashCode : " + sud.hashCode());
33+
System.out.println("sudClone object : " + sudClone + " and hashCode : " + sudClone.hashCode());
6234

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-
}
35+
System.out.println("ud object : " + ud + " and hashCode : " + ud.hashCode());
36+
System.out.println("udClone object : " + udClone + " and hashCode : " + udClone.hashCode());
7137

72-
}
73-
74-
75-
76-
77-
38+
}
7839
}

‎src/creational/singleton/test/SingletonTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import creational.singleton.singleton.StatikBlokBaslatma;
1010
import creational.singleton.singleton.TembelBaslatma;
1111
import creational.singleton.singleton.ThreadGuvenligi;
12+
import java.lang.reflect.Constructor;
1213

1314
/**
1415
*
@@ -43,14 +44,38 @@ public static void main(String[] args){
4344
ThreadGuvenligi instance4_2 = ThreadGuvenligi.getInstance();
4445
if(instance4_1 == instance4_2){
4546
System.out.println("Thread Güvenliği singleton test sonucu : " + "Aynı nesneye işaret edildiğinden test başarılı.");
47+
4648
}
4749

4850
// Bill Pugh Singleton
4951
BillPughSingleton instance5_1 = BillPughSingleton.getInstance();
5052
BillPughSingleton instance5_2 = BillPughSingleton.getInstance();
5153
if(instance5_1 == instance5_2){
5254
System.out.println("Bill Pugh singleton test sonucu : " + "Aynı nesneye işaret edildiğinden test başarılı.");
55+
System.out.println("instance5_1 hashcode : " + instance5_1.hashCode());
56+
System.out.println("instance5_2 hashcode : " + instance5_2.hashCode());
5357
}
5458

59+
60+
61+
// BillPughSingleton'ın amacını yok etme
62+
// BillPughSingleton crack
63+
BillPughSingleton instance = BillPughSingleton.getInstance();
64+
BillPughSingleton instance2 = null;
65+
66+
try{
67+
Constructor[] constructors = BillPughSingleton.class.getDeclaredConstructors();
68+
for(Constructor constructor : constructors){
69+
constructor.setAccessible(true);
70+
instance2 = (BillPughSingleton)constructor.newInstance();
71+
break;
72+
}
73+
74+
} catch(Exception e){
75+
e.printStackTrace();
76+
}
77+
78+
System.out.println("instance hashcode : " + instance.hashCode());
79+
System.out.println("instance1 hashcode : " + instance2.hashCode());
5580
}
5681
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 structural.adapter.database;
6+
7+
8+
/**
9+
*
10+
* @author https://github.com/cbozan
11+
*/
12+
public class FireBase{
13+
14+
public FireBase(String connection) {
15+
}
16+
17+
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 structural.adapter.database;
6+
7+
import structural.adapter.entity.DBConnection;
8+
9+
/**
10+
*
11+
* @author https://github.com/cbozan
12+
*/
13+
public class MySQL extends DBConnection{
14+
15+
public MySQL(String connection) {
16+
super(connection);
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 structural.adapter.database;
6+
7+
import structural.adapter.entity.DBConnection;
8+
9+
/**
10+
*
11+
* @author https://github.com/cbozan
12+
*/
13+
public class PostgreSQL extends DBConnection{
14+
15+
public PostgreSQL(String connection) {
16+
super(connection);
17+
}
18+
19+
}

0 commit comments

Comments
(0)

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