-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ Existing | |
hs_err_pid* | ||
|
||
# End of https://www.gitignore.io/api/java | ||
|
||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
27 changes: 27 additions & 0 deletions
27
code-samples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...amples/03-Design-Patterns/01-singleton/src/main/java/com/algogrit/java/PrinterDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...Patterns/01-singleton/src/main/java/com/algogrit/java/ThreadSafeLazyLoadedIvoryTower.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
code-samples/03-Design-Patterns/01-singleton/src/test/java/com/algogrit/java/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
code-samples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...s/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...sign-Patterns/02-factory-method/src/main/java/com/algogrit/java/CivilEngineerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...3-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...n-Patterns/02-factory-method/src/main/java/com/algogrit/java/ComputerEngineerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...amples/03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/Engineer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.algogrit.java; | ||
|
||
public interface Engineer { | ||
public void work(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...03-Design-Patterns/02-factory-method/src/main/java/com/algogrit/java/EngineerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.algogrit.java; | ||
|
||
interface EngineerFactory { | ||
Engineer getEngineer(); | ||
} |
20 changes: 20 additions & 0 deletions
20
...samples/03-Design-Patterns/02-factory-method/src/test/java/com/algogrit/java/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
Oops, something went wrong.