Skip to content

Commit

Permalink
Adding more examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
algogrit committed Jul 2, 2019
1 parent bbb1e1e commit 030c509
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 15 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algogrit.java;

public class DieselEngine implements Engine {
public String getFuelType() {
return "Diesel";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algogrit.java;

public class ElectricEngine implements Engine {
public String getFuelType() {
return "Electricity";
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.algogrit.java;

public interface Engine {
String getFuelType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.algogrit.java;

// PetrolEngine
// ElectricEngine

public class MotorCycle implements Vehicle {
public void runsOn() {
System.out.println("MotorCycle runs on: ");
}
}
Original file line number Diff line number Diff line change
@@ -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: ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.algogrit.java;

public interface Vehicle {
void runsOn();
}
75 changes: 75 additions & 0 deletions code-samples/03-Design-Patterns/10-template/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>template</artifactId>
<version>1.0-SNAPSHOT</version>

<name>template</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,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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.algogrit.java;

public class ConcreteHouse extends House {
@Override
public void layWalls() {
System.out.println("Lay concrete walls");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.algogrit.java;

public class GlassHouse extends House {
@Override
public void layWalls() {
System.out.println("Lay glass walls");
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
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 );
}
}
25 changes: 25 additions & 0 deletions code-samples/03-Design-Patterns/11-iterator/iterator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<conelement value="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<src_description expected_position="2">
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="0" />
<src_folder value="file://$MODULE_DIR$/src/test/java" expected_position="1" />
<src_folder value="file://$MODULE_DIR$/target/generated-sources/annotations" expected_position="4" />
<src_folder value="file://$MODULE_DIR$/target/generated-test-sources/test-annotations" expected_position="5" />
</src_description>
</component>
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/target/classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-test-sources/test-annotations" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="library" name="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" level="application" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
75 changes: 75 additions & 0 deletions code-samples/03-Design-Patterns/11-iterator/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>iterator</artifactId>
<version>1.0-SNAPSHOT</version>

<name>iterator</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.8</maven.compiler.source>
<maven.compiler.target>1.8</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,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);
}
}
}
Loading

0 comments on commit 030c509

Please sign in to comment.