Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
commit
fe63c9cec4
@ -3,21 +3,18 @@ package com.iluwatar.abstractfactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual
|
||||
* factories that have a common theme without specifying their concrete classes. In
|
||||
* normal usage, the client software creates a concrete implementation of the abstract
|
||||
* factory and then uses the generic interface of the factory to create the concrete
|
||||
* objects that are part of the theme. The client does not know (or care) which
|
||||
* concrete objects it gets from each of these internal factories, since it uses only
|
||||
* the generic interfaces of their products. This pattern separates the details of
|
||||
* implementation of a set of objects from their general usage and relies on object
|
||||
* composition, as object creation is implemented in methods exposed in the factory
|
||||
* interface.
|
||||
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
|
||||
* have a common theme without specifying their concrete classes. In normal usage, the client
|
||||
* software creates a concrete implementation of the abstract factory and then uses the generic
|
||||
* interface of the factory to create the concrete objects that are part of the theme. The client
|
||||
* does not know (or care) which concrete objects it gets from each of these internal factories,
|
||||
* since it uses only the generic interfaces of their products. This pattern separates the details
|
||||
* of implementation of a set of objects from their general usage and relies on object composition,
|
||||
* as object creation is implemented in methods exposed in the factory interface.
|
||||
* <p>
|
||||
* The essence of the Abstract Factory pattern is a factory interface
|
||||
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory},
|
||||
* {@link OrcKingdomFactory}). The example uses both concrete implementations to
|
||||
* create a king, a castle and an army.
|
||||
* The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
|
||||
* its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
|
||||
* concrete implementations to create a king, a castle and an army.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
@ -28,6 +25,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Creates kingdom
|
||||
*
|
||||
* @param factory
|
||||
*/
|
||||
public void createKingdom(final KingdomFactory factory) {
|
||||
|
@ -18,5 +18,4 @@ public class OrcKingdomFactory implements KingdomFactory {
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
package com.iluwatar.abstractfactory;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
@ -2,26 +2,25 @@ package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* An adapter helps two incompatible interfaces to work together. This is the real
|
||||
* world definition for an adapter. Interfaces may be incompatible but the inner
|
||||
* functionality should suit the need. The Adapter design pattern allows otherwise
|
||||
* incompatible classes to work together by converting the interface of one class
|
||||
* into an interface expected by the clients.
|
||||
* An adapter helps two incompatible interfaces to work together. This is the real world definition
|
||||
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
|
||||
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
|
||||
* the interface of one class into an interface expected by the clients.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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}).
|
||||
* 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) {
|
||||
|
@ -8,5 +8,4 @@ package com.iluwatar.adapter;
|
||||
public interface Engineer {
|
||||
|
||||
void operateDevice();
|
||||
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into
|
||||
* {@link Engineer} interface expected by the client ({@link GnomeEngineeringManager}).
|
||||
* 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 {
|
||||
@ -20,5 +20,4 @@ public class GnomeEngineer implements Engineer {
|
||||
glider.gainSpeed();
|
||||
glider.takeOff();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,23 +4,24 @@ import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* 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>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>
|
||||
* 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.
|
||||
* 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>
|
||||
* 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.
|
||||
* 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>
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* @see AsyncResult
|
||||
* @see AsyncCallback
|
||||
@ -40,8 +41,10 @@ public class App {
|
||||
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
|
||||
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
|
||||
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
|
||||
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
AsyncResult<Integer> asyncResult4 =
|
||||
executor.startProcess(lazyval(20, 400), callback("Callback result 4"));
|
||||
AsyncResult<String> asyncResult5 =
|
||||
executor.startProcess(lazyval("callback", 600), callback("Callback result 5"));
|
||||
|
||||
// emulate processing in the current thread while async tasks are running in their own threads
|
||||
Thread.sleep(350); // Oh boy I'm working hard here
|
||||
|
@ -18,5 +18,4 @@ public interface AsyncCallback<T> {
|
||||
* @param ex empty value if execution succeeds, some exception if executions fails
|
||||
*/
|
||||
void onComplete(T value, Optional<Exception> ex);
|
||||
|
||||
}
|
||||
|
@ -38,5 +38,4 @@ public interface AsyncExecutor {
|
||||
* @throws InterruptedException if the execution is interrupted
|
||||
*/
|
||||
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
|
||||
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException {
|
||||
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
|
||||
InterruptedException {
|
||||
if (asyncResult.isCompleted()) {
|
||||
return asyncResult.getValue();
|
||||
} else {
|
||||
@ -44,8 +45,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple implementation of async result that allows completing it successfully with a value
|
||||
* or exceptionally with an exception. A really simplified version from its real life cousins
|
||||
* Simple implementation of async result that allows completing it successfully with a value or
|
||||
* exceptionally with an exception. A really simplified version from its real life cousins
|
||||
* FutureTask and CompletableFuture.
|
||||
*
|
||||
* @see java.util.concurrent.FutureTask
|
||||
@ -70,8 +71,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value from successful execution and executes callback if available. Notifies
|
||||
* any thread waiting for completion.
|
||||
* Sets the value from successful execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
*
|
||||
* @param value value of the evaluated task
|
||||
*/
|
||||
@ -85,8 +86,8 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies
|
||||
* any thread waiting for completion.
|
||||
* Sets the exception from failed execution and executes callback if available. Notifies any
|
||||
* thread waiting for completion.
|
||||
*
|
||||
* @param exception exception of the failed task
|
||||
*/
|
||||
|
@ -14,5 +14,4 @@ public class AppTest {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,41 +2,38 @@ package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Bridge pattern can also be thought of as two layers of abstraction. With Bridge,
|
||||
* you can decouple an abstraction from its implementation so that the two can vary independently.
|
||||
* The Bridge pattern can also be thought of as two layers of abstraction. With Bridge, you can
|
||||
* decouple an abstraction from its implementation so that the two can vary independently.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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 blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(
|
||||
new Mjollnir());
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(
|
||||
new Stormbringer());
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -34,5 +34,4 @@ public class BlindingMagicWeapon extends MagicWeapon {
|
||||
public void blind() {
|
||||
getImp().blindImp();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,8 +24,6 @@ public class Excalibur extends BlindingMagicWeaponImpl {
|
||||
|
||||
@Override
|
||||
public void blindImp() {
|
||||
System.out
|
||||
.println("bright light streams from Excalibur blinding the enemy");
|
||||
System.out.println("bright light streams from Excalibur blinding the enemy");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,5 +22,4 @@ public abstract class MagicWeapon {
|
||||
public MagicWeaponImpl getImp() {
|
||||
return imp;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,8 +24,6 @@ public class Mjollnir extends FlyingMagicWeaponImpl {
|
||||
|
||||
@Override
|
||||
public void flyImp() {
|
||||
System.out
|
||||
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||
System.out.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,5 +26,4 @@ public class Stormbringer extends SoulEatingMagicWeaponImpl {
|
||||
public void eatSoulImp() {
|
||||
System.out.println("Stormbringer devours the enemy's soul");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,51 +4,51 @@ import com.iluwatar. builder.Hero.HeroBuilder;
|
||||
|
||||
/**
|
||||
*
|
||||
* The intention of the Builder pattern is to find a solution to the telescoping
|
||||
* constructor anti-pattern. The telescoping constructor anti-pattern occurs when the
|
||||
* increase of object constructor parameter combination leads to an exponential list
|
||||
* of constructors. Instead of using numerous constructors, the builder pattern uses
|
||||
* another object, a builder, that receives each initialization parameter step by step
|
||||
* and then returns the resulting constructed object at once.
|
||||
* The intention of the Builder pattern is to find a solution to the telescoping constructor
|
||||
* anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object
|
||||
* constructor parameter combination leads to an exponential list of constructors. Instead of using
|
||||
* numerous constructors, the builder pattern uses another object, a builder, that receives each
|
||||
* initialization parameter step by step and then returns the resulting constructed object at once.
|
||||
* <p>
|
||||
* The Builder pattern has another benefit. It can be used for objects that contain
|
||||
* flat data (html code, SQL query, X.509 certificate...), that is to say, data that
|
||||
* can't be easily edited. This type of data cannot be edited step by step and must
|
||||
* be edited at once. The best way to construct such an object is to use a builder
|
||||
* class.
|
||||
* The Builder pattern has another benefit. It can be used for objects that contain flat data (html
|
||||
* code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This
|
||||
* type of data cannot be edited step by step and must be edited at once. The best way to construct
|
||||
* such an object is to use a builder class.
|
||||
* <p>
|
||||
* In this example we have the Builder pattern variation as described by Joshua Bloch in
|
||||
* Effective Java 2nd Edition.
|
||||
* In this example we have 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.
|
||||
* 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();
|
||||
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();
|
||||
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();
|
||||
Hero thief =
|
||||
new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
|
||||
.withWeapon(Weapon.BOW).build();
|
||||
System.out.println(thief);
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,8 @@ package com.iluwatar.builder;
|
||||
*/
|
||||
public enum HairType {
|
||||
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");
|
||||
BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY(
|
||||
"long curly");
|
||||
|
||||
private String title;
|
||||
|
||||
|
@ -95,8 +95,7 @@ public class Hero {
|
||||
|
||||
public HeroBuilder(Profession profession, String name) {
|
||||
if (profession == null || name == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"profession and name can not be null");
|
||||
throw new IllegalArgumentException("profession and name can not be null");
|
||||
}
|
||||
this.profession = profession;
|
||||
this.name = name;
|
||||
|
@ -13,5 +13,4 @@ public enum Profession {
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,5 +13,4 @@ public enum Weapon {
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,23 +2,25 @@ package com.iluwatar.business.delegate;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* <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.
|
||||
* 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) {
|
||||
|
@ -2,8 +2,9 @@ package com.iluwatar.callback;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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.
|
||||
* 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 {
|
||||
|
@ -11,5 +11,4 @@ public class SimpleTask extends Task {
|
||||
public void execute() {
|
||||
System.out.println("Perform some important activity and after call the callback method.");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Add a field as a counter. Every time the callback method is called increment this
|
||||
* field. Unit test checks that the field is being incremented.
|
||||
* Add a field as a counter. Every time the callback method is called increment this field. Unit
|
||||
* test checks that the field is being incremented.
|
||||
*
|
||||
* Could be done with mock objects as well where the call method call is verified.
|
||||
*/
|
||||
|
@ -2,30 +2,29 @@ package com.iluwatar.chain;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Chain of Responsibility pattern is a design pattern consisting of command
|
||||
* objects and a series of processing objects. Each processing object contains
|
||||
* logic that defines the types of command objects that it can handle; the rest are
|
||||
* passed to the next processing object in the chain. A mechanism also exists for
|
||||
* adding new processing objects to the end of this chain.
|
||||
* The Chain of Responsibility pattern is a design pattern consisting of command objects and a
|
||||
* series of processing objects. Each processing object contains logic that defines the types of
|
||||
* command objects that it can handle; the rest are passed to the next processing object in the
|
||||
* chain. A mechanism also exists for adding new processing objects to the end of this chain.
|
||||
* <p>
|
||||
* In this example we organize the request handlers ({@link RequestHandler}) into a
|
||||
* chain where each handler has a chance to act on the request on its turn. Here
|
||||
* the king ({@link OrcKing}) makes requests and the military orcs ({@link OrcCommander},
|
||||
* {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain.
|
||||
* In this example we organize the request handlers ({@link RequestHandler}) into a chain where each
|
||||
* handler has a chance to act on the request on its turn. Here 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.TORTURE_PRISONER, "torture prisoner"));
|
||||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
|
||||
|
||||
}
|
||||
|
@ -2,21 +2,22 @@ package com.iluwatar.command;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all information
|
||||
* needed to perform an action or trigger an event at a later time. This information includes the method name,
|
||||
* the object that owns the method and values for the method parameters.
|
||||
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
|
||||
* information needed to perform an action or trigger an event at a later time. This information
|
||||
* includes the method name, the object that owns the method and values for the method parameters.
|
||||
* <p>
|
||||
* Four terms always associated with the command pattern are command, receiver, invoker and client. A command
|
||||
* 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.
|
||||
* Four terms always associated with the command pattern are command, receiver, invoker and client.
|
||||
* A command 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.
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
*/
|
||||
@ -24,6 +25,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -31,8 +31,8 @@ public abstract class Target {
|
||||
public abstract String toString();
|
||||
|
||||
public void printStatus() {
|
||||
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this,
|
||||
getSize(), getVisibility()));
|
||||
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, getSize(),
|
||||
getVisibility()));
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,7 @@ public class Wizard {
|
||||
private Deque<Command> undoStack = new LinkedList<>();
|
||||
private Deque<Command> redoStack = new LinkedList<>();
|
||||
|
||||
public Wizard() {
|
||||
}
|
||||
public Wizard() {}
|
||||
|
||||
public void castSpell(Command command, Target target) {
|
||||
System.out.println(this + " casts " + command + " at " + target);
|
||||
|
@ -1,20 +1,21 @@
|
||||
package com.iluwatar.composite;
|
||||
|
||||
/**
|
||||
* The Composite pattern is a partitioning design pattern. The Composite pattern
|
||||
* describes that a group of objects is to be treated in the same way as a single
|
||||
* instance of an object. The intent of a composite is to "compose" objects into
|
||||
* tree structures to represent part-whole hierarchies. Implementing the Composite
|
||||
* pattern lets clients treat individual objects and compositions uniformly.
|
||||
* The Composite pattern is a partitioning design pattern. The Composite pattern describes that a
|
||||
* group of objects is to be treated in the same way as a single instance of an object. The intent
|
||||
* of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
|
||||
* Implementing the Composite pattern lets clients treat individual objects and compositions
|
||||
* uniformly.
|
||||
* <p>
|
||||
* In this example we have sentences composed of words composed of letters. All of
|
||||
* the objects can be treated through the same interface ({@link LetterComposite}).
|
||||
* In this example we have sentences composed of words composed of letters. All of the objects can
|
||||
* be treated through the same interface ({@link LetterComposite}).
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -22,5 +22,4 @@ public class Letter extends LetterComposite {
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,20 +15,19 @@ public class Messenger {
|
||||
|
||||
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('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('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'))));
|
||||
words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));
|
||||
|
||||
return new Sentence(words);
|
||||
|
||||
@ -38,18 +37,18 @@ public class Messenger {
|
||||
|
||||
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'))));
|
||||
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);
|
||||
|
||||
|
@ -24,5 +24,4 @@ public class Sentence extends LetterComposite {
|
||||
protected void printThisAfter() {
|
||||
System.out.print(".");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,5 +24,4 @@ public class Word extends LetterComposite {
|
||||
protected void printThisAfter() {
|
||||
// nop
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,14 +7,16 @@ import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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.
|
||||
* 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.
|
||||
* <p>
|
||||
* 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 CRUD operations: select, add, update, and delete.
|
||||
* 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 CRUD operations: select,
|
||||
* add, update, and delete.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
@ -2,21 +2,21 @@ package com.iluwatar.decorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* The 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. Using the Decorator pattern it is possible to
|
||||
* change the behavior of the class during runtime.
|
||||
* The 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. Using the Decorator pattern it is possible to change the behavior of the class during
|
||||
* runtime.
|
||||
* <p>
|
||||
* In this example we show how the simple {@link Troll} first attacks and then
|
||||
* flees the battle. Then we decorate the {@link Troll} with a {@link SmartTroll}
|
||||
* and perform the attack again. You can see how the behavior changes after the
|
||||
* decoration.
|
||||
* In this example we show how the simple {@link Troll} first attacks and then flees the battle.
|
||||
* Then we decorate the {@link Troll} with a {@link SmartTroll} and perform the attack again. You
|
||||
* can see how the behavior changes after the decoration.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -1,10 +1,9 @@
|
||||
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.
|
||||
* 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 {
|
||||
@ -32,5 +31,4 @@ public class SmartTroll implements Hostile {
|
||||
System.out.println("The troll calls for help!");
|
||||
decorated.fleeBattle();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,5 +19,4 @@ public class Troll implements Hostile {
|
||||
public void fleeBattle() {
|
||||
System.out.println("The troll shrieks in horror and runs away!");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,8 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
/**
|
||||
*
|
||||
* AdvancedWizard implements inversion of control.
|
||||
* It depends on abstraction that can be injected through
|
||||
* its constructor.
|
||||
* AdvancedWizard implements inversion of control. It depends on abstraction that can be injected
|
||||
* through its constructor.
|
||||
*
|
||||
*/
|
||||
public class AdvancedWizard implements Wizard {
|
||||
|
@ -10,24 +10,25 @@ import com.google.inject.Injector;
|
||||
* - 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.
|
||||
* <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.
|
||||
* 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.
|
||||
* <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.
|
||||
* 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.
|
||||
* {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then used to create
|
||||
* {@link GuiceWizard} object with correct dependencies.
|
||||
* The third example takes the pattern a step further. It uses Guice framework for Dependency
|
||||
* Injection. {@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) {
|
||||
|
@ -4,9 +4,8 @@ import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
*
|
||||
* GuiceWizard implements inversion of control.
|
||||
* Its dependencies are injected through its constructor
|
||||
* by Guice framework.
|
||||
* GuiceWizard implements inversion of control. Its dependencies are injected through its
|
||||
* constructor by Guice framework.
|
||||
*
|
||||
*/
|
||||
public class GuiceWizard implements Wizard {
|
||||
|
@ -2,8 +2,8 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
/**
|
||||
*
|
||||
* Naive Wizard implementation violating the inversion of control principle.
|
||||
* It should depend on abstraction instead.
|
||||
* Naive Wizard implementation violating the inversion of control principle. It should depend on
|
||||
* abstraction instead.
|
||||
*
|
||||
*/
|
||||
public class SimpleWizard implements Wizard {
|
||||
|
@ -8,6 +8,7 @@ package com.iluwatar.dependency.injection;
|
||||
public abstract class Tobacco {
|
||||
|
||||
public void smoke(Wizard wizard) {
|
||||
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this
|
||||
.getClass().getSimpleName()));
|
||||
}
|
||||
}
|
||||
|
@ -6,21 +6,21 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
*
|
||||
* Double Checked Locking is a concurrency design pattern used to reduce the overhead
|
||||
* of acquiring a lock by first testing the locking criterion (the "lock hint") without
|
||||
* actually acquiring the lock. Only if the locking criterion check indicates that
|
||||
* locking is required does the actual locking logic proceed.
|
||||
* Double Checked Locking is a concurrency design pattern used to reduce the overhead of acquiring a
|
||||
* lock by first testing the locking criterion (the "lock hint") without actually acquiring the
|
||||
* lock. Only if the locking criterion check indicates that locking is required does the actual
|
||||
* locking logic proceed.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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) {
|
||||
@ -28,8 +28,7 @@ public class App {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
executorService.execute(() -> {
|
||||
while (inventory.addItem(new Item()))
|
||||
;
|
||||
while (inventory.addItem(new Item()));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -28,8 +28,7 @@ public class Inventory {
|
||||
try {
|
||||
if (items.size() < inventorySize) {
|
||||
items.add(item);
|
||||
System.out.println(Thread.currentThread()
|
||||
+ ": items.size()=" + items.size()
|
||||
System.out.println(Thread.currentThread() + ": items.size()=" + items.size()
|
||||
+ ", inventorySize=" + inventorySize);
|
||||
return true;
|
||||
}
|
||||
@ -39,5 +38,4 @@ public class Inventory {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,20 +6,20 @@ 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.
|
||||
* 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.
|
||||
* 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).
|
||||
* <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
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
@ -27,6 +27,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
@ -40,7 +41,10 @@ public class App {
|
||||
System.out.println("");
|
||||
|
||||
// collision check
|
||||
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2 && o1.intersectsWith(o2)) o1.collision(o2); } ));
|
||||
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> {
|
||||
if (o1 != o2 && o1.intersectsWith(o2))
|
||||
o1.collision(o2);
|
||||
}));
|
||||
System.out.println("");
|
||||
|
||||
// output eventual object statuses
|
||||
|
@ -2,8 +2,7 @@ package com.iluwatar.doubledispatch;
|
||||
|
||||
/**
|
||||
*
|
||||
* Game objects have coordinates and some
|
||||
* other status information.
|
||||
* Game objects have coordinates and some other status information.
|
||||
*
|
||||
*/
|
||||
public abstract class GameObject extends Rectangle {
|
||||
|
@ -18,21 +18,25 @@ public class Meteoroid extends GameObject {
|
||||
|
||||
@Override
|
||||
public void collisionResolve(FlamingAsteroid asteroid) {
|
||||
System.out.println(String.format("%s hits %s.", asteroid.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s.", asteroid.getClass().getSimpleName(), this
|
||||
.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(Meteoroid meteoroid) {
|
||||
System.out.println(String.format("%s hits %s.", meteoroid.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s.", meteoroid.getClass().getSimpleName(), this
|
||||
.getClass().getSimpleName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(SpaceStationMir mir) {
|
||||
System.out.println(String.format("%s hits %s.", mir.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s.", mir.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(SpaceStationIss iss) {
|
||||
System.out.println(String.format("%s hits %s.", iss.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s.", iss.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName()));
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,7 @@ package com.iluwatar.doubledispatch;
|
||||
|
||||
/**
|
||||
*
|
||||
* Rectangle has coordinates and can be checked for overlap against
|
||||
* other Rectangles.
|
||||
* Rectangle has coordinates and can be checked for overlap against other Rectangles.
|
||||
*
|
||||
*/
|
||||
public class Rectangle {
|
||||
@ -23,18 +22,22 @@ public class Rectangle {
|
||||
public int getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public int getTop() {
|
||||
return top;
|
||||
}
|
||||
|
||||
public int getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public int getBottom() {
|
||||
return bottom;
|
||||
}
|
||||
|
||||
boolean intersectsWith(Rectangle r) {
|
||||
return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r.getBottom() < getTop());
|
||||
return !(r.getLeft() > getRight() || r.getRight() < getLeft() || r.getTop() > getBottom() || r
|
||||
.getBottom() < getTop());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,34 +18,31 @@ public class SpaceStationMir extends GameObject {
|
||||
|
||||
@Override
|
||||
public void collisionResolve(FlamingAsteroid asteroid) {
|
||||
System.out.println(String.format("%s hits %s. %s is damaged! %s is set on fire!",
|
||||
asteroid.getClass().getSimpleName(), this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s. %s is damaged! %s is set on fire!", asteroid
|
||||
.getClass().getSimpleName(), this.getClass().getSimpleName(), this.getClass()
|
||||
.getSimpleName(), this.getClass().getSimpleName()));
|
||||
setDamaged(true);
|
||||
setOnFire(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(Meteoroid meteoroid) {
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!",
|
||||
meteoroid.getClass().getSimpleName(), this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!", meteoroid.getClass()
|
||||
.getSimpleName(), this.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
setDamaged(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(SpaceStationMir mir) {
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!",
|
||||
mir.getClass().getSimpleName(), this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!", mir.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
setDamaged(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void collisionResolve(SpaceStationIss iss) {
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!",
|
||||
iss.getClass().getSimpleName(), this.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName()));
|
||||
System.out.println(String.format("%s hits %s. %s is damaged!", iss.getClass().getSimpleName(),
|
||||
this.getClass().getSimpleName(), this.getClass().getSimpleName()));
|
||||
setDamaged(true);
|
||||
}
|
||||
}
|
||||
|
@ -5,22 +5,23 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* A system with lots of objects can lead to complexities when a client wants to subscribe
|
||||
* to events. The client has to find and register for each object individually, if each
|
||||
* object has multiple events then each event requires a separate subscription.
|
||||
* A system with lots of objects can lead to complexities when a client wants to subscribe to
|
||||
* events. The client has to find and register for each object individually, if each object has
|
||||
* multiple events then each event requires a separate subscription.
|
||||
* <p>
|
||||
* An Event Aggregator acts as a single source of events for many objects. It registers
|
||||
* for all the events of the many objects allowing clients to register with just the aggregator.
|
||||
* An Event Aggregator acts as a single source of events for many objects. It registers for all the
|
||||
* events of the many objects allowing clients to register with just the aggregator.
|
||||
* <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}.
|
||||
* {@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) {
|
||||
|
@ -7,7 +7,8 @@ package com.iluwatar.event.aggregator;
|
||||
*/
|
||||
public enum Event {
|
||||
|
||||
STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected");
|
||||
STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED(
|
||||
"Traitor detected");
|
||||
|
||||
private String description;
|
||||
|
||||
|
@ -2,8 +2,7 @@ package com.iluwatar.event.aggregator;
|
||||
|
||||
/**
|
||||
*
|
||||
* KingsHand observes events from multiple sources and delivers them
|
||||
* to listeners.
|
||||
* KingsHand observes events from multiple sources and delivers them to listeners.
|
||||
*
|
||||
*/
|
||||
public class KingsHand extends EventEmitter implements EventObserver {
|
||||
|
@ -7,7 +7,8 @@ package com.iluwatar.event.aggregator;
|
||||
*/
|
||||
public enum Weekday {
|
||||
|
||||
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday");
|
||||
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY(
|
||||
"Friday"), SATURDAY("Saturday"), SUNDAY("Sunday");
|
||||
|
||||
private String description;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
package com.iluwatar.event.aggregator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.event.aggregator.App;
|
||||
|
@ -4,19 +4,20 @@ import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* The Execute Around idiom specifies some code to be executed before and after
|
||||
* a method. Typically the idiom is used when the API has methods to be executed in
|
||||
* pairs, such as resource allocation/deallocation or lock acquisition/release.
|
||||
* The Execute Around idiom specifies some code to be executed before and after a method. Typically
|
||||
* the idiom is used when the API has methods to be executed in pairs, such as resource
|
||||
* allocation/deallocation or lock acquisition/release.
|
||||
* <p>
|
||||
* In this example, we have {@link SimpleFileWriter} class that opens and closes the file
|
||||
* for the user. The user specifies only what to do with the file by providing the
|
||||
* {@link FileWriterAction} implementation.
|
||||
* In this example, we have {@link SimpleFileWriter} class that opens and closes the file for the
|
||||
* user. The user specifies only what to do with the file by providing the {@link FileWriterAction}
|
||||
* implementation.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
* @throws IOException
|
||||
*/
|
||||
|
@ -5,9 +5,8 @@ import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* SimpleFileWriter handles opening and closing file for the user. The user
|
||||
* only has to specify what to do with the file resource through {@link FileWriterAction}
|
||||
* parameter.
|
||||
* SimpleFileWriter handles opening and closing file for the user. The user only has to specify what
|
||||
* to do with the file resource through {@link FileWriterAction} parameter.
|
||||
*
|
||||
*/
|
||||
public class SimpleFileWriter {
|
||||
|
@ -2,21 +2,22 @@ package com.iluwatar.facade;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Facade design pattern is often used when a system is very complex or difficult
|
||||
* to understand because the system has a large number of interdependent classes or
|
||||
* its source code is unavailable. This pattern hides the complexities of the larger
|
||||
* system and provides a simpler interface to the client. It typically involves a single
|
||||
* wrapper class which contains a set of members required by client. These members access
|
||||
* the system on behalf of the facade client and hide the implementation details.
|
||||
* The Facade design pattern is often used when a system is very complex or difficult to understand
|
||||
* because the system has a large number of interdependent classes or its source code is
|
||||
* unavailable. This pattern hides the complexities of the larger system and provides a simpler
|
||||
* interface to the client. It typically involves a single wrapper class which contains a set of
|
||||
* members required by client. These members access the system on behalf of the facade client and
|
||||
* hide the implementation details.
|
||||
* <p>
|
||||
* In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler
|
||||
* interface to the goldmine subsystem.
|
||||
* In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler interface
|
||||
* to the goldmine subsystem.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -16,5 +16,4 @@ public class DwarvenCartOperator extends DwarvenMineWorker {
|
||||
public String name() {
|
||||
return "Dwarf cart operator";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,5 +16,4 @@ public class DwarvenGoldDigger extends DwarvenMineWorker {
|
||||
public String name() {
|
||||
return "Dwarf gold digger";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,12 +6,10 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* DwarvenGoldmineFacade provides a single interface
|
||||
* through which users can operate the subsystems.
|
||||
* DwarvenGoldmineFacade provides a single interface through which users can operate the subsystems.
|
||||
*
|
||||
* This makes the goldmine easier to operate and
|
||||
* cuts the dependencies from the goldmine user to
|
||||
* the subsystems.
|
||||
* This makes the goldmine easier to operate and cuts the dependencies from the goldmine user to the
|
||||
* subsystems.
|
||||
*
|
||||
*/
|
||||
public class DwarvenGoldmineFacade {
|
||||
@ -37,7 +35,8 @@ public class DwarvenGoldmineFacade {
|
||||
makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
|
||||
}
|
||||
|
||||
private void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
|
||||
private void makeActions(Collection<DwarvenMineWorker> workers,
|
||||
DwarvenMineWorker.Action... actions) {
|
||||
for (DwarvenMineWorker worker : workers) {
|
||||
worker.action(actions);
|
||||
}
|
||||
|
@ -16,5 +16,4 @@ public class DwarvenTunnelDigger extends DwarvenMineWorker {
|
||||
public String name() {
|
||||
return "Dwarven tunnel digger";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,23 +2,23 @@ package com.iluwatar.factory.method;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Factory Method is a creational design pattern which uses factory methods to deal
|
||||
* with the problem of creating objects without specifying the exact class of object
|
||||
* that will be created. This is done by creating objects via calling a factory
|
||||
* method either specified in an interface and implemented by child classes, or implemented
|
||||
* in a base class and optionally overridden by derived classes—rather than by calling a
|
||||
* constructor.
|
||||
* The Factory Method is a creational design pattern which uses factory methods to deal with the
|
||||
* problem of creating objects without specifying the exact class of object that will be created.
|
||||
* This is done by creating objects via calling a factory method either specified in an interface
|
||||
* and implemented by child classes, or implemented in a base class and optionally overridden by
|
||||
* derived classes—rather than by calling a constructor.
|
||||
* <p>
|
||||
* In this Factory Method example we have an interface ({@link Blacksmith}) with a method for
|
||||
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses
|
||||
* ({@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce
|
||||
* objects of their liking.
|
||||
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses (
|
||||
* {@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of
|
||||
* their liking.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -17,5 +17,4 @@ public class ElfWeapon implements Weapon {
|
||||
public String toString() {
|
||||
return "Elven " + weaponType;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,5 +10,4 @@ public class OrcBlacksmith implements Blacksmith {
|
||||
public Weapon manufactureWeapon(WeaponType weaponType) {
|
||||
return new OrcWeapon(weaponType);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,5 +17,4 @@ public class OrcWeapon implements Weapon {
|
||||
public String toString() {
|
||||
return "Orcish " + weaponType;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,13 +11,15 @@ import java.util.function.Predicate;
|
||||
import static java.lang.String.valueOf;
|
||||
|
||||
/**
|
||||
* The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those
|
||||
* interfaces tend to mimic domain specific languages, so they can nearly be read as human languages.
|
||||
* The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API.
|
||||
* Those interfaces tend to mimic domain specific languages, so they can nearly be read as human
|
||||
* languages.
|
||||
* <p>
|
||||
* In this example two implementations of a {@link FluentIterable} interface are given. The
|
||||
* {@link SimpleFluentIterable} evaluates eagerly and would be too costly for real world applications.
|
||||
* The {@link LazyFluentIterable} is evaluated on termination. Their usage is demonstrated with a
|
||||
* simple number list that is filtered, transformed and collected. The result is printed afterwards.
|
||||
* {@link SimpleFluentIterable} evaluates eagerly and would be too costly for real world
|
||||
* applications. The {@link LazyFluentIterable} is evaluated on termination. Their usage is
|
||||
* demonstrated with a simple number list that is filtered, transformed and collected. The result is
|
||||
* printed afterwards.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
@ -25,10 +27,8 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> integerList = new ArrayList<>();
|
||||
integerList.addAll(Arrays.asList(
|
||||
1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97,
|
||||
45, 23, 2, -68, 45
|
||||
));
|
||||
integerList.addAll(Arrays.asList(1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2,
|
||||
-68, 45));
|
||||
|
||||
prettyPrint("The initial list contains: ", integerList);
|
||||
|
||||
|
@ -7,7 +7,8 @@ package com.iluwatar.flux.action;
|
||||
*/
|
||||
public enum Content {
|
||||
|
||||
PRODUCTS("Products - This page lists the company's products."), COMPANY("Company - This page displays information about the company.");
|
||||
PRODUCTS("Products - This page lists the company's products."), COMPANY(
|
||||
"Company - This page displays information about the company.");
|
||||
|
||||
private String title;
|
||||
|
||||
|
@ -11,13 +11,14 @@ import com.iluwatar.flux.view.MenuView;
|
||||
*
|
||||
* Flux is the application architecture that Facebook uses for building client-side web
|
||||
* applications. Flux eschews MVC in favor of a unidirectional data flow. When a user interacts with
|
||||
* a React view, the view propagates an action through a central dispatcher, to the various stores that
|
||||
* hold the application's data and business logic, which updates all of the views that are affected.
|
||||
* a React view, the view propagates an action through a central dispatcher, to the various stores
|
||||
* that hold the application's data and business logic, which updates all of the views that are
|
||||
* affected.
|
||||
* <p>
|
||||
* This example has two views: menu and content. They represent typical main menu and content area of
|
||||
* a web page. When menu item is clicked it triggers events through the dispatcher. The events are
|
||||
* received and handled by the stores updating their data as needed. The stores then notify the views
|
||||
* that they should rerender themselves.
|
||||
* This example has two views: menu and content. They represent typical main menu and content area
|
||||
* of a web page. When menu item is clicked it triggers events through the dispatcher. The events
|
||||
* are received and handled by the stores updating their data as needed. The stores then notify the
|
||||
* views that they should rerender themselves.
|
||||
* <p>
|
||||
* http://facebook.github.io/flux/docs/overview.html
|
||||
*
|
||||
@ -26,6 +27,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -21,8 +21,7 @@ public class Dispatcher {
|
||||
|
||||
private List<Store> stores = new LinkedList<>();
|
||||
|
||||
private Dispatcher() {
|
||||
}
|
||||
private Dispatcher() {}
|
||||
|
||||
public static Dispatcher getInstance() {
|
||||
return instance;
|
||||
|
@ -5,8 +5,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* AlchemistShop holds potions on its shelves.
|
||||
* It uses PotionFactory to provide the potions.
|
||||
* AlchemistShop holds potions on its shelves. It uses PotionFactory to provide the potions.
|
||||
*
|
||||
*/
|
||||
public class AlchemistShop {
|
||||
|
@ -2,22 +2,23 @@ package com.iluwatar.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
* Flyweight pattern is useful when the program needs a huge amount of objects.
|
||||
* It provides means to decrease resource usage by sharing object instances.
|
||||
* Flyweight pattern is useful when the program needs a huge amount of objects. It provides means to
|
||||
* decrease resource usage by sharing object instances.
|
||||
* <p>
|
||||
* In this example {@link AlchemistShop} has great amount of potions on its shelves.
|
||||
* To fill the shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents
|
||||
* the Flyweight in this example). Internally {@link PotionFactory} holds a map
|
||||
* of the potions and lazily creates new ones when requested.
|
||||
* In this example {@link AlchemistShop} has great amount of potions on its shelves. To fill the
|
||||
* shelves {@link AlchemistShop} uses {@link PotionFactory} (which represents the Flyweight in this
|
||||
* example). Internally {@link PotionFactory} holds a map of the potions and lazily creates new ones
|
||||
* when requested.
|
||||
* <p>
|
||||
* To enable safe sharing, between clients and threads, Flyweight objects must
|
||||
* be immutable. Flyweight objects are by definition value objects.
|
||||
* To enable safe sharing, between clients and threads, Flyweight objects must be immutable.
|
||||
* Flyweight objects are by definition value objects.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -9,8 +9,6 @@ public class HealingPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel healed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel healed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,6 @@ public class HolyWaterPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel blessed. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel blessed. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,6 @@ public class InvisibilityPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You become invisible. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
System.out.println("You become invisible. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,6 @@ public class PoisonPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("Urgh! This is poisonous. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
System.out.println("Urgh! This is poisonous. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,10 +5,9 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* PotionFactory is the Flyweight in this example.
|
||||
* It minimizes memory use by sharing object instances.
|
||||
* It holds a map of potion instances and new potions
|
||||
* are created only when none of the type already exists.
|
||||
* PotionFactory is the Flyweight in this example. It minimizes memory use by sharing object
|
||||
* instances. It holds a map of potion instances and new potions are created only when none of the
|
||||
* type already exists.
|
||||
*
|
||||
*/
|
||||
public class PotionFactory {
|
||||
|
@ -9,7 +9,6 @@ public class StrengthPotion implements Potion {
|
||||
|
||||
@Override
|
||||
public void drink() {
|
||||
System.out.println("You feel strong. (Potion="
|
||||
+ System.identityHashCode(this) + ")");
|
||||
System.out.println("You feel strong. (Potion=" + System.identityHashCode(this) + ")");
|
||||
}
|
||||
}
|
||||
|
@ -8,63 +8,62 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
* {@link AsyncTask} and {@link AsynchronousService}.
|
||||
*
|
||||
* <p>
|
||||
* <i>PROBLEM</i>
|
||||
* <br/>
|
||||
* A concurrent system have a mixture of short duration, mid duration and long duration tasks.
|
||||
* Mid or long duration tasks should be performed asynchronously to meet quality of service
|
||||
* <i>PROBLEM</i> <br/>
|
||||
* A concurrent system have a mixture of short duration, mid duration and long duration tasks. Mid
|
||||
* or long duration tasks should be performed asynchronously to meet quality of service
|
||||
* requirements.
|
||||
*
|
||||
* <p><i>INTENT</i>
|
||||
* <br/>
|
||||
* The intent of this pattern is to separate the the synchronous and asynchronous processing
|
||||
* in the concurrent application by introducing two intercommunicating layers - one for sync
|
||||
* and one for async. This simplifies the programming without unduly affecting the performance.
|
||||
* <p>
|
||||
* <i>INTENT</i> <br/>
|
||||
* The intent of this pattern is to separate the the synchronous and asynchronous processing in the
|
||||
* concurrent application by introducing two intercommunicating layers - one for sync and one for
|
||||
* async. This simplifies the programming without unduly affecting the performance.
|
||||
*
|
||||
* <p>
|
||||
* <i>APPLICABILITY</i>
|
||||
* <br/>
|
||||
* <i>APPLICABILITY</i> <br/>
|
||||
* <ul>
|
||||
* <li>UNIX network subsystems - In operating systems network operations are carried out
|
||||
* asynchronously with help of hardware level interrupts.</li>
|
||||
* <li>CORBA - At the asynchronous layer one thread is associated with each socket that is
|
||||
* connected to the client. Thread blocks waiting for CORBA requests from the client. On receiving
|
||||
* request it is inserted in the queuing layer which is then picked up by synchronous layer which
|
||||
* processes the request and sends response back to the client.</li>
|
||||
* <li>Android AsyncTask framework - Framework provides a way to execute long running blocking calls,
|
||||
* such as downloading a file, in background threads so that the UI thread remains free to respond
|
||||
* to user inputs.</i>
|
||||
* <li>CORBA - At the asynchronous layer one thread is associated with each socket that is connected
|
||||
* to the client. Thread blocks waiting for CORBA requests from the client. On receiving request it
|
||||
* is inserted in the queuing layer which is then picked up by synchronous layer which processes the
|
||||
* request and sends response back to the client.</li>
|
||||
* <li>Android AsyncTask framework - Framework provides a way to execute long running blocking
|
||||
* calls, such as downloading a file, in background threads so that the UI thread remains free to
|
||||
* respond to user inputs.</i>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* <i>IMPLEMENTATION</i>
|
||||
* <br/>
|
||||
* The main method creates an asynchronous service which does not block the main thread while
|
||||
* the task is being performed. The main thread continues its work which is similar to Async Method
|
||||
* Invocation pattern. The difference between them is that there is a queuing layer between Asynchronous
|
||||
* layer and synchronous layer, which allows for different communication patterns between both layers.
|
||||
* Such as Priority Queue can be used as queuing layer to prioritize the way tasks are executed.
|
||||
* Our implementation is just one simple way of implementing this pattern, there are many variants possible
|
||||
* as described in its applications.
|
||||
* <i>IMPLEMENTATION</i> <br/>
|
||||
* The main method creates an asynchronous service which does not block the main thread while the
|
||||
* task is being performed. The main thread continues its work which is similar to Async Method
|
||||
* Invocation pattern. The difference between them is that there is a queuing layer between
|
||||
* Asynchronous layer and synchronous layer, which allows for different communication patterns
|
||||
* between both layers. Such as Priority Queue can be used as queuing layer to prioritize the way
|
||||
* tasks are executed. Our implementation is just one simple way of implementing this pattern, there
|
||||
* are many variants possible as described in its applications.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
AsynchronousService service = new AsynchronousService(new LinkedBlockingQueue<>());
|
||||
/*
|
||||
* A new task to calculate sum is received but as this is main thread, it should not block.
|
||||
* So it passes it to the asynchronous task layer to compute and proceeds with handling other
|
||||
* incoming requests. This is particularly useful when main thread is waiting on Socket to receive
|
||||
* new incoming requests and does not wait for particular request to be completed before responding
|
||||
* to new request.
|
||||
* A new task to calculate sum is received but as this is main thread, it should not block. So
|
||||
* it passes it to the asynchronous task layer to compute and proceeds with handling other
|
||||
* incoming requests. This is particularly useful when main thread is waiting on Socket to
|
||||
* receive new incoming requests and does not wait for particular request to be completed before
|
||||
* responding to new request.
|
||||
*/
|
||||
service.execute(new ArithmeticSumTask(1000));
|
||||
|
||||
/* New task received, lets pass that to async layer for computation. So both requests will be
|
||||
/*
|
||||
* New task received, lets pass that to async layer for computation. So both requests will be
|
||||
* executed in parallel.
|
||||
*/
|
||||
service.execute(new ArithmeticSumTask(500));
|
||||
@ -85,8 +84,8 @@ public class App {
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the long running task that is performed in background. In our example
|
||||
* the long running task is calculating arithmetic sum with artificial delay.
|
||||
* This is the long running task that is performed in background. In our example the long
|
||||
* running task is calculating arithmetic sum with artificial delay.
|
||||
*/
|
||||
@Override
|
||||
public Long call() throws Exception {
|
||||
@ -94,11 +93,10 @@ public class App {
|
||||
}
|
||||
|
||||
/*
|
||||
* This will be called in context of the main thread where some validations can be
|
||||
* done regarding the inputs. Such as it must be greater than 0. It's a small
|
||||
* computation which can be performed in main thread. If we did validated the input
|
||||
* in background thread then we pay the cost of context switching
|
||||
* which is much more than validating it in main thread.
|
||||
* This will be called in context of the main thread where some validations can be done
|
||||
* regarding the inputs. Such as it must be greater than 0. It's a small computation which can
|
||||
* be performed in main thread. If we did validated the input in background thread then we pay
|
||||
* the cost of context switching which is much more than validating it in main thread.
|
||||
*/
|
||||
@Override
|
||||
public void onPreCall() {
|
||||
|
@ -3,41 +3,41 @@ package com.iluwatar.halfsynchalfasync;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Represents some computation that is performed asynchronously and its result.
|
||||
* The computation is typically done is background threads and the result is posted
|
||||
* back in form of callback. The callback does not implement {@code isComplete}, {@code cancel}
|
||||
* as it is out of scope of this pattern.
|
||||
* Represents some computation that is performed asynchronously and its result. The computation is
|
||||
* typically done is background threads and the result is posted back in form of callback. The
|
||||
* callback does not implement {@code isComplete}, {@code cancel} as it is out of scope of this
|
||||
* pattern.
|
||||
*
|
||||
* @param <O> type of result
|
||||
*/
|
||||
public interface AsyncTask<O> extends Callable<O> {
|
||||
/**
|
||||
* Is called in context of caller thread before call to {@link #call()}. Large
|
||||
* tasks should not be performed in this method as it will block the caller thread.
|
||||
* Small tasks such as validations can be performed here so that the performance penalty
|
||||
* of context switching is not incurred in case of invalid requests.
|
||||
* Is called in context of caller thread before call to {@link #call()}. Large tasks should not be
|
||||
* performed in this method as it will block the caller thread. Small tasks such as validations
|
||||
* can be performed here so that the performance penalty of context switching is not incurred in
|
||||
* case of invalid requests.
|
||||
*/
|
||||
void onPreCall();
|
||||
|
||||
/**
|
||||
* A callback called after the result is successfully computed by {@link #call()}. In our
|
||||
* implementation this method is called in context of background thread but in some variants,
|
||||
* such as Android where only UI thread can change the state of UI widgets, this method is called
|
||||
* in context of UI thread.
|
||||
* implementation this method is called in context of background thread but in some variants, such
|
||||
* as Android where only UI thread can change the state of UI widgets, this method is called in
|
||||
* context of UI thread.
|
||||
*/
|
||||
void onPostCall(O result);
|
||||
|
||||
/**
|
||||
* A callback called if computing the task resulted in some exception. This method
|
||||
* is called when either of {@link #call()} or {@link #onPreCall()} throw any exception.
|
||||
* A callback called if computing the task resulted in some exception. This method is called when
|
||||
* either of {@link #call()} or {@link #onPreCall()} throw any exception.
|
||||
*
|
||||
* @param throwable error cause
|
||||
*/
|
||||
void onError(Throwable throwable);
|
||||
|
||||
/**
|
||||
* This is where the computation of task should reside. This method is called in context
|
||||
* of background thread.
|
||||
* This is where the computation of task should reside. This method is called in context of
|
||||
* background thread.
|
||||
*/
|
||||
@Override
|
||||
O call() throws Exception;
|
||||
|
@ -9,25 +9,25 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* This is the asynchronous layer which does not block when a new request arrives. It just passes
|
||||
* the request to the synchronous layer which consists of a queue i.e. a {@link BlockingQueue} and
|
||||
* a pool of threads i.e. {@link ThreadPoolExecutor}. Out of this pool of worker threads one of the
|
||||
* thread picks up the task and executes it synchronously in background and the result is posted back
|
||||
* to the caller via callback.
|
||||
* the request to the synchronous layer which consists of a queue i.e. a {@link BlockingQueue} and a
|
||||
* pool of threads i.e. {@link ThreadPoolExecutor}. Out of this pool of worker threads one of the
|
||||
* thread picks up the task and executes it synchronously in background and the result is posted
|
||||
* back to the caller via callback.
|
||||
*/
|
||||
public class AsynchronousService {
|
||||
|
||||
/*
|
||||
* This represents the queuing layer as well as synchronous layer of the pattern. The thread
|
||||
* pool contains worker threads which execute the tasks in blocking/synchronous manner. Long
|
||||
* running tasks should be performed in the background which does not affect the performance of
|
||||
* main thread.
|
||||
* This represents the queuing layer as well as synchronous layer of the pattern. The thread pool
|
||||
* contains worker threads which execute the tasks in blocking/synchronous manner. Long running
|
||||
* tasks should be performed in the background which does not affect the performance of main
|
||||
* thread.
|
||||
*/
|
||||
private ExecutorService service;
|
||||
|
||||
/**
|
||||
* Creates an asynchronous service using {@code workQueue} as communication channel between
|
||||
* asynchronous layer and synchronous layer. Different types of queues such as Priority queue,
|
||||
* can be used to control the pattern of communication between the layers.
|
||||
* asynchronous layer and synchronous layer. Different types of queues such as Priority queue, can
|
||||
* be used to control the pattern of communication between the layers.
|
||||
*/
|
||||
public AsynchronousService(BlockingQueue<Runnable> workQueue) {
|
||||
service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, workQueue);
|
||||
@ -38,8 +38,8 @@ public class AsynchronousService {
|
||||
* A non-blocking method which performs the task provided in background and returns immediately.
|
||||
* <p>
|
||||
* On successful completion of task the result is posted back using callback method
|
||||
* {@link AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally
|
||||
* due to some exception then the reason for error is posted back using callback method
|
||||
* {@link AsyncTask#onPostCall(Object)}, if task execution is unable to complete normally due to
|
||||
* some exception then the reason for error is posted back using callback method
|
||||
* {@link AsyncTask#onError(Throwable)}.
|
||||
* <p>
|
||||
* NOTE: The results are posted back in the context of background thread in this implementation.
|
||||
@ -57,11 +57,11 @@ public class AsynchronousService {
|
||||
protected void done() {
|
||||
super.done();
|
||||
try {
|
||||
/* called in context of background thread. There is other variant possible
|
||||
* where result is posted back and sits in the queue of caller thread which
|
||||
* then picks it up for processing. An example of such a system is Android OS,
|
||||
* where the UI elements can only be updated using UI thread. So result must be
|
||||
* posted back in UI thread.
|
||||
/*
|
||||
* called in context of background thread. There is other variant possible where result is
|
||||
* posted back and sits in the queue of caller thread which then picks it up for
|
||||
* processing. An example of such a system is Android OS, where the UI elements can only
|
||||
* be updated using UI thread. So result must be posted back in UI thread.
|
||||
*/
|
||||
task.onPostCall(get());
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -1,16 +1,14 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Base class for order processing filters.
|
||||
* Handles chain management.
|
||||
* Base class for order processing filters. Handles chain management.
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractFilter implements Filter {
|
||||
|
||||
private Filter next;
|
||||
|
||||
public AbstractFilter() {
|
||||
}
|
||||
public AbstractFilter() {}
|
||||
|
||||
public AbstractFilter(Filter next) {
|
||||
this.next = next;
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter
|
||||
* This filter is responsible for checking/filtering the input in the address field.
|
||||
* Concrete implementation of filter This filter is responsible for checking/filtering the input in
|
||||
* the address field.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
*/
|
||||
|
@ -2,32 +2,27 @@ package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
*
|
||||
* When a request enters a Web application, it often must pass several entrance
|
||||
* tests prior to the main processing stage. For example,
|
||||
* - Has the client been authenticated?
|
||||
* - Does the client have a valid session?
|
||||
* - Is the client's IP address from a trusted network?
|
||||
* - Does the request path violate any constraints?
|
||||
* - What encoding does the client use to send the data?
|
||||
* - Do we support the browser type of the client?
|
||||
* Some of these checks are tests, resulting in a yes or no answer that determines
|
||||
* whether processing will continue. Other checks manipulate the incoming data
|
||||
* When a request enters a Web application, it often must pass several entrance tests prior to the
|
||||
* main processing stage. For example, - Has the client been authenticated? - Does the client have a
|
||||
* valid session? - Is the client's IP address from a trusted network? - Does the request path
|
||||
* violate any constraints? - What encoding does the client use to send the data? - Do we support
|
||||
* the browser type of the client? Some of these checks are tests, resulting in a yes or no answer
|
||||
* that determines whether processing will continue. Other checks manipulate the incoming data
|
||||
* stream into a form suitable for processing.
|
||||
* <p>
|
||||
* The classic solution consists of a series of conditional checks, with any failed
|
||||
* check aborting the request. Nested if/else statements are a standard strategy,
|
||||
* but this solution leads to code fragility and a copy-and-paste style of programming,
|
||||
* because the flow of the filtering and the action of the filters is compiled into
|
||||
* the application.
|
||||
* The classic solution consists of a series of conditional checks, with any failed check aborting
|
||||
* the request. Nested if/else statements are a standard strategy, but this solution leads to code
|
||||
* fragility and a copy-and-paste style of programming, because the flow of the filtering and the
|
||||
* action of the filters is compiled into the application.
|
||||
* <p>
|
||||
* The key to solving this problem in a flexible and unobtrusive manner is to have a
|
||||
* simple mechanism for adding and removing processing components, in which each
|
||||
* component completes a specific filtering action. This is the Intercepting Filter
|
||||
* pattern in action.
|
||||
* The key to solving this problem in a flexible and unobtrusive manner is to have a simple
|
||||
* mechanism for adding and removing processing components, in which each component completes a
|
||||
* specific filtering action. This is the Intercepting Filter pattern in action.
|
||||
* <p>
|
||||
* In this example we check whether the order request is valid through pre-processing
|
||||
* done via {@link Filter}. Each field has its own corresponding {@link Filter}
|
||||
* In this example we check whether the order request is valid through pre-processing done via
|
||||
* {@link Filter}. Each field has its own corresponding {@link Filter}
|
||||
* <p>
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
*/
|
||||
@ -35,6 +30,7 @@ public class App{
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -15,9 +15,11 @@ import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
/**
|
||||
* The Client class is responsible for handling the input and running them through filters inside the {@link FilterManager}.
|
||||
* The Client class is responsible for handling the input and running them through filters inside
|
||||
* the {@link FilterManager}.
|
||||
*
|
||||
* This is where {@link Filter}s come to play as the client pre-processes the request before being displayed in the {@link Target}.
|
||||
* This is where {@link Filter}s come to play as the client pre-processes the request before being
|
||||
* displayed in the {@link Target}.
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
@ -85,8 +87,8 @@ public class Client extends JFrame {
|
||||
processButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Order order = new Order(jtFields[0].getText(), jtFields[1]
|
||||
.getText(), jtAreas[0].getText(),
|
||||
Order order =
|
||||
new Order(jtFields[0].getText(), jtFields[1].getText(), jtAreas[0].getText(),
|
||||
jtFields[2].getText(), jtAreas[1].getText());
|
||||
jl.setText(sendRequest(order));
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter
|
||||
* This filter checks for the contact field in which it checks if the input consist of numbers
|
||||
* and it also checks if the input follows the length constraint (11 digits)
|
||||
* Concrete implementation of filter This filter checks for the contact field in which it checks if
|
||||
* the input consist of numbers and it also checks if the input follows the length constraint (11
|
||||
* digits)
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
*/
|
||||
@ -12,8 +13,7 @@ public class ContactFilter extends AbstractFilter {
|
||||
@Override
|
||||
public String execute(Order order) {
|
||||
String result = super.execute(order);
|
||||
if (order.getContactNumber() == null
|
||||
|| order.getContactNumber().isEmpty()
|
||||
if (order.getContactNumber() == null || order.getContactNumber().isEmpty()
|
||||
|| order.getContactNumber().matches(".*[^\\d]+.*")
|
||||
|| order.getContactNumber().length() != 11) {
|
||||
return result + "Invalid contact number! ";
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter
|
||||
* This checks for the deposit code
|
||||
* Concrete implementation of filter This checks for the deposit code
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
*/
|
||||
|
@ -1,9 +1,8 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Filters perform certain tasks prior or after execution of
|
||||
* request by request handler. In this case, before the request is handled by
|
||||
* the target, the request undergoes through each Filter
|
||||
* Filters perform certain tasks prior or after execution of request by request handler. In this
|
||||
* case, before the request is handled by the target, the request undergoes through each Filter
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
@ -12,6 +11,7 @@ public interface Filter {
|
||||
|
||||
/**
|
||||
* Execute order processing filter.
|
||||
*
|
||||
* @param order
|
||||
* @return empty string on success, otherwise error message.
|
||||
*/
|
||||
@ -19,18 +19,21 @@ public interface Filter {
|
||||
|
||||
/**
|
||||
* Set next filter in chain after this.
|
||||
*
|
||||
* @param filter
|
||||
*/
|
||||
void setNext(Filter filter);
|
||||
|
||||
/**
|
||||
* Get next filter in chain after this.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Filter getNext();
|
||||
|
||||
/**
|
||||
* Get last filter in the chain.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Filter getLast();
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.iluwatar.intercepting.filter;
|
||||
|
||||
/**
|
||||
* Concrete implementation of filter. This filter checks if the input in the Name
|
||||
* field is valid. (alphanumeric)
|
||||
* Concrete implementation of filter. This filter checks if the input in the Name field is valid.
|
||||
* (alphanumeric)
|
||||
*
|
||||
* @author joshzambales
|
||||
*
|
||||
@ -12,7 +12,8 @@ public class NameFilter extends AbstractFilter {
|
||||
@Override
|
||||
public String execute(Order order) {
|
||||
String result = super.execute(order);
|
||||
if (order.getName() == null || order.getName().isEmpty() || order.getName().matches(".*[^\\w|\\s]+.*")) {
|
||||
if (order.getName() == null || order.getName().isEmpty()
|
||||
|| order.getName().matches(".*[^\\w|\\s]+.*")) {
|
||||
return result + "Invalid order! ";
|
||||
} else {
|
||||
return result;
|
||||
|
@ -12,8 +12,7 @@ public class Order {
|
||||
private String depositNumber;
|
||||
private String order;
|
||||
|
||||
public Order() {
|
||||
}
|
||||
public Order() {}
|
||||
|
||||
public Order(String name, String contactNumber, String address, String depositNumber, String order) {
|
||||
this.name = name;
|
||||
|
@ -33,8 +33,9 @@ public class Target extends JFrame {
|
||||
super("Order System");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setSize(640, 480);
|
||||
dtm = new DefaultTableModel(new Object[] { "Name", "Contact Number",
|
||||
"Address", "Deposit Number", "Order" }, 0);
|
||||
dtm =
|
||||
new DefaultTableModel(new Object[] {"Name", "Contact Number", "Address", "Deposit Number",
|
||||
"Order"}, 0);
|
||||
jt = new JTable(dtm);
|
||||
del = new JButton("Delete");
|
||||
setup();
|
||||
@ -59,8 +60,7 @@ public class Target extends JFrame {
|
||||
}
|
||||
|
||||
public void execute(String[] request) {
|
||||
dtm.addRow(new Object[] { request[0], request[1], request[2],
|
||||
request[3], request[4] });
|
||||
dtm.addRow(new Object[] {request[0], request[1], request[2], request[3], request[4]});
|
||||
}
|
||||
|
||||
class DListener implements ActionListener {
|
||||
|
@ -4,14 +4,13 @@ import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Interpreter pattern is a design pattern that specifies how to evaluate sentences
|
||||
* in a language. The basic idea is to have a class for each symbol (terminal or nonterminal)
|
||||
* in a specialized computer language. The syntax tree of a sentence in the language is an
|
||||
* instance of the composite pattern and is used to evaluate (interpret) the sentence for a
|
||||
* client.
|
||||
* The Interpreter pattern is a design pattern that specifies how to evaluate sentences in a
|
||||
* language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a
|
||||
* specialized computer language. The syntax tree of a sentence in the language is an instance of
|
||||
* the composite pattern and is used to evaluate (interpret) the sentence for a client.
|
||||
* <p>
|
||||
* In this example we use the Interpreter pattern to break sentences into expressions
|
||||
* ({@link Expression}) that can be evaluated and as a whole form the result.
|
||||
* In this example we use the Interpreter pattern to break sentences into expressions (
|
||||
* {@link Expression}) that can be evaluated and as a whole form the result.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
@ -20,8 +19,8 @@ public class App {
|
||||
*
|
||||
* Program entry point.
|
||||
* <p>
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations
|
||||
* This sample uses postfix, where operator comes after the operands
|
||||
* Expressions can be evaluated using prefix, infix or postfix notations This sample uses postfix,
|
||||
* where operator comes after the operands
|
||||
*
|
||||
* @param args command line args
|
||||
*
|
||||
@ -35,36 +34,28 @@ public class App {
|
||||
if (isOperator(s)) {
|
||||
Expression rightExpression = stack.pop();
|
||||
Expression leftExpression = stack.pop();
|
||||
System.out
|
||||
.println(String.format(
|
||||
"popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(),
|
||||
rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression,
|
||||
rightExpression);
|
||||
System.out.println(String.format("popped from stack left: %d right: %d",
|
||||
leftExpression.interpret(), rightExpression.interpret()));
|
||||
Expression operator = getOperatorInstance(s, leftExpression, rightExpression);
|
||||
System.out.println(String.format("operator: %s", operator));
|
||||
int result = operator.interpret();
|
||||
NumberExpression resultExpression = new NumberExpression(result);
|
||||
stack.push(resultExpression);
|
||||
System.out.println(String.format("push result to stack: %d",
|
||||
resultExpression.interpret()));
|
||||
System.out.println(String.format("push result to stack: %d", resultExpression.interpret()));
|
||||
} else {
|
||||
Expression i = new NumberExpression(s);
|
||||
stack.push(i);
|
||||
System.out.println(String.format("push to stack: %d",
|
||||
i.interpret()));
|
||||
System.out.println(String.format("push to stack: %d", i.interpret()));
|
||||
}
|
||||
}
|
||||
System.out
|
||||
.println(String.format("result: %d", stack.pop().interpret()));
|
||||
System.out.println(String.format("result: %d", stack.pop().interpret()));
|
||||
}
|
||||
|
||||
public static boolean isOperator(String s) {
|
||||
return s.equals("+") || s.equals("-") || s.equals("*");
|
||||
}
|
||||
|
||||
public static Expression getOperatorInstance(String s, Expression left,
|
||||
Expression right) {
|
||||
public static Expression getOperatorInstance(String s, Expression left, Expression right) {
|
||||
switch (s) {
|
||||
case "+":
|
||||
return new PlusExpression(left, right);
|
||||
|
@ -10,8 +10,7 @@ public class MultiplyExpression extends Expression {
|
||||
private Expression leftExpression;
|
||||
private Expression rightExpression;
|
||||
|
||||
public MultiplyExpression(Expression leftExpression,
|
||||
Expression rightExpression) {
|
||||
public MultiplyExpression(Expression leftExpression, Expression rightExpression) {
|
||||
this.leftExpression = leftExpression;
|
||||
this.rightExpression = rightExpression;
|
||||
}
|
||||
|
@ -26,5 +26,4 @@ public class NumberExpression extends Expression {
|
||||
public String toString() {
|
||||
return "number";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,5 +24,4 @@ public class PlusExpression extends Expression {
|
||||
public String toString() {
|
||||
return "+";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,19 +2,19 @@ package com.iluwatar.iterator;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Iterator pattern is a design pattern in which an iterator is used to
|
||||
* traverse a container and access the container's elements. The Iterator pattern
|
||||
* decouples algorithms from containers.
|
||||
* The Iterator pattern is a design pattern in which an iterator is used to traverse a container and
|
||||
* access the container's elements. The Iterator pattern decouples algorithms from containers.
|
||||
* <p>
|
||||
* In this example the Iterator ({@link ItemIterator}) adds abstraction layer on
|
||||
* top of a collection ({@link TreasureChest}). This way the collection can change
|
||||
* its internal implementation without affecting its clients.
|
||||
* In this example the Iterator ({@link ItemIterator}) adds abstraction layer on top of a collection
|
||||
* ({@link TreasureChest}). This way the collection can change its internal implementation without
|
||||
* affecting its clients.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
@ -44,8 +44,7 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
tempIdx = -1;
|
||||
break;
|
||||
}
|
||||
if (type.equals(ItemType.ANY)
|
||||
|| items.get(tempIdx).getType().equals(type)) {
|
||||
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -4,32 +4,31 @@ import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* Layers is an architectural style where software responsibilities are
|
||||
* divided among the different layers of the application.
|
||||
* Layers is an architectural style where software responsibilities are divided among the different
|
||||
* layers of the application.
|
||||
* <p>
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access
|
||||
* layer, business layer and presentation layer.
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access layer,
|
||||
* business layer and presentation layer.
|
||||
* <p>
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>, <code>CakeToppingDao</code> and
|
||||
* <code>CakeLayerDao</code>. The repositories can be used for CRUD operations on cakes, cake toppings
|
||||
* and cake layers respectively.
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>,
|
||||
* <code>CakeToppingDao</code> and <code>CakeLayerDao</code>. The repositories can be used for CRUD
|
||||
* operations on cakes, cake toppings and cake layers respectively.
|
||||
* <p>
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code> offers
|
||||
* methods to retrieve available cake toppings and cake layers and baked cakes. Also the
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code>
|
||||
* offers methods to retrieve available cake toppings and cake layers and baked cakes. Also the
|
||||
* service is used to create new cakes out of cake toppings and cake layers.
|
||||
* <p>
|
||||
* The presentation layer is built on the business layer and in this example it simply lists
|
||||
* the cakes that have been baked.
|
||||
* The presentation layer is built on the business layer and in this example it simply lists the
|
||||
* cakes that have been baked.
|
||||
* <p>
|
||||
* We have applied so called strict layering which means that the layers can only access
|
||||
* the classes directly beneath them. This leads the solution to create an additional set of
|
||||
* DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
|
||||
* to translate data between layers. In other words, <code>CakeBakingService</code> cannot
|
||||
* return entities (<code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>)
|
||||
* directly since these reside on data access layer but instead translates these into business
|
||||
* layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
|
||||
* and returns them instead. This way the presentation layer does not have any knowledge of
|
||||
* other layers than the business layer and thus is not affected by changes to them.
|
||||
* We have applied so called strict layering which means that the layers can only access the classes
|
||||
* directly beneath them. This leads the solution to create an additional set of DTOs (
|
||||
* <code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate
|
||||
* data between layers. In other words, <code>CakeBakingService</code> cannot return entities (
|
||||
* <code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>) directly since these reside
|
||||
* on data access layer but instead translates these into business layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns them instead. This way
|
||||
* the presentation layer does not have any knowledge of other layers than the business layer and
|
||||
* thus is not affected by changes to them.
|
||||
*
|
||||
* @see Cake
|
||||
* @see CakeTopping
|
||||
@ -49,6 +48,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Application entry point
|
||||
*
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
@ -63,6 +63,7 @@ public class App {
|
||||
|
||||
/**
|
||||
* Initializes the example data
|
||||
*
|
||||
* @param cakeBakingService
|
||||
*/
|
||||
private static void initializeData(CakeBakingService cakeBakingService) {
|
||||
@ -76,17 +77,18 @@ public class App {
|
||||
cakeBakingService.saveNewTopping(new CakeToppingInfo("candies", 350));
|
||||
cakeBakingService.saveNewTopping(new CakeToppingInfo("cherry", 350));
|
||||
|
||||
CakeInfo cake1 = new CakeInfo(new CakeToppingInfo("candies", 0),
|
||||
Arrays.asList(new CakeLayerInfo("chocolate", 0), new CakeLayerInfo("banana", 0),
|
||||
new CakeLayerInfo("strawberry", 0)));
|
||||
CakeInfo cake1 =
|
||||
new CakeInfo(new CakeToppingInfo("candies", 0), Arrays.asList(new CakeLayerInfo(
|
||||
"chocolate", 0), new CakeLayerInfo("banana", 0), new CakeLayerInfo("strawberry", 0)));
|
||||
try {
|
||||
cakeBakingService.bakeNewCake(cake1);
|
||||
} catch (CakeBakingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
CakeInfo cake2 = new CakeInfo(new CakeToppingInfo("cherry", 0),
|
||||
Arrays.asList(new CakeLayerInfo("vanilla", 0), new CakeLayerInfo("lemon", 0),
|
||||
new CakeLayerInfo("strawberry", 0)));
|
||||
CakeInfo cake2 =
|
||||
new CakeInfo(new CakeToppingInfo("cherry", 0), Arrays.asList(
|
||||
new CakeLayerInfo("vanilla", 0), new CakeLayerInfo("lemon", 0), new CakeLayerInfo(
|
||||
"strawberry", 0)));
|
||||
try {
|
||||
cakeBakingService.bakeNewCake(cake2);
|
||||
} catch (CakeBakingException e) {
|
||||
|
@ -9,8 +9,7 @@ public class CakeBakingException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CakeBakingException() {
|
||||
}
|
||||
public CakeBakingException() {}
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
|
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