Skip to content

Commit 9c8010d

Browse files
committedDec 11, 2021
generics and design patterns
1 parent 4f6858a commit 9c8010d

File tree

16 files changed

+338
-0
lines changed

16 files changed

+338
-0
lines changed
 

‎java-path-core-despattern/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package gr.codelearn.designpatterns.behavioral;
2+
3+
public interface EncryptionStrategy {
4+
String encryptData(String text);
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN" monitorInterval="30">
3+
<Properties>
4+
<Property name="LOG_PATTERN">
5+
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} - [%15.15t] %-30.30c{1.} : %m%n%ex
6+
</Property>
7+
</Properties>
8+
9+
<Appenders>
10+
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
11+
<PatternLayout pattern="${LOG_PATTERN}"/>
12+
</Console>
13+
14+
<!-- Rolling Random Access File Appender with a default buffer of 256 * 1024 bytes -->
15+
<RollingRandomAccessFile name="RollingRandomAccessFileAppender"
16+
fileName="logs/java-path-core-despattern.log"
17+
filePattern="logs/java-path-core-despattern-%d{yyyy-MM-dd}.zip">
18+
<PatternLayout>
19+
<Pattern>${LOG_PATTERN}</Pattern>
20+
</PatternLayout>
21+
<Policies>
22+
<TimeBasedTriggeringPolicy/>
23+
</Policies>
24+
<DefaultRolloverStrategy>
25+
<Delete basePath="logs">
26+
<IfLastModified age="30d"/>
27+
</Delete>
28+
</DefaultRolloverStrategy>
29+
</RollingRandomAccessFile>
30+
</Appenders>
31+
32+
<Loggers>
33+
<AsyncLogger name="gr.codelearn" level="debug" additivity="false">
34+
<AppenderRef ref="ConsoleAppender"/>
35+
<AppenderRef ref="RollingRandomAccessFileAppender"/>
36+
</AsyncLogger>
37+
38+
<Root level="info">
39+
<AppenderRef ref="ConsoleAppender"/>
40+
<AppenderRef ref="RollingRandomAccessFileAppender"/>
41+
</Root>
42+
</Loggers>
43+
</Configuration>

‎java-path-core.generics/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.generics</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>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gr.codelearn.generics;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Main {
10+
private static final Logger logger = LoggerFactory.getLogger(Main.class);
11+
12+
public static void main(String[] args) {
13+
// customGeneric();
14+
// logger.info("{}", genericMethod(5));
15+
// upperBound();
16+
lowerBound();
17+
ArrayList a = new ArrayList();
18+
}
19+
20+
public static void lowerBound(){
21+
List<Integer> intList = new ArrayList<>();
22+
intList.add(5); intList.add(15); intList.add(25);
23+
24+
List<Double> doubleList = new ArrayList<>();
25+
doubleList.add(3.5); doubleList.add(23.53); doubleList.add(34.45);
26+
27+
List<Number> numberList = new ArrayList<>();
28+
numberList.add(3.5); numberList.add(23); numberList.add(45f);
29+
30+
List<? super Integer> lowerBoundList;
31+
lowerBoundList = intList;
32+
lowerBoundList = numberList;
33+
// lowerBoundList = doubleList; // compilation error
34+
}
35+
36+
37+
public static void upperBound(){
38+
List<Integer> intList = new ArrayList<>();
39+
intList.add(5); intList.add(15); intList.add(25);
40+
41+
List<Double> doubleList = new ArrayList<>();
42+
doubleList.add(3.5); doubleList.add(23.53); doubleList.add(34.45);
43+
44+
logger.info("{}", avg(intList));
45+
logger.info("{}", avg(doubleList));
46+
}
47+
48+
private static double avg(List<? extends Number> numbers){
49+
double av = 0;
50+
for (int i = 0; i < numbers.size() ; i++) {
51+
av += numbers.get(i).doubleValue();
52+
}
53+
return av/numbers.size();
54+
}
55+
56+
57+
58+
59+
60+
private static void customGeneric() {
61+
Process<Integer, Double> mangProcess = new Process<>(1, "print", 2013, 8.9);
62+
Process<String, Integer> sysProcess = new Process("admin1", "print", 2013, 9);
63+
logger.info("{}", mangProcess);
64+
logger.info("{}", sysProcess);
65+
}
66+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package gr.codelearn.generics;
2+
3+
public class Process<T, S> {
4+
private T pid;
5+
private String pname;
6+
private int releasedYear;
7+
private S runningTime;
8+
9+
public Process(T pid, String pname, int releasedYear, S runningTime) {
10+
this.pid = pid;
11+
this.pname = pname;
12+
this.releasedYear = releasedYear;
13+
this.runningTime = runningTime;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Process{" + "pid=" + pid + ", pname='" + pname + '\'' + ", releasedYear=" + releasedYear +
19+
", runningTime=" + runningTime + '}';
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN" monitorInterval="30">
3+
<Properties>
4+
<Property name="LOG_PATTERN">
5+
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} - [%15.15t] %-30.30c{1.} : %m%n%ex
6+
</Property>
7+
</Properties>
8+
9+
<Appenders>
10+
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
11+
<PatternLayout pattern="${LOG_PATTERN}"/>
12+
</Console>
13+
14+
<!-- Rolling Random Access File Appender with a default buffer of 256 * 1024 bytes -->
15+
<RollingRandomAccessFile name="RollingRandomAccessFileAppender"
16+
fileName="logs/java-path-core-generics.log"
17+
filePattern="logs/java-path-core-generics-%d{yyyy-MM-dd}.zip">
18+
<PatternLayout>
19+
<Pattern>${LOG_PATTERN}</Pattern>
20+
</PatternLayout>
21+
<Policies>
22+
<TimeBasedTriggeringPolicy/>
23+
</Policies>
24+
<DefaultRolloverStrategy>
25+
<Delete basePath="logs">
26+
<IfLastModified age="30d"/>
27+
</Delete>
28+
</DefaultRolloverStrategy>
29+
</RollingRandomAccessFile>
30+
</Appenders>
31+
32+
<Loggers>
33+
<AsyncLogger name="gr.codelearn" level="debug" additivity="false">
34+
<AppenderRef ref="ConsoleAppender"/>
35+
<AppenderRef ref="RollingRandomAccessFileAppender"/>
36+
</AsyncLogger>
37+
38+
<Root level="info">
39+
<AppenderRef ref="ConsoleAppender"/>
40+
<AppenderRef ref="RollingRandomAccessFileAppender"/>
41+
</Root>
42+
</Loggers>
43+
</Configuration>

‎pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<module>java-path-core-oop</module>
2929
<module>java-path-core-databases</module>
3030
<module>java-path-core-exceptions</module>
31+
<module>java-path-core-despattern</module>
32+
<module>java-path-core.generics</module>
3133
</modules>
3234

3335
<!-- Properties/Variables -->

0 commit comments

Comments
 (0)
Please sign in to comment.