Skip to content

Commit

Permalink
Adding design pattern examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
algogrit committed Jul 2, 2019
1 parent 6bfb096 commit bbb1e1e
Show file tree
Hide file tree
Showing 19 changed files with 475 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ Existing
hs_err_pid*

# End of https://www.gitignore.io/api/java

target/
75 changes: 75 additions & 0 deletions code-samples/03-Design-Patterns/01-singleton/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.algogrit.java</groupId>
<artifactId>singleton</artifactId>
<version>1.0-SNAPSHOT</version>

<name>singleton</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.algogrit.java;

import java.util.ArrayList;
import java.util.List;

public class PrinterDriver {
private List<Object> queue = new ArrayList<Object>();

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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}
75 changes: 75 additions & 0 deletions code-samples/03-Design-Patterns/02-factory-method/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.algogrit.java</groupId>
<artifactId>factory-method</artifactId>
<version>1.0-SNAPSHOT</version>

<name>factory-method</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.algogrit.java;

public class CivilEngineer implements Engineer {
@Override
public void work() {
System.out.println("Does Construction");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algogrit.java;

public class CivilEngineerFactory implements EngineerFactory {
public Engineer getEngineer() {
return new CivilEngineer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.algogrit.java;

public class ComputerEngineer implements Engineer {
@Override
public void work() {
System.out.println("Does Software");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algogrit.java;

public class ComputerEngineerFactory implements EngineerFactory {
public Engineer getEngineer() {
return new ComputerEngineer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.algogrit.java;

public interface Engineer {
public void work();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.algogrit.java;

interface EngineerFactory {
Engineer getEngineer();
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Loading

0 comments on commit bbb1e1e

Please sign in to comment.