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 deleted file mode 100644 index f1a9a35..0000000 --- a/code-samples/03-Design-Patterns/03-strategy/src/main/java/com/algogrit/java/App.java +++ /dev/null @@ -1,13 +0,0 @@ -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/pom.xml b/code-samples/03-Design-Patterns/06-bridge/pom.xml similarity index 100% rename from code-samples/03-Design-Patterns/03-strategy/pom.xml rename to code-samples/03-Design-Patterns/06-bridge/pom.xml diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..b966add --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,17 @@ +package com.algogrit.java; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + // System.out.println( "Hello World!" ); + + Vehicle dieselCar = new Car(new DieselEngine()); + + dieselCar.runsOn(); + } +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Car.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Car.java new file mode 100644 index 0000000..95dfe66 --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Car.java @@ -0,0 +1,22 @@ +package com.algogrit.java; + +// PetrolEngine +// DieselEngine +// HybridEngine +// ElectricEngine + +public class Car implements Vehicle { + Engine engine; + + public Car() { + this.engine = new ElectricEngine(); + } + + public Car(Engine engine) { + this.engine = engine; + } + + public void runsOn() { + System.out.println("Car runs on: " + engine.getFuelType()); + } +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/DieselEngine.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/DieselEngine.java new file mode 100644 index 0000000..e98aff9 --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/DieselEngine.java @@ -0,0 +1,7 @@ +package com.algogrit.java; + +public class DieselEngine implements Engine { + public String getFuelType() { + return "Diesel"; + }; +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/ElectricEngine.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/ElectricEngine.java new file mode 100644 index 0000000..b1ba2d5 --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/ElectricEngine.java @@ -0,0 +1,7 @@ +package com.algogrit.java; + +public class ElectricEngine implements Engine { + public String getFuelType() { + return "Electricity"; + }; +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Engine.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Engine.java new file mode 100644 index 0000000..0b20d2f --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Engine.java @@ -0,0 +1,5 @@ +package com.algogrit.java; + +public interface Engine { + String getFuelType(); +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/MotorCycle.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/MotorCycle.java new file mode 100644 index 0000000..776aba0 --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/MotorCycle.java @@ -0,0 +1,10 @@ +package com.algogrit.java; + +// PetrolEngine +// ElectricEngine + +public class MotorCycle implements Vehicle { + public void runsOn() { + System.out.println("MotorCycle runs on: "); + } +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Truck.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Truck.java new file mode 100644 index 0000000..dd4412f --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Truck.java @@ -0,0 +1,12 @@ +package com.algogrit.java; + +// DieselEngine +// ElectricEngine + +public class Truck implements Vehicle { + Engine engine; + + public void runsOn() { + System.out.println("Truck runs on: "); + } +} diff --git a/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Vehicle.java b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Vehicle.java new file mode 100644 index 0000000..1ed0137 --- /dev/null +++ b/code-samples/03-Design-Patterns/06-bridge/src/main/java/com/algogrit/java/Vehicle.java @@ -0,0 +1,5 @@ +package com.algogrit.java; + +public interface Vehicle { + void runsOn(); +} diff --git a/code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/06-bridge/src/test/java/com/algogrit/java/AppTest.java similarity index 100% rename from code-samples/03-Design-Patterns/03-strategy/src/test/java/com/algogrit/java/AppTest.java rename to code-samples/03-Design-Patterns/06-bridge/src/test/java/com/algogrit/java/AppTest.java diff --git a/code-samples/03-Design-Patterns/10-template/pom.xml b/code-samples/03-Design-Patterns/10-template/pom.xml new file mode 100644 index 0000000..fe0aacd --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.algogrit.java + template + 1.0-SNAPSHOT + + template + + 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/10-template/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..3a35579 --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,17 @@ +package com.algogrit.java; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + House glassHouse = new GlassHouse(); + glassHouse.build(); + + House concreteHouse = new GlassHouse(); + concreteHouse.build(); + } +} diff --git a/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/ConcreteHouse.java b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/ConcreteHouse.java new file mode 100644 index 0000000..3e6096e --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/ConcreteHouse.java @@ -0,0 +1,8 @@ +package com.algogrit.java; + +public class ConcreteHouse extends House { + @Override + public void layWalls() { + System.out.println("Lay concrete walls"); + } +} diff --git a/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/GlassHouse.java b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/GlassHouse.java new file mode 100644 index 0000000..afe6534 --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/GlassHouse.java @@ -0,0 +1,8 @@ +package com.algogrit.java; + +public class GlassHouse extends House { + @Override + public void layWalls() { + System.out.println("Lay glass walls"); + } +} diff --git a/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/House.java b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/House.java new file mode 100644 index 0000000..db6d3df --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/src/main/java/com/algogrit/java/House.java @@ -0,0 +1,13 @@ +package com.algogrit.java; + +public abstract class House { + public void build() { + System.out.println("Laying foundation..."); + this.layWalls(); + System.out.println("Lay the roof"); + System.out.println("Place windows and doors"); + System.out.println("Finish with interiors"); + }; + + abstract void layWalls(); +} diff --git a/code-samples/03-Design-Patterns/10-template/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/10-template/src/test/java/com/algogrit/java/AppTest.java new file mode 100644 index 0000000..bf10580 --- /dev/null +++ b/code-samples/03-Design-Patterns/10-template/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/11-iterator/iterator.iml b/code-samples/03-Design-Patterns/11-iterator/iterator.iml new file mode 100644 index 0000000..b6f4b24 --- /dev/null +++ b/code-samples/03-Design-Patterns/11-iterator/iterator.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/code-samples/03-Design-Patterns/11-iterator/pom.xml b/code-samples/03-Design-Patterns/11-iterator/pom.xml new file mode 100644 index 0000000..df7bdd7 --- /dev/null +++ b/code-samples/03-Design-Patterns/11-iterator/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.algogrit.java + iterator + 1.0-SNAPSHOT + + iterator + + http://www.example.com + + + UTF-8 + 1.8 + 1.8 + + + + + 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/11-iterator/src/main/java/com/algogrit/java/App.java b/code-samples/03-Design-Patterns/11-iterator/src/main/java/com/algogrit/java/App.java new file mode 100644 index 0000000..49b40e2 --- /dev/null +++ b/code-samples/03-Design-Patterns/11-iterator/src/main/java/com/algogrit/java/App.java @@ -0,0 +1,20 @@ +package com.algogrit.java; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + IntegerBox box = new IntegerBox(); + + for (int i = 0; i < 10; i++) { + box.add(i); + } + for (Integer value : box) { + System.out.println(value); + } + } +} diff --git a/code-samples/03-Design-Patterns/11-iterator/src/main/java/com/algogrit/java/IntegerBox.java b/code-samples/03-Design-Patterns/11-iterator/src/main/java/com/algogrit/java/IntegerBox.java new file mode 100644 index 0000000..d7258aa --- /dev/null +++ b/code-samples/03-Design-Patterns/11-iterator/src/main/java/com/algogrit/java/IntegerBox.java @@ -0,0 +1,22 @@ +package com.algogrit.java; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +class IntegerBox implements Iterable { + private final List list = new ArrayList<>(); + + public void add(int in) { + list.add(in); + } + + public List getData() { + return list; + } + + @Override + public Iterator iterator() { + return list.iterator(); + } +} diff --git a/code-samples/03-Design-Patterns/11-iterator/src/test/java/com/algogrit/java/AppTest.java b/code-samples/03-Design-Patterns/11-iterator/src/test/java/com/algogrit/java/AppTest.java new file mode 100644 index 0000000..bf10580 --- /dev/null +++ b/code-samples/03-Design-Patterns/11-iterator/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/README.md similarity index 78% rename from code-samples/03-Design-Patterns/notes.md rename to code-samples/03-Design-Patterns/README.md index 90d62ee..1e6c3e5 100644 --- a/code-samples/03-Design-Patterns/notes.md +++ b/code-samples/03-Design-Patterns/README.md @@ -1,11 +1,11 @@ # Design Patterns -Java Design Patterns: https://java-design-patterns.com/patterns/factory-method/ +Java Design Patterns: https://java-design-patterns.com/ ```bash mvn archetype:generate \ -DgroupId=com.algogrit.java \ --DartifactId=strategy \ +-DartifactId=iterator \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false \ -DarchetypeVersion=1.4