From bbb1e1e335489021f8ef57fc3c194adfb122f53f Mon Sep 17 00:00:00 2001 From: Gaurav Agarwal Date: Tue, 2 Jul 2019 11:41:37 +0530 Subject: [PATCH] Adding design pattern examples. --- .gitignore | 2 + .../03-Design-Patterns/01-singleton/pom.xml | 75 +++++++++++++++++++ .../src/main/java/com/algogrit/java/App.java | 27 +++++++ .../java/com/algogrit/java/PrinterDriver.java | 27 +++++++ .../java/ThreadSafeLazyLoadedIvoryTower.java | 51 +++++++++++++ .../test/java/com/algogrit/java/AppTest.java | 20 +++++ .../02-factory-method/pom.xml | 75 +++++++++++++++++++ .../src/main/java/com/algogrit/java/App.java | 14 ++++ .../java/com/algogrit/java/CivilEngineer.java | 8 ++ .../algogrit/java/CivilEngineerFactory.java | 7 ++ .../com/algogrit/java/ComputerEngineer.java | 8 ++ .../java/ComputerEngineerFactory.java | 7 ++ .../main/java/com/algogrit/java/Engineer.java | 5 ++ .../com/algogrit/java/EngineerFactory.java | 5 ++ .../test/java/com/algogrit/java/AppTest.java | 20 +++++ .../03-Design-Patterns/03-strategy/pom.xml | 75 +++++++++++++++++++ .../src/main/java/com/algogrit/java/App.java | 13 ++++ .../test/java/com/algogrit/java/AppTest.java | 20 +++++ code-samples/03-Design-Patterns/notes.md | 16 ++++ 19 files changed, 475 insertions(+) create mode 100644 code-samples/03-Design-Patterns/01-singleton/pom.xml create mode 100644 code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/App.java create mode 100644 code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/PrinterDriver.java create mode 100644 code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/ThreadSafeLazyLoadedIvoryTower.java create mode 100644 code-samples/03-Design-Patterns/01-singleton/src/test/java/com/algogrit/java/AppTest.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/pom.xml create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/App.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineer.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineerFactory.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineer.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineerFactory.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/Engineer.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/EngineerFactory.java create mode 100644 code-samples/03-Design-Patterns/02-factory-method/src/test/java/com/algogrit/java/AppTest.java create mode 100644 code-samples/03-Design-Patterns/03-strategy/pom.xml create mode 100644 code-samples/03-Design-Patterns/03-strategy/src/main/java/com/algogrit/java/App.java create mode 100644 code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java create mode 100644 code-samples/03-Design-Patterns/notes.md diff --git a/.gitignore b/.gitignore index ad1211e..103401f 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ Existing hs_err_pid* # End of https://www.gitignore.io/api/java + +target/ diff --git a/code-samples/03-Design-Patterns/01-singleton/pom.xml b/code-samples/03-Design-Patterns/01-singleton/pom.xml new file mode 100644 index 0000000..22e612c --- /dev/null +++ b/code-samples/03-Design-Patterns/01-singleton/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.algogrit.java + singleton + 1.0-SNAPSHOT + + singleton + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..bc14035 --- /dev/null +++ b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,27 @@ +package com.algogrit.java; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + // printerDriver.print("Hello World!"); + + System.out.println("Creating instance..."); + PrinterDriver printerDriver1 = PrinterDriver.getInstance(); + + System.out.println("Fetching instance..."); + PrinterDriver printerDriver2 = PrinterDriver.getInstance(); + + // ThreadSafeLazyLoadedIvoryTower ivoryTower = new ThreadSafeLazyLoadedIvoryTower(); + + // printerDriver.print("Hello World!"); + + System.out.println(PrinterDriver.class); + + System.out.println("Hello world!"); + } +} diff --git a/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/PrinterDriver.java b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/PrinterDriver.java new file mode 100644 index 0000000..04ff2e5 --- /dev/null +++ b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/PrinterDriver.java @@ -0,0 +1,27 @@ +package com.algogrit.java; + +import java.util.ArrayList; +import java.util.List; + +public class PrinterDriver { + private List queue = new ArrayList(); + + private static PrinterDriver INSTANCE = new PrinterDriver(); + + public static PrinterDriver getInstance() { + return INSTANCE; + } + + private PrinterDriver() { + System.out.println("Instance created!"); + } + + public void print(Object o) { + this.queue.add(o); + lazyPrint(); + } + + public void lazyPrint() { + System.out.println("I will lazily print the objects"); + } +} diff --git a/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/ThreadSafeLazyLoadedIvoryTower.java b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/ThreadSafeLazyLoadedIvoryTower.java new file mode 100644 index 0000000..582e649 --- /dev/null +++ b/code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/ThreadSafeLazyLoadedIvoryTower.java @@ -0,0 +1,51 @@ +/** + * The MIT License Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.algogrit.java; + +/** + * Thread-safe Singleton class. The instance is lazily initialized and thus needs synchronization + * mechanism. + * + * Note: if created by reflection then a singleton will not be created but multiple options in the + * same classloader + */ +public final class ThreadSafeLazyLoadedIvoryTower { + + private static ThreadSafeLazyLoadedIvoryTower instance; + + private ThreadSafeLazyLoadedIvoryTower() { + // protect against instantiation via reflection + if (instance == null) { + instance = this; + } else { + throw new IllegalStateException("Already initialized."); + } + } + + /** + * The instance gets created only when it is called for first time. Lazy-loading + */ + public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() { + if (instance == null) { + instance = new ThreadSafeLazyLoadedIvoryTower(); + } + + return instance; + } +} diff --git a/code-samples/03-Design-Patterns/01-singleton/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/01-singleton/src/test/java/com/algogrit/java/AppTest.java new file mode 100644 index 0000000..bf10580 --- /dev/null +++ b/code-samples/03-Design-Patterns/01-singleton/src/test/java/com/algogrit/java/AppTest.java @@ -0,0 +1,20 @@ +package com.algogrit.java; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/pom.xml b/code-samples/03-Design-Patterns/02-factory-method/pom.xml new file mode 100644 index 0000000..0decc42 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.algogrit.java + factory-method + 1.0-SNAPSHOT + + factory-method + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..a8347d7 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,14 @@ +package com.algogrit.java; + +/** +* Hello world! +* +*/ +public class App { + public static void main(String[] args) { + EngineerFactory engineerFactory = new CivilEngineerFactory(); + + Engineer engineer = engineerFactory.getEngineer(); + engineer.work(); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineer.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineer.java new file mode 100644 index 0000000..9f041f1 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineer.java @@ -0,0 +1,8 @@ +package com.algogrit.java; + +public class CivilEngineer implements Engineer { + @Override + public void work() { + System.out.println("Does Construction"); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineerFactory.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineerFactory.java new file mode 100644 index 0000000..bbb2ee1 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineerFactory.java @@ -0,0 +1,7 @@ +package com.algogrit.java; + +public class CivilEngineerFactory implements EngineerFactory { + public Engineer getEngineer() { + return new CivilEngineer(); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineer.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineer.java new file mode 100644 index 0000000..be3762b --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineer.java @@ -0,0 +1,8 @@ +package com.algogrit.java; + +public class ComputerEngineer implements Engineer { + @Override + public void work() { + System.out.println("Does Software"); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineerFactory.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineerFactory.java new file mode 100644 index 0000000..9a2c1f0 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineerFactory.java @@ -0,0 +1,7 @@ +package com.algogrit.java; + +public class ComputerEngineerFactory implements EngineerFactory { + public Engineer getEngineer() { + return new ComputerEngineer(); + } +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/Engineer.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/Engineer.java new file mode 100644 index 0000000..af3a5c0 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/Engineer.java @@ -0,0 +1,5 @@ +package com.algogrit.java; + +public interface Engineer { + public void work(); +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/EngineerFactory.java b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/EngineerFactory.java new file mode 100644 index 0000000..c67b8bc --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/EngineerFactory.java @@ -0,0 +1,5 @@ +package com.algogrit.java; + +interface EngineerFactory { + Engineer getEngineer(); +} diff --git a/code-samples/03-Design-Patterns/02-factory-method/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/02-factory-method/src/test/java/com/algogrit/java/AppTest.java new file mode 100644 index 0000000..bf10580 --- /dev/null +++ b/code-samples/03-Design-Patterns/02-factory-method/src/test/java/com/algogrit/java/AppTest.java @@ -0,0 +1,20 @@ +package com.algogrit.java; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/code-samples/03-Design-Patterns/03-strategy/pom.xml b/code-samples/03-Design-Patterns/03-strategy/pom.xml new file mode 100644 index 0000000..94dbc6f --- /dev/null +++ b/code-samples/03-Design-Patterns/03-strategy/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.algogrit.java + strategy + 1.0-SNAPSHOT + + strategy + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/code-samples/03-Design-Patterns/03-strategy/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/03-strategy/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..f1a9a35 --- /dev/null +++ b/code-samples/03-Design-Patterns/03-strategy/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,13 @@ +package com.algogrit.java; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java new file mode 100644 index 0000000..bf10580 --- /dev/null +++ b/code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java @@ -0,0 +1,20 @@ +package com.algogrit.java; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/code-samples/03-Design-Patterns/notes.md b/code-samples/03-Design-Patterns/notes.md new file mode 100644 index 0000000..90d62ee --- /dev/null +++ b/code-samples/03-Design-Patterns/notes.md @@ -0,0 +1,16 @@ +# Design Patterns + +Java Design Patterns: https://java-design-patterns.com/patterns/factory-method/ + +```bash +mvn archetype:generate \ +-DgroupId=com.algogrit.java \ +-DartifactId=strategy \ +-DarchetypeArtifactId=maven-archetype-quickstart \ +-DinteractiveMode=false \ +-DarchetypeVersion=1.4 +``` + +Hello World Design Patterns: https://github.com/code4craft/hello-design-pattern + +Abuse: https://taskinoor.wordpress.com/2011/09/21/the-abuse-of-design-patterns-in-writing-a-hello-world-program/