Merge branch 'javadoc'
This commit is contained in:
commit
cbab406e47
@ -1,29 +1,37 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The essence of the Abstract Factory pattern is a factory interface
|
||||
* (KingdomFactory) and its implementations (ElfKingdomFactory,
|
||||
* OrcKingdomFactory).
|
||||
*
|
||||
* The example uses both concrete implementations to create a king, a castle and
|
||||
* an army.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
createKingdom(new ElfKingdomFactory());
|
||||
createKingdom(new OrcKingdomFactory());
|
||||
}
|
||||
|
||||
public static void createKingdom(KingdomFactory factory) {
|
||||
King king = factory.createKing();
|
||||
Castle castle = factory.createCastle();
|
||||
Army army = factory.createArmy();
|
||||
System.out.println("The kingdom was created.");
|
||||
System.out.println(king);
|
||||
System.out.println(castle);
|
||||
System.out.println(army);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The essence of the Abstract Factory pattern is a factory interface
|
||||
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory},
|
||||
* {@link OrcKingdomFactory}).
|
||||
* <p>
|
||||
* The example uses both concrete implementations to create a king, a castle and
|
||||
* an army.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
createKingdom(new ElfKingdomFactory());
|
||||
createKingdom(new OrcKingdomFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates kingdom
|
||||
* @param factory
|
||||
*/
|
||||
public static void createKingdom(KingdomFactory factory) {
|
||||
King king = factory.createKing();
|
||||
Castle castle = factory.createCastle();
|
||||
Army army = factory.createArmy();
|
||||
System.out.println("The kingdom was created.");
|
||||
System.out.println(king);
|
||||
System.out.println(castle);
|
||||
System.out.println(army);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface Army {
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Army interface
|
||||
*
|
||||
*/
|
||||
public interface Army {
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface Castle {
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Castle interface
|
||||
*
|
||||
*/
|
||||
public interface Castle {
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven Army!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* ElfArmy
|
||||
*
|
||||
*/
|
||||
public class ElfArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven Army!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven castle!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* ElfCastle
|
||||
*
|
||||
*/
|
||||
public class ElfCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven castle!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class ElfKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven king!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* ElfKing
|
||||
*
|
||||
*/
|
||||
public class ElfKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Elven king!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Concrete factory.
|
||||
*
|
||||
*/
|
||||
public class ElfKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new ElfCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new ElfKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new ElfArmy();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* ElfKingdomFactory concrete factory.
|
||||
*
|
||||
*/
|
||||
public class ElfKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new ElfCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new ElfKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new ElfArmy();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public interface King {
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* King interface
|
||||
*
|
||||
*/
|
||||
public interface King {
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The factory interface.
|
||||
*
|
||||
*/
|
||||
public interface KingdomFactory {
|
||||
|
||||
Castle createCastle();
|
||||
|
||||
King createKing();
|
||||
|
||||
Army createArmy();
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* KingdomFactory factory interface.
|
||||
*
|
||||
*/
|
||||
public interface KingdomFactory {
|
||||
|
||||
Castle createCastle();
|
||||
|
||||
King createKing();
|
||||
|
||||
Army createArmy();
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish Army!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcArmy
|
||||
*
|
||||
*/
|
||||
public class OrcArmy implements Army {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish Army!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish castle!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcCastle
|
||||
*
|
||||
*/
|
||||
public class OrcCastle implements Castle {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orcish castle!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
public class OrcKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orc king!";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcKing
|
||||
*
|
||||
*/
|
||||
public class OrcKing implements King {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "This is the Orc king!";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Concrete factory.
|
||||
*
|
||||
*/
|
||||
public class OrcKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new OrcCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new OrcKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcKingdomFactory concrete factory.
|
||||
*
|
||||
*/
|
||||
public class OrcKingdomFactory implements KingdomFactory {
|
||||
|
||||
public Castle createCastle() {
|
||||
return new OrcCastle();
|
||||
}
|
||||
|
||||
public King createKing() {
|
||||
return new OrcKing();
|
||||
}
|
||||
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,13 +1,18 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.abstractfactory.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.abstractfactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.abstractfactory.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,25 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* There are two variations of the Adapter pattern: The class adapter implements
|
||||
* the adaptee's interface whereas the object adapter uses composition to
|
||||
* contain the adaptee in the adapter object. This example uses the object
|
||||
* adapter approach.
|
||||
*
|
||||
* The Adapter (GnomeEngineer) converts the interface of the target class
|
||||
* (GoblinGlider) into a suitable one expected by the client
|
||||
* (GnomeEngineeringManager).
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Engineer manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
}
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* There are two variations of the Adapter pattern: The class adapter implements
|
||||
* the adaptee's interface whereas the object adapter uses composition to
|
||||
* contain the adaptee in the adapter object. This example uses the object
|
||||
* adapter approach.
|
||||
* <p>
|
||||
* The Adapter ({@link GnomeEngineer}) converts the interface of the target class
|
||||
* ({@link GoblinGlider}) into a suitable one expected by the client
|
||||
* ({@link GnomeEngineeringManager}).
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Engineer manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device (GoblinGlider) into
|
||||
* Engineer interface expected by the client (GnomeEngineeringManager).
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineer implements Engineer {
|
||||
|
||||
private GoblinGlider glider;
|
||||
|
||||
public GnomeEngineer() {
|
||||
glider = new GoblinGlider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
glider.attachGlider();
|
||||
glider.gainSpeed();
|
||||
glider.takeOff();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into
|
||||
* {@link Engineer} interface expected by the client ({@link GnomeEngineeringManager}).
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineer implements Engineer {
|
||||
|
||||
private GoblinGlider glider;
|
||||
|
||||
public GnomeEngineer() {
|
||||
glider = new GoblinGlider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
glider.attachGlider();
|
||||
glider.gainSpeed();
|
||||
glider.takeOff();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,20 +1,20 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* GnomeEngineering manager uses Engineer to operate devices.
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineeringManager implements Engineer {
|
||||
|
||||
private Engineer engineer;
|
||||
|
||||
public GnomeEngineeringManager() {
|
||||
engineer = new GnomeEngineer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
engineer.operateDevice();
|
||||
}
|
||||
}
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* GnomeEngineering manager uses {@link Engineer} to operate devices.
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineeringManager implements Engineer {
|
||||
|
||||
private Engineer engineer;
|
||||
|
||||
public GnomeEngineeringManager() {
|
||||
engineer = new GnomeEngineer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
engineer.operateDevice();
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.adapter.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.adapter.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -3,29 +3,24 @@ package com.iluwatar.async.method.invocation;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This application demonstrates the async method invocation pattern. Key parts of the pattern are
|
||||
* <code>AsyncResult</code> which is an intermediate container for an asynchronously evaluated value,
|
||||
* <code>AsyncCallback</code> which can be provided to be executed on task completion and
|
||||
* <code>AsyncExecutor</code> that manages the execution of the async tasks.
|
||||
* </p>
|
||||
* <p>
|
||||
* The main method shows example flow of async invocations. The main thread starts multiple tasks with
|
||||
* variable durations and then continues its own work. When the main thread has done it's job it collects
|
||||
* the results of the async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are
|
||||
* executed immediately when the tasks complete.
|
||||
* </p>
|
||||
* <p>
|
||||
* Noteworthy difference of thread usage between the async results and callbacks is that the async results
|
||||
* are collected in the main thread but the callbacks are executed within the worker threads. This should be
|
||||
* noted when working with thread pools.
|
||||
* </p>
|
||||
* <p>
|
||||
* Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture
|
||||
* and ExecutorService are the real world implementations of this pattern. But due to the nature of parallel
|
||||
* programming, the implementations are not trivial. This example does not take all possible scenarios into
|
||||
* account but rather provides a simple version that helps to understand the pattern.
|
||||
* </p>
|
||||
*
|
||||
* @see AsyncResult
|
||||
* @see AsyncCallback
|
||||
|
@ -2,6 +2,13 @@ package com.iluwatar.async.method.invocation;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* AsyncCallback interface
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
*/
|
||||
public interface AsyncCallback<T> {
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,11 @@ package com.iluwatar.async.method.invocation;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
*
|
||||
* AsyncExecutor interface
|
||||
*
|
||||
*/
|
||||
public interface AsyncExecutor {
|
||||
|
||||
/**
|
||||
|
@ -2,6 +2,12 @@ package com.iluwatar.async.method.invocation;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
*
|
||||
* AsyncResult interface
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public interface AsyncResult<T> {
|
||||
|
||||
/**
|
||||
|
@ -6,7 +6,9 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
*
|
||||
* Implementation of async executor that creates a new thread for every task.
|
||||
*
|
||||
*/
|
||||
public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
|
||||
|
@ -2,6 +2,11 @@ package com.iluwatar.async.method.invocation;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 52 KiB |
@ -1,18 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.bridge.FlyingMagicWeaponImp" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeaponImp.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="192" x="193" y="297"/>
|
||||
<class id="1" language="java" name="com.iluwatar.bridge.FlyingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="515" y="591"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.bridge.SoulEatingMagicWeaponImp" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeaponImp.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="226" x="425" y="297"/>
|
||||
<class id="2" language="java" name="com.iluwatar.bridge.SoulEatingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="605"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -21,25 +21,25 @@
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.bridge.Stormbringer" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Stormbringer.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="160" width="125" x="425" y="442"/>
|
||||
<position height="-1" width="-1" x="791" y="788"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.bridge.MagicWeaponImp" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/MagicWeaponImp.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="141" width="149" x="221" y="79"/>
|
||||
<class id="4" language="java" name="com.iluwatar.bridge.MagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/MagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="433"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.bridge.BlindingMagicWeaponImp" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeaponImp.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="208" x="691" y="297"/>
|
||||
<class id="5" language="java" name="com.iluwatar.bridge.BlindingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1105" y="593"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -48,7 +48,7 @@
|
||||
</class>
|
||||
<class id="6" language="java" name="com.iluwatar.bridge.SoulEatingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="178" width="347" x="410" y="79"/>
|
||||
<position height="-1" width="-1" x="380" y="21"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -57,7 +57,7 @@
|
||||
</class>
|
||||
<class id="7" language="java" name="com.iluwatar.bridge.Excalibur" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Excalibur.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="160" width="124" x="691" y="442"/>
|
||||
<position height="-1" width="-1" x="1105" y="782"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -66,7 +66,7 @@
|
||||
</class>
|
||||
<class id="8" language="java" name="com.iluwatar.bridge.Mjollnir" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Mjollnir.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="160" width="124" x="193" y="442"/>
|
||||
<position height="-1" width="-1" x="515" y="788"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -75,7 +75,7 @@
|
||||
</class>
|
||||
<class id="9" language="java" name="com.iluwatar.bridge.BlindingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="178" width="313" x="797" y="79"/>
|
||||
<position height="-1" width="-1" x="791" y="14"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -84,7 +84,7 @@
|
||||
</class>
|
||||
<class id="10" language="java" name="com.iluwatar.bridge.MagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/MagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="159" width="221" x="644" y="-120"/>
|
||||
<position height="-1" width="-1" x="791" y="237"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -93,7 +93,7 @@
|
||||
</class>
|
||||
<class id="11" language="java" name="com.iluwatar.bridge.FlyingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="178" width="291" x="1150" y="79"/>
|
||||
<position height="-1" width="-1" x="1144" y="12"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
@ -101,48 +101,52 @@
|
||||
</display>
|
||||
</class>
|
||||
<generalization id="12">
|
||||
<end type="SOURCE" refId="11"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</generalization>
|
||||
<generalization id="13">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="14">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="15">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<generalization id="16">
|
||||
<end type="SOURCE" refId="6"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<generalization id="17">
|
||||
<end type="SOURCE" refId="9"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<generalization id="18">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
<generalization id="15">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<association id="19">
|
||||
<association id="16">
|
||||
<end type="SOURCE" refId="10" navigable="false">
|
||||
<attribute id="20" name="imp"/>
|
||||
<multiplicity id="21" minimum="0" maximum="1"/>
|
||||
<attribute id="17" name="imp">
|
||||
<position height="0" width="0" x="478" y="284"/>
|
||||
</attribute>
|
||||
<multiplicity id="18" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="478" y="284"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<generalization id="22">
|
||||
<generalization id="19">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="20">
|
||||
<end type="SOURCE" refId="6"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<generalization id="21">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<generalization id="22">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</generalization>
|
||||
<generalization id="23">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
<end type="SOURCE" refId="11"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 85 KiB |
@ -11,7 +11,7 @@ tags: Java
|
||||
vary independently.
|
||||
|
||||
|
||||

|
||||

|
||||
|
||||
**Applicability:** Use the Bridge pattern when
|
||||
|
||||
|
@ -1,35 +1,39 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* In Bridge pattern both abstraction (MagicWeapon) and implementation
|
||||
* (MagicWeaponImp) have their own class hierarchies. The interface of the
|
||||
* implementations can be changed without affecting the clients.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(
|
||||
new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||
new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||
new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
|
||||
}
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* In Bridge pattern both abstraction ({@link MagicWeapon}) and implementation
|
||||
* ({@link MagicWeaponImpl}) have their own class hierarchies. The interface of the
|
||||
* implementations can be changed without affecting the clients.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(
|
||||
new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||
new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||
new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,38 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class BlindingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public BlindingMagicWeapon(BlindingMagicWeaponImp imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlindingMagicWeaponImp getImp() {
|
||||
return (BlindingMagicWeaponImp) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void blind() {
|
||||
getImp().blindImp();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* BlindingMagicWeapon
|
||||
*
|
||||
*/
|
||||
public class BlindingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public BlindingMagicWeapon(BlindingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlindingMagicWeaponImpl getImp() {
|
||||
return (BlindingMagicWeaponImpl) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void blind() {
|
||||
getImp().blindImp();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public abstract class BlindingMagicWeaponImp extends MagicWeaponImp {
|
||||
|
||||
public abstract void blindImp();
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* BlindingMagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class BlindingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
|
||||
public abstract void blindImp();
|
||||
|
||||
}
|
@ -1,26 +1,31 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class Excalibur extends BlindingMagicWeaponImp {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void blindImp() {
|
||||
System.out
|
||||
.println("bright light streams from Excalibur blinding the enemy");
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* Excalibur
|
||||
*
|
||||
*/
|
||||
public class Excalibur extends BlindingMagicWeaponImpl {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void blindImp() {
|
||||
System.out
|
||||
.println("bright light streams from Excalibur blinding the enemy");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,32 +1,37 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class FlyingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public FlyingMagicWeapon(FlyingMagicWeaponImp imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
public FlyingMagicWeaponImp getImp() {
|
||||
return (FlyingMagicWeaponImp) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void fly() {
|
||||
getImp().flyImp();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* FlyingMagicWeapon
|
||||
*
|
||||
*/
|
||||
public class FlyingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public FlyingMagicWeapon(FlyingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
public FlyingMagicWeaponImpl getImp() {
|
||||
return (FlyingMagicWeaponImpl) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void fly() {
|
||||
getImp().flyImp();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public abstract class FlyingMagicWeaponImp extends MagicWeaponImp {
|
||||
|
||||
public abstract void flyImp();
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* FlyingMagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class FlyingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
|
||||
public abstract void flyImp();
|
||||
|
||||
}
|
@ -1,26 +1,26 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* Abstraction interface.
|
||||
*
|
||||
*/
|
||||
public abstract class MagicWeapon {
|
||||
|
||||
protected MagicWeaponImp imp;
|
||||
|
||||
public MagicWeapon(MagicWeaponImp imp) {
|
||||
this.imp = imp;
|
||||
}
|
||||
|
||||
public abstract void wield();
|
||||
|
||||
public abstract void swing();
|
||||
|
||||
public abstract void unwield();
|
||||
|
||||
public MagicWeaponImp getImp() {
|
||||
return imp;
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* MagicWeapon
|
||||
*
|
||||
*/
|
||||
public abstract class MagicWeapon {
|
||||
|
||||
protected MagicWeaponImpl imp;
|
||||
|
||||
public MagicWeapon(MagicWeaponImpl imp) {
|
||||
this.imp = imp;
|
||||
}
|
||||
|
||||
public abstract void wield();
|
||||
|
||||
public abstract void swing();
|
||||
|
||||
public abstract void unwield();
|
||||
|
||||
public MagicWeaponImpl getImp() {
|
||||
return imp;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* Implementation interface.
|
||||
*
|
||||
*/
|
||||
public abstract class MagicWeaponImp {
|
||||
|
||||
public abstract void wieldImp();
|
||||
|
||||
public abstract void swingImp();
|
||||
|
||||
public abstract void unwieldImp();
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* MagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class MagicWeaponImpl {
|
||||
|
||||
public abstract void wieldImp();
|
||||
|
||||
public abstract void swingImp();
|
||||
|
||||
public abstract void unwieldImp();
|
||||
|
||||
}
|
@ -1,26 +1,31 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class Mjollnir extends FlyingMagicWeaponImp {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flyImp() {
|
||||
System.out
|
||||
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* Mjollnir
|
||||
*
|
||||
*/
|
||||
public class Mjollnir extends FlyingMagicWeaponImpl {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flyImp() {
|
||||
System.out
|
||||
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,33 +1,38 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class SoulEatingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImp imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SoulEatingMagicWeaponImp getImp() {
|
||||
return (SoulEatingMagicWeaponImp) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void eatSoul() {
|
||||
getImp().eatSoulImp();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* SoulEatingMagicWeapon
|
||||
*
|
||||
*/
|
||||
public class SoulEatingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SoulEatingMagicWeaponImpl getImp() {
|
||||
return (SoulEatingMagicWeaponImpl) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void eatSoul() {
|
||||
getImp().eatSoulImp();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public abstract class SoulEatingMagicWeaponImp extends MagicWeaponImp {
|
||||
|
||||
public abstract void eatSoulImp();
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* SoulEatingMagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class SoulEatingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
|
||||
public abstract void eatSoulImp();
|
||||
|
||||
}
|
@ -1,25 +1,30 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
public class Stormbringer extends SoulEatingMagicWeaponImp {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eatSoulImp() {
|
||||
System.out.println("Stormbringer devours the enemy's soul");
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* Stormbringer
|
||||
*
|
||||
*/
|
||||
public class Stormbringer extends SoulEatingMagicWeaponImpl {
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
System.out.println("wielding Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
System.out.println("swinging Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
System.out.println("unwielding Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eatSoulImp() {
|
||||
System.out.println("Stormbringer devours the enemy's soul");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.bridge.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.bridge.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,42 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import com.iluwatar. builder.Hero.HeroBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||
* Effective Java 2nd Edition.
|
||||
*
|
||||
* We want to build Hero objects, but its construction is complex because of the
|
||||
* many parameters needed. To aid the user we introduce HeroBuilder class.
|
||||
* HeroBuilder takes the minimum parameters to build Hero object in its
|
||||
* constructor. After that additional configuration for the Hero object can be
|
||||
* done using the fluent HeroBuilder interface. When configuration is ready the
|
||||
* build method is called to receive the final Hero object.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD).build();
|
||||
System.out.println(warrior);
|
||||
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import com.iluwatar. builder.Hero.HeroBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* This is the Builder pattern variation as described by Joshua Bloch in
|
||||
* Effective Java 2nd Edition.
|
||||
* <p>
|
||||
* We want to build {@link Hero} objects, but its construction is complex because of the
|
||||
* many parameters needed. To aid the user we introduce {@link HeroBuilder} class.
|
||||
* {@link HeroBuilder} takes the minimum parameters to build {@link Hero} object in its
|
||||
* constructor. After that additional configuration for the {@link Hero} object can be
|
||||
* done using the fluent {@link HeroBuilder} interface. When configuration is ready the
|
||||
* build method is called to receive the final {@link Hero} object.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard")
|
||||
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER)
|
||||
.build();
|
||||
System.out.println(mage);
|
||||
|
||||
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill")
|
||||
.withHairColor(HairColor.BLOND)
|
||||
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL)
|
||||
.withWeapon(Weapon.SWORD).build();
|
||||
System.out.println(warrior);
|
||||
|
||||
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond")
|
||||
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Armor {
|
||||
|
||||
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
|
||||
|
||||
private String title;
|
||||
|
||||
Armor(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Armor enumeration
|
||||
*
|
||||
*/
|
||||
public enum Armor {
|
||||
|
||||
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
|
||||
|
||||
private String title;
|
||||
|
||||
Armor(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum HairColor {
|
||||
|
||||
WHITE, BLOND, RED, BROWN, BLACK;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* HairColor enumeration
|
||||
*
|
||||
*/
|
||||
public enum HairColor {
|
||||
|
||||
WHITE, BLOND, RED, BROWN, BLACK;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,17 +1,22 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum HairType {
|
||||
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
|
||||
|
||||
private String title;
|
||||
|
||||
HairType(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* HairType enumeration
|
||||
*
|
||||
*/
|
||||
public enum HairType {
|
||||
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
|
||||
|
||||
private String title;
|
||||
|
||||
HairType(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* The class with many parameters.
|
||||
* Hero, the class with many parameters.
|
||||
*
|
||||
*/
|
||||
public class Hero {
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Profession {
|
||||
|
||||
WARRIOR, THIEF, MAGE, PRIEST;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Profession enumeration
|
||||
*
|
||||
*/
|
||||
public enum Profession {
|
||||
|
||||
WARRIOR, THIEF, MAGE, PRIEST;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
public enum Weapon {
|
||||
|
||||
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
/**
|
||||
*
|
||||
* Weapon enumeration
|
||||
*
|
||||
*/
|
||||
public enum Weapon {
|
||||
|
||||
DAGGER, SWORD, AXE, WARHAMMER, BOW;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar. builder.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.builder;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar. builder.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -2,21 +2,25 @@ package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Business Delegate pattern adds an abstraction layer between presentation and business tiers.
|
||||
* The Business Delegate pattern adds an abstraction layer between the presentation and business tiers.
|
||||
* By using the pattern we gain loose coupling between the tiers. The Business Delegate encapsulates
|
||||
* knowledge about how to locate, connect to, and interact with the business objects that make up
|
||||
* the application.
|
||||
*
|
||||
* <p>
|
||||
* Some of the services the Business Delegate uses are instantiated directly, and some can be retrieved
|
||||
* through service lookups. The Business Delegate itself may contain business logic too potentially tying
|
||||
* together multiple service calls, exception handling, retrying etc.
|
||||
*
|
||||
* In this example the client (Client) utilizes a business delegate (BusinessDelegate) to execute a task.
|
||||
* <p>
|
||||
* In this example the client ({@link Client}) utilizes a business delegate ({@link BusinessDelegate}) to execute a task.
|
||||
* The Business Delegate then selects the appropriate service and makes the service call.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
BusinessDelegate businessDelegate = new BusinessDelegate();
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
*
|
||||
* BusinessDelegate separates presentation and business tiers
|
||||
* BusinessDelegate separates the presentation and business tiers
|
||||
*
|
||||
*/
|
||||
public class BusinessDelegate {
|
||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.business.delegate.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
@ -1,8 +1,10 @@
|
||||
package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
* Callback pattern is more native for functional languages where function is treated as first-class citizen.
|
||||
* Prior to Java8 can be simulated using simple (alike command) interfaces.
|
||||
*
|
||||
* Callback pattern is more native for functional languages where functions are treated as first-class citizens.
|
||||
* Prior to Java 8 callbacks can be simulated using simple (alike command) interfaces.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* Callback interface
|
||||
*
|
||||
*/
|
||||
public interface Callback {
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* Implementation of task that need to be executed
|
||||
*
|
||||
*/
|
||||
public class SimpleTask extends Task {
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* Template-method class for callback hook execution
|
||||
*
|
||||
*/
|
||||
public abstract class Task {
|
||||
|
||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.callback.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
@ -1,22 +1,26 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* Chain of Responsibility organizes request handlers (RequestHandler) into a
|
||||
* chain where each handler has a chance to act on the request on its turn. In
|
||||
* this example the king (OrcKing) makes requests and the military orcs
|
||||
* (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
OrcKing king = new OrcKing();
|
||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
||||
"torture prisoner"));
|
||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||
|
||||
}
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* Chain of Responsibility organizes request handlers ({@link RequestHandler}) into a
|
||||
* chain where each handler has a chance to act on the request on its turn. In
|
||||
* this example the king ({@link OrcKing}) makes requests and the military orcs
|
||||
* ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
OrcKing king = new OrcKing();
|
||||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
|
||||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER,
|
||||
"torture prisoner"));
|
||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,27 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public class OrcCommander extends RequestHandler {
|
||||
|
||||
public OrcCommander(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc commander";
|
||||
}
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcCommander
|
||||
*
|
||||
*/
|
||||
public class OrcCommander extends RequestHandler {
|
||||
|
||||
public OrcCommander(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc commander";
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* Makes requests that are handled by the chain.
|
||||
*
|
||||
*/
|
||||
public class OrcKing {
|
||||
|
||||
RequestHandler chain;
|
||||
|
||||
public OrcKing() {
|
||||
buildChain();
|
||||
}
|
||||
|
||||
private void buildChain() {
|
||||
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
||||
}
|
||||
|
||||
public void makeRequest(Request req) {
|
||||
chain.handleRequest(req);
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcKing makes requests that are handled by the chain.
|
||||
*
|
||||
*/
|
||||
public class OrcKing {
|
||||
|
||||
RequestHandler chain;
|
||||
|
||||
public OrcKing() {
|
||||
buildChain();
|
||||
}
|
||||
|
||||
private void buildChain() {
|
||||
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
||||
}
|
||||
|
||||
public void makeRequest(Request req) {
|
||||
chain.handleRequest(req);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,28 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public class OrcOfficer extends RequestHandler {
|
||||
|
||||
public OrcOfficer(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc officer";
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcOfficer
|
||||
*
|
||||
*/
|
||||
public class OrcOfficer extends RequestHandler {
|
||||
|
||||
public OrcOfficer(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc officer";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,22 +1,27 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public class OrcSoldier extends RequestHandler {
|
||||
|
||||
public OrcSoldier(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc soldier";
|
||||
}
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* OrcSoldier
|
||||
*
|
||||
*/
|
||||
public class OrcSoldier extends RequestHandler {
|
||||
|
||||
public OrcSoldier(RequestHandler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(Request req) {
|
||||
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
|
||||
printHandling(req);
|
||||
} else {
|
||||
super.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orc soldier";
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,38 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public class Request {
|
||||
|
||||
private String requestDescription;
|
||||
private RequestType requestType;
|
||||
|
||||
public Request(RequestType requestType, String requestDescription) {
|
||||
this.setRequestType(requestType);
|
||||
this.setRequestDescription(requestDescription);
|
||||
}
|
||||
|
||||
public String getRequestDescription() {
|
||||
return requestDescription;
|
||||
}
|
||||
|
||||
public void setRequestDescription(String requestDescription) {
|
||||
this.requestDescription = requestDescription;
|
||||
}
|
||||
|
||||
public RequestType getRequestType() {
|
||||
return requestType;
|
||||
}
|
||||
|
||||
public void setRequestType(RequestType requestType) {
|
||||
this.requestType = requestType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getRequestDescription();
|
||||
}
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* Request
|
||||
*
|
||||
*/
|
||||
public class Request {
|
||||
|
||||
private String requestDescription;
|
||||
private RequestType requestType;
|
||||
|
||||
public Request(RequestType requestType, String requestDescription) {
|
||||
this.setRequestType(requestType);
|
||||
this.setRequestDescription(requestDescription);
|
||||
}
|
||||
|
||||
public String getRequestDescription() {
|
||||
return requestDescription;
|
||||
}
|
||||
|
||||
public void setRequestDescription(String requestDescription) {
|
||||
this.requestDescription = requestDescription;
|
||||
}
|
||||
|
||||
public RequestType getRequestType() {
|
||||
return requestType;
|
||||
}
|
||||
|
||||
public void setRequestType(RequestType requestType) {
|
||||
this.requestType = requestType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getRequestDescription();
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,28 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public abstract class RequestHandler {
|
||||
|
||||
private RequestHandler next;
|
||||
|
||||
public RequestHandler(RequestHandler next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public void handleRequest(Request req) {
|
||||
if (next != null) {
|
||||
next.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
protected void printHandling(Request req) {
|
||||
System.out.println(this + " handling request \"" + req + "\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* RequestHandler
|
||||
*
|
||||
*/
|
||||
public abstract class RequestHandler {
|
||||
|
||||
private RequestHandler next;
|
||||
|
||||
public RequestHandler(RequestHandler next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public void handleRequest(Request req) {
|
||||
if (next != null) {
|
||||
next.handleRequest(req);
|
||||
}
|
||||
}
|
||||
|
||||
protected void printHandling(Request req) {
|
||||
System.out.println(this + " handling request \"" + req + "\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
public enum RequestType {
|
||||
|
||||
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* RequestType enumeration
|
||||
*
|
||||
*/
|
||||
public enum RequestType {
|
||||
|
||||
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.chain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.chain.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.chain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.chain.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,15 @@ package com.iluwatar.command;
|
||||
/**
|
||||
*
|
||||
* In Command pattern actions are objects that can be executed and undone.
|
||||
*
|
||||
* <p>
|
||||
* Four terms always associated with the command pattern are command, receiver, invoker and client. A command
|
||||
* object (spell) knows about receiver (target) and invokes a method of the receiver. Values for parameters of
|
||||
* object (spell) knows about the receiver (target) and invokes a method of the receiver. Values for parameters of
|
||||
* the receiver method are stored in the command. The receiver then does the work. An invoker object (wizard)
|
||||
* knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker
|
||||
* does not know anything about a concrete command, it knows only about command interface. Both an invoker object
|
||||
* and several command objects are held by a client object (app). The client decides which commands to execute at
|
||||
* which points. To execute a command, it passes the command object to the invoker object.
|
||||
*
|
||||
* <p>
|
||||
* In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of the previous
|
||||
* spells cast, so it is easy to undo them. In addition, the wizard keeps track of the spells undone, so they
|
||||
* can be redone.
|
||||
@ -20,6 +20,10 @@ package com.iluwatar.command;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.command;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.command.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.command;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.command.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,29 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
/**
|
||||
*
|
||||
* With Composite we can treat tree hierarchies of objects with uniform
|
||||
* interface (LetterComposite). In this example we have sentences composed of
|
||||
* words composed of letters.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Message from the orcs: ");
|
||||
|
||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||
orcMessage.print();
|
||||
|
||||
System.out.println("\n");
|
||||
|
||||
System.out.println("Message from the elves: ");
|
||||
|
||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||
elfMessage.print();
|
||||
}
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
/**
|
||||
*
|
||||
* With Composite we can treat tree hierarchies of objects with uniform
|
||||
* interface ({@link LetterComposite}). In this example we have sentences composed of
|
||||
* words composed of letters.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Message from the orcs: ");
|
||||
|
||||
LetterComposite orcMessage = new Messenger().messageFromOrcs();
|
||||
orcMessage.print();
|
||||
|
||||
System.out.println("\n");
|
||||
|
||||
System.out.println("Message from the elves: ");
|
||||
|
||||
LetterComposite elfMessage = new Messenger().messageFromElves();
|
||||
elfMessage.print();
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,26 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
public class Letter extends LetterComposite {
|
||||
|
||||
private char c;
|
||||
|
||||
public Letter(char c) {
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
System.out.print(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
/**
|
||||
*
|
||||
* Letter
|
||||
*
|
||||
*/
|
||||
public class Letter extends LetterComposite {
|
||||
|
||||
private char c;
|
||||
|
||||
public Letter(char c) {
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
System.out.print(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,53 +1,58 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Messenger {
|
||||
|
||||
LetterComposite messageFromOrcs() {
|
||||
|
||||
List<Word> words = new ArrayList<Word>();
|
||||
|
||||
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'),
|
||||
new Letter('i'), new Letter('p'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'),
|
||||
new Letter('y'))));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
LetterComposite messageFromElves() {
|
||||
|
||||
List<Word> words = new ArrayList<Word>();
|
||||
|
||||
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'),
|
||||
new Letter('c'), new Letter('h'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'),
|
||||
new Letter('n'), new Letter('d'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'),
|
||||
new Letter('u'), new Letter('r'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'),
|
||||
new Letter('o'), new Letter('m'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'),
|
||||
new Letter('u'), new Letter('r'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'),
|
||||
new Letter('u'), new Letter('t'), new Letter('h'))));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Messenger
|
||||
*
|
||||
*/
|
||||
public class Messenger {
|
||||
|
||||
LetterComposite messageFromOrcs() {
|
||||
|
||||
List<Word> words = new ArrayList<Word>();
|
||||
|
||||
words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'),
|
||||
new Letter('i'), new Letter('p'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'),
|
||||
new Letter('e'), new Letter('r'), new Letter('e'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('a'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'),
|
||||
new Letter('y'))));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
LetterComposite messageFromElves() {
|
||||
|
||||
List<Word> words = new ArrayList<Word>();
|
||||
|
||||
words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'),
|
||||
new Letter('c'), new Letter('h'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'),
|
||||
new Letter('n'), new Letter('d'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'),
|
||||
new Letter('u'), new Letter('r'), new Letter('s'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'),
|
||||
new Letter('o'), new Letter('m'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'),
|
||||
new Letter('u'), new Letter('r'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'),
|
||||
new Letter('u'), new Letter('t'), new Letter('h'))));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,28 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Sentence extends LetterComposite {
|
||||
|
||||
public Sentence(List<Word> words) {
|
||||
for (Word w : words) {
|
||||
this.add(w);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
// nop
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
System.out.print(".");
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Sentence
|
||||
*
|
||||
*/
|
||||
public class Sentence extends LetterComposite {
|
||||
|
||||
public Sentence(List<Word> words) {
|
||||
for (Word w : words) {
|
||||
this.add(w);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
// nop
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
System.out.print(".");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,28 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Word extends LetterComposite {
|
||||
|
||||
public Word(List<Letter> letters) {
|
||||
for (Letter l : letters) {
|
||||
this.add(l);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
System.out.print(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Word
|
||||
*
|
||||
*/
|
||||
public class Word extends LetterComposite {
|
||||
|
||||
public Word(List<Letter> letters) {
|
||||
for (Letter l : letters) {
|
||||
this.add(l);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisBefore() {
|
||||
System.out.print(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.composite.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.composite.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,47 +1,56 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
* interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
return customers;
|
||||
}
|
||||
}
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
* interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate customers
|
||||
* @return list of customers
|
||||
*/
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
return customers;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
/**
|
||||
*
|
||||
* Customer
|
||||
*
|
||||
*/
|
||||
public class Customer {
|
||||
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
@ -2,7 +2,13 @@ package com.iluwatar.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* CustomerDao
|
||||
*
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
public List<Customer> getAllCustomers();
|
||||
public Customer getCusterById(int id);
|
||||
public void addCustomer(Customer customer);
|
||||
|
@ -3,11 +3,13 @@ package com.iluwatar.dao;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
|
||||
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
|
||||
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
|
||||
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
|
||||
* database schema, etc.
|
||||
*
|
||||
*/
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.dao.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.dao.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,33 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* Decorator pattern is more flexible alternative to subclassing. The decorator
|
||||
* class implements the same interface as the target and uses composition to
|
||||
* "decorate" calls to the target.
|
||||
*
|
||||
* Using decorator pattern it is possible to change class behavior during
|
||||
* runtime, as the example shows.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// simple troll
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
|
||||
// change the behavior of the simple troll by adding a decorator
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(troll);
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
}
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* Decorator pattern is a more flexible alternative to subclassing. The decorator
|
||||
* class implements the same interface as the target and uses composition to
|
||||
* "decorate" calls to the target.
|
||||
* <p>
|
||||
* Using decorator pattern it is possible to change class behavior during
|
||||
* runtime, as the example shows.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// simple troll
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
|
||||
// change the behavior of the simple troll by adding a decorator
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(troll);
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
* SmartTroll is a decorator for Hostile objects.
|
||||
* The calls to the Hostile interface are intercepted
|
||||
* and decorated. Finally the calls are delegated
|
||||
* to the decorated Hostile object.
|
||||
*
|
||||
*/
|
||||
public class SmartTroll implements Hostile {
|
||||
|
||||
private Hostile decorated;
|
||||
|
||||
public SmartTroll(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("The troll throws a rock at you!");
|
||||
decorated.attack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll calls for help!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
* SmartTroll is a decorator for {@link Hostile} objects.
|
||||
* The calls to the {@link Hostile} interface are intercepted
|
||||
* and decorated. Finally the calls are delegated
|
||||
* to the decorated {@link Hostile} object.
|
||||
*
|
||||
*/
|
||||
public class SmartTroll implements Hostile {
|
||||
|
||||
private Hostile decorated;
|
||||
|
||||
public SmartTroll(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attack() {
|
||||
System.out.println("The troll throws a rock at you!");
|
||||
decorated.attack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll calls for help!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* Troll implements Hostile interface directly.
|
||||
*
|
||||
*/
|
||||
public class Troll implements Hostile {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("The troll swings at you with a club!");
|
||||
}
|
||||
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll shrieks in horror and runs away!");
|
||||
}
|
||||
|
||||
}
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* Troll implements {@link Hostile} interface directly.
|
||||
*
|
||||
*/
|
||||
public class Troll implements Hostile {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("The troll swings at you with a club!");
|
||||
}
|
||||
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll shrieks in horror and runs away!");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,19 @@
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.decorator.App;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
package com.iluwatar.decorator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.decorator.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -9,23 +9,27 @@ import com.google.inject.Injector;
|
||||
* implements so called inversion of control principle. Inversion of control has two specific rules:
|
||||
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
|
||||
* - Abstractions should not depend on details. Details should depend on abstractions.
|
||||
*
|
||||
* In this example we show you three different wizards. The first one (SimpleWizard) is a naive
|
||||
* <p>
|
||||
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a naive
|
||||
* implementation violating the inversion of control principle. It depends directly on a concrete
|
||||
* implementation which cannot be changed.
|
||||
*
|
||||
* The second wizard (AdvancedWizard) is more flexible. It does not depend on any concrete implementation
|
||||
* but abstraction. It utilizes Dependency Injection pattern allowing its Tobacco dependency to be
|
||||
* <p>
|
||||
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete implementation
|
||||
* but abstraction. It utilizes Dependency Injection pattern allowing its {@link Tobacco} dependency to be
|
||||
* injected through its constructor. This way, handling the dependency is no longer the wizard's
|
||||
* responsibility. It is resolved outside the wizard class.
|
||||
*
|
||||
* <p>
|
||||
* The third example takes the pattern a step further. It uses Guice framework for Dependency Injection.
|
||||
* TobaccoModule binds a concrete implementation to abstraction. Injector is then used to create
|
||||
* GuiceWizard object with correct dependencies.
|
||||
* {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then used to create
|
||||
* {@link GuiceWizard} object with correct dependencies.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main( String[] args ) {
|
||||
SimpleWizard simpleWizard = new SimpleWizard();
|
||||
simpleWizard.smoke();
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
/**
|
||||
*
|
||||
* OldTobyTobacco concrete Tobacco implementation
|
||||
* OldTobyTobacco concrete {@link Tobacco} implementation
|
||||
*
|
||||
*/
|
||||
public class OldTobyTobacco extends Tobacco {
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
/**
|
||||
*
|
||||
* RivendellTobacco concrete Tobacco implementation
|
||||
* RivendellTobacco concrete {@link Tobacco} implementation
|
||||
*
|
||||
*/
|
||||
public class RivendellTobacco extends Tobacco {
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
/**
|
||||
*
|
||||
* SecondBreakfastTobacco concrete Tobacco implementation
|
||||
* SecondBreakfastTobacco concrete {@link Tobacco} implementation
|
||||
*
|
||||
*/
|
||||
public class SecondBreakfastTobacco extends Tobacco {
|
||||
|
@ -4,7 +4,7 @@ import com.google.inject.AbstractModule;
|
||||
|
||||
/**
|
||||
*
|
||||
* Guice module for binding certain concrete Tobacco implementation.
|
||||
* Guice module for binding certain concrete {@link Tobacco} implementation.
|
||||
*
|
||||
*/
|
||||
public class TobaccoModule extends AbstractModule {
|
||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.dependency.injection.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
@ -5,13 +5,18 @@ import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
*
|
||||
* In Inventory we store the items with a given size. However, we do not store
|
||||
* In {@link Inventory} we store the items with a given size. However, we do not store
|
||||
* more items than the inventory size. To address concurrent access problems we
|
||||
* use double checked locking to add item to inventory. In this method, the
|
||||
* thread which gets the lock first adds the item.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final Inventory inventory = new Inventory(1000);
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
|
@ -5,6 +5,11 @@ import java.util.List;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
*
|
||||
* Inventory
|
||||
*
|
||||
*/
|
||||
public class Inventory {
|
||||
|
||||
private int inventorySize;
|
||||
|
@ -1,6 +1,12 @@
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
/**
|
||||
*
|
||||
* Item
|
||||
*
|
||||
*/
|
||||
public class Item {
|
||||
String name;
|
||||
int level;
|
||||
|
||||
private String name;
|
||||
private int level;
|
||||
}
|
||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.doublechecked.locking.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
@ -8,23 +8,27 @@ import java.util.List;
|
||||
* When a message with a parameter is sent to an object, the resultant behaviour is defined by the
|
||||
* implementation of that method in the receiver. Sometimes the behaviour must also be determined
|
||||
* by the type of the parameter.
|
||||
*
|
||||
* <p>
|
||||
* One way to implement this would be to create multiple instanceof-checks for the methods parameter.
|
||||
* However, this creates a maintenance issue. When new types are added we would also need to change
|
||||
* the method's implementation and add a new instanceof-check. This violates the single responsibility
|
||||
* principle - a class should have only one reason to change.
|
||||
*
|
||||
* <p>
|
||||
* Instead of the instanceof-checks a better way is to make another virtual call on the parameter
|
||||
* object. This way new functionality can be easily added without the need to modify existing
|
||||
* implementation (open-closed principle).
|
||||
*
|
||||
* In this example we have hierarchy of objects (GameObject) that can collide to each other. Each
|
||||
* <p>
|
||||
* In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. Each
|
||||
* object has its own coordinates which are checked against the other objects' coordinates. If
|
||||
* there is an overlap, then the objects collide utilizing the Double Dispatch pattern.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main( String[] args ) {
|
||||
// initialize game objects and print their status
|
||||
List<GameObject> objects = new ArrayList<>();
|
||||
|
@ -4,6 +4,11 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.doubledispatch.App;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
|
@ -7,14 +7,18 @@ import java.util.List;
|
||||
*
|
||||
* The Event Aggregator pattern channels events from multiple objects
|
||||
* into a single object to simplify registration for clients.
|
||||
*
|
||||
* In the example LordBaelish, LordVarys and Scout deliver events to
|
||||
* KingsHand. KingsHand, the event aggregator, then delivers the events
|
||||
* to KingJoffrey.
|
||||
* <p>
|
||||
* In the example {@link LordBaelish}, {@link LordVarys} and {@link Scout} deliver events to
|
||||
* {@link KingsHand}. {@link KingsHand}, the event aggregator, then delivers the events
|
||||
* to {@link KingJoffrey}.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
KingJoffrey kingJoffrey = new KingJoffrey();
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.event.aggregator;
|
||||
|
||||
/**
|
||||
*
|
||||
* KingJoffrey observes events from KingsHand.
|
||||
* KingJoffrey observes events from {@link KingsHand}.
|
||||
*
|
||||
*/
|
||||
public class KingJoffrey implements EventObserver {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user