File tree Expand file tree Collapse file tree 16 files changed +338
-0
lines changed
java-path-core-despattern
java/gr/codelearn/designpatterns
java/gr/codelearn/generics Expand file tree Collapse file tree 16 files changed +338
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5
+ <parent >
6
+ <artifactId >java-path-core</artifactId >
7
+ <groupId >gr.codelearn</groupId >
8
+ <version >2021.1.0</version >
9
+ </parent >
10
+ <modelVersion >4.0.0</modelVersion >
11
+
12
+ <artifactId >java-path-core-despattern</artifactId >
13
+
14
+ <properties >
15
+ <maven .compiler.source>17</maven .compiler.source>
16
+ <maven .compiler.target>17</maven .compiler.target>
17
+ </properties >
18
+
19
+ </project >
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .behavioral ;
2
+
3
+ public class AESEncryptionStrategy implements EncryptionStrategy {
4
+ @ Override
5
+ public String encryptData (String text ) {
6
+ return text .hashCode () + " " + "AES" .hashCode () ;
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .behavioral ;
2
+
3
+ public interface EncryptionStrategy {
4
+ String encryptData (String text );
5
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .behavioral ;
2
+
3
+ public class Encryptor {
4
+ private EncryptionStrategy strategy ;
5
+
6
+ public void setStrategy (EncryptionStrategy strategy ){
7
+ this .strategy = strategy ;
8
+ }
9
+
10
+ public String encrypt (String plainText ){
11
+ return strategy .encryptData (plainText );
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .behavioral ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+
6
+ public class Main {
7
+ private static final Logger logger = LoggerFactory .getLogger (Main .class );
8
+
9
+ public static void main (String [] args ) {
10
+ Encryptor encryptor = new Encryptor ();
11
+
12
+ EncryptionStrategy aesStrategy = new AESEncryptionStrategy ();
13
+ EncryptionStrategy rsaStrategy = new RSAEncryptionStrategy ();
14
+
15
+ encryptor .setStrategy (aesStrategy );
16
+ logger .info ("{}" , encryptor .encrypt ("This is a text" ));
17
+
18
+ encryptor .setStrategy (rsaStrategy );
19
+ logger .info ("{}" , encryptor .encrypt ("This is a text" ));
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .behavioral ;
2
+
3
+ public class RSAEncryptionStrategy implements EncryptionStrategy {
4
+ @ Override
5
+ public String encryptData (String text ) {
6
+ return text .hashCode () + " " + "RSA" .hashCode () ;
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .creational ;
2
+
3
+ public class EagerSingleton {
4
+ private static final EagerSingleton INSTANCE = new EagerSingleton ();
5
+
6
+ private EagerSingleton (){}
7
+
8
+ public static EagerSingleton getInstance (){
9
+ return INSTANCE ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .creational ;
2
+
3
+ public class LazySingleton {
4
+
5
+ private static LazySingleton instance = null ;
6
+
7
+ private LazySingleton (){}
8
+
9
+ public synchronized static LazySingleton getInstance (){
10
+ if (instance == null ){
11
+ instance = new LazySingleton ();
12
+ }
13
+ return instance ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .creational ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+
6
+ public class Main {
7
+ private static final Logger logger = LoggerFactory .getLogger (Main .class );
8
+
9
+ public static void main (String [] args ) {
10
+ ScoreTracker scoreTracker = ScoreTracker .getInstance ();
11
+
12
+ logger .info ("{}" , scoreTracker .getScore ());
13
+ scoreTracker .increaseScore (10 );
14
+ logger .info ("{}" , scoreTracker .getScore ());
15
+
16
+ scoreTracker = ScoreTracker .getInstance ();
17
+ logger .info ("{}" , scoreTracker .getScore ());
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package gr .codelearn .designpatterns .creational ;
2
+
3
+ public class ScoreTracker {
4
+ private static ScoreTracker instance = null ;
5
+ private int score = 0 ;
6
+
7
+ private ScoreTracker (){}
8
+
9
+ public static ScoreTracker getInstance (){
10
+ if (instance == null ){
11
+ instance = new ScoreTracker ();
12
+ }
13
+ return instance ;
14
+ }
15
+
16
+ public void increaseScore (int number ){
17
+ score += number ;
18
+ }
19
+
20
+ public int getScore (){
21
+ return score ;
22
+ }
23
+
24
+
25
+ }
You can’t perform that action at this time.
0 commit comments