Compare commits

..

39 Commits
1.7.0 ... 1.8.0

Author SHA1 Message Date
f018d13c39 Set version number for monthly release 2015-12-02 23:21:44 +02:00
5bec63659f Merge pull request #305 from hoswey/master
Fix #216 Repository vs DAO
2015-11-28 00:09:27 +02:00
9d4fff6029 Fix #216 Repository vs DAO 2015-11-27 11:22:33 +08:00
092d48d150 Merge pull request #294 from DevFactory/release1
Unit tests for adapter, business-delegate, factory-method and command modules
2015-11-22 08:44:13 +02:00
6b99f2669e Added capability for test coverage report generation and steps to do so. 2015-11-22 05:47:36 +05:30
ced317bc9d Added UnitTest cases for factory method. 2015-11-22 05:46:14 +05:30
e5614e5a20 Added UnitTest cases for command. 2015-11-22 05:45:45 +05:30
012b638023 Added UnitTest cases for business delegate. 2015-11-22 05:45:14 +05:30
b577890db4 Added UnitTest cases for adapter. 2015-11-22 05:44:45 +05:30
8ba0192864 Add EIP tag to relevant patterns 2015-11-21 16:34:59 +02:00
899b9617c9 Merge pull request #295 from iluwatar/JaXt0r-publish-subscribe
Jaxt0r publish subscribe
2015-11-21 16:10:34 +02:00
d0fca6d318 Improve the example and documentation 2015-11-21 16:05:55 +02:00
7885380633 Apply Google coding conventions 2015-11-21 15:12:31 +02:00
230c02fb24 Fix spelling 2015-11-21 15:09:23 +02:00
b97e9207a7 Update version number for Publish-Subscribe 2015-11-21 15:03:27 +02:00
8519e13de6 Merge branch 'publish-subscribe' of https://github.com/JaXt0r/java-design-patterns into JaXt0r-publish-subscribe
Conflicts:
	pom.xml
2015-11-21 14:55:02 +02:00
ba3f583467 Merge pull request #290 from hoswey/master
implements Twin design pattern #63
2015-11-16 21:09:41 +02:00
142274f3f7 implements Twin design pattern #63, add credit and rephrase the comments 2015-11-16 10:01:03 +08:00
5b06c52fff Merge pull request #291 from tainarareis/abstract-factory
Issue #286: Added main method in Abstract Factory Example
2015-11-14 21:33:48 +02:00
033f8e740d Merge pull request #287 from l-cortese/master
log4j.xml relocated and excluded from JAR
2015-11-14 21:19:50 +02:00
34528173b7 Issue #286: Added main method in Abstract Factory Example 2015-11-14 16:48:19 -02:00
fdbfa9e8ee implements Twin design pattern #63 2015-11-14 17:38:35 +08:00
bf65168776 corrected typos in the comments 2015-11-13 22:35:30 +01:00
043a610754 log4j.xml relocated due to visibility issues and excluded from generated
JAR. pom.xml formatted according to coding conventions
2015-11-13 18:34:10 +01:00
27199325ec Merge pull request #284 from hoswey/master
correct the package name for producer-consumer test folder
2015-11-07 11:48:51 +02:00
960b568fbb correct the package name for producer-consumer test folder 2015-11-07 15:51:16 +08:00
c02e65fa3a Added alias names for GOF patterns #267 2015-11-04 21:13:32 +02:00
fe63c9cec4 Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
2015-11-02 21:39:17 +02:00
7e4f04602e Merge pull request #282 from l-cortese/master
Corrected Freddy's last name
2015-11-02 21:36:20 +02:00
306b1f3d31 Reformat rest of the design patterns - Issue #224 2015-11-01 21:29:13 -05:00
449340bd2b Reformat business-delegate, callback, chain, command, composite, dao, decorator & dependency-injection patterns. 2015-11-01 18:48:43 -05:00
3af06a3a3a Reformat builder pattern - issue #224 2015-11-01 18:03:29 -05:00
e7b6542134 Reformat bridge design pattern - Issue #224 2015-11-01 17:43:54 -05:00
16a8c85af6 Reformat Async Method Invocation - Issue #224 2015-11-01 17:33:25 -05:00
95c16200e7 Reformat Adapter Pattern - Issue #224 2015-11-01 17:26:57 -05:00
c0c21ebd91 Reformat Abstract Factory - Issue #224 2015-11-01 17:18:39 -05:00
25c1f87d46 Corrected Freddy's second name 2015-11-01 23:15:57 +01:00
c6ca80b25f Bump version number for the next development iteration 2015-11-01 22:22:31 +02:00
ac5c1967df #161 - Publish Subscribe Channel realisation.Heavily based on iluwatars Message Channel implementation (#143). 2015-10-25 07:11:34 +01:00
550 changed files with 9808 additions and 8604 deletions

13
CODE_COVERAGE.md Normal file
View File

@ -0,0 +1,13 @@
# Code Coverage Report generation
To generate the code coverage report, execute the following command:
> mvn clean verify
This will generate code coverage report in each of the modules. In order to view the same, open the following file in your browser.
> target/site/jacoco/index.html
Please note that the above folder is created under each of the modules. For example:
* adapter/target/site/jacoco/index.html
* busniess-delegate/target/site/jacoco/index.html

View File

@ -9,6 +9,8 @@ tags:
- Gang Of Four - Gang Of Four
--- ---
**Also known as:** Kit
**Intent:** Provide an interface for creating families of related or dependent **Intent:** Provide an interface for creating families of related or dependent
objects without specifying their concrete classes. objects without specifying their concrete classes.

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>abstract-factory</artifactId> <artifactId>abstract-factory</artifactId>
<dependencies> <dependencies>

View File

@ -3,80 +3,108 @@ package com.iluwatar.abstractfactory;
/** /**
* *
* The Abstract Factory pattern provides a way to encapsulate a group of individual * The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
* factories that have a common theme without specifying their concrete classes. In * have a common theme without specifying their concrete classes. In normal usage, the client
* normal usage, the client software creates a concrete implementation of the abstract * software creates a concrete implementation of the abstract factory and then uses the generic
* factory and then uses the generic interface of the factory to create the concrete * interface of the factory to create the concrete objects that are part of the theme. The client
* objects that are part of the theme. The client does not know (or care) which * does not know (or care) which concrete objects it gets from each of these internal factories,
* concrete objects it gets from each of these internal factories, since it uses only * since it uses only the generic interfaces of their products. This pattern separates the details
* the generic interfaces of their products. This pattern separates the details of * of implementation of a set of objects from their general usage and relies on object composition,
* implementation of a set of objects from their general usage and relies on object * as object creation is implemented in methods exposed in the factory interface.
* composition, as object creation is implemented in methods exposed in the factory
* interface.
* <p> * <p>
* The essence of the Abstract Factory pattern is a factory interface * The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory}) and
* ({@link KingdomFactory}) and its implementations ({@link ElfKingdomFactory}, * its implementations ({@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses both
* {@link OrcKingdomFactory}). The example uses both concrete implementations to * concrete implementations to create a king, a castle and an army.
* create a king, a castle and an army.
* *
*/ */
public class App { public class App {
private King king; private King king;
private Castle castle; private Castle castle;
private Army army; private Army army;
/**
* Creates kingdom /**
* @param factory * Creates kingdom
*/ *
public void createKingdom(final KingdomFactory factory) { * @param factory
setKing(factory.createKing()); */
setCastle(factory.createCastle()); public void createKingdom(final KingdomFactory factory) {
setArmy(factory.createArmy()); setKing(factory.createKing());
} setCastle(factory.createCastle());
setArmy(factory.createArmy());
}
ElfKingdomFactory getElfKingdomFactory() {
return new ElfKingdomFactory();
}
OrcKingdomFactory getOrcKingdomFactory() {
return new OrcKingdomFactory();
}
King getKing(final KingdomFactory factory) {
return factory.createKing();
}
Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}
Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}
public King getKing() {
return king;
}
private void setKing(final King king) {
this.king = king;
}
public Castle getCastle() {
return castle;
}
private void setCastle(final Castle castle) {
this.castle = castle;
}
public Army getArmy() {
return army;
}
private void setArmy(final Army army) {
this.army = army;
}
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
App app = new App();
System.out.println("Elf Kingdom");
KingdomFactory elfKingdomFactory;
elfKingdomFactory = app.getElfKingdomFactory();
app.createKingdom(elfKingdomFactory);
System.out.println(app.getArmy().getDescription());
System.out.println(app.getCastle().getDescription());
System.out.println(app.getKing().getDescription());
ElfKingdomFactory getElfKingdomFactory() { System.out.println("\nOrc Kingdom");
return new ElfKingdomFactory(); KingdomFactory orcKingdomFactory;
} orcKingdomFactory = app.getOrcKingdomFactory();
app.createKingdom(orcKingdomFactory);
OrcKingdomFactory getOrcKingdomFactory() { System.out.println(app.getArmy().getDescription());
return new OrcKingdomFactory(); System.out.println(app.getCastle().getDescription());
} System.out.println(app.getKing().getDescription());
King getKing(final KingdomFactory factory) { }
return factory.createKing();
}
Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}
Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}
public King getKing() {
return king;
}
private void setKing(final King king) {
this.king = king;
}
public Castle getCastle() {
return castle;
}
private void setCastle(final Castle castle) {
this.castle = castle;
}
public Army getArmy() {
return army;
}
private void setArmy(final Army army) {
this.army = army;
}
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface Army { public interface Army {
String getDescription(); String getDescription();
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface Castle { public interface Castle {
String getDescription(); String getDescription();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfArmy implements Army { public class ElfArmy implements Army {
static final String DESCRIPTION = "This is the Elven Army!"; static final String DESCRIPTION = "This is the Elven Army!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfCastle implements Castle { public class ElfCastle implements Castle {
static final String DESCRIPTION = "This is the Elven castle!"; static final String DESCRIPTION = "This is the Elven castle!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfKing implements King { public class ElfKing implements King {
static final String DESCRIPTION = "This is the Elven king!"; static final String DESCRIPTION = "This is the Elven king!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,16 +7,16 @@ package com.iluwatar.abstractfactory;
*/ */
public class ElfKingdomFactory implements KingdomFactory { public class ElfKingdomFactory implements KingdomFactory {
public Castle createCastle() { public Castle createCastle() {
return new ElfCastle(); return new ElfCastle();
} }
public King createKing() { public King createKing() {
return new ElfKing(); return new ElfKing();
} }
public Army createArmy() { public Army createArmy() {
return new ElfArmy(); return new ElfArmy();
} }
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.abstractfactory;
*/ */
public interface King { public interface King {
String getDescription(); String getDescription();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public interface KingdomFactory { public interface KingdomFactory {
Castle createCastle(); Castle createCastle();
King createKing(); King createKing();
Army createArmy(); Army createArmy();
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcArmy implements Army { public class OrcArmy implements Army {
static final String DESCRIPTION = "This is the Orc Army!"; static final String DESCRIPTION = "This is the Orc Army!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcCastle implements Castle { public class OrcCastle implements Castle {
static final String DESCRIPTION = "This is the Orc castle!"; static final String DESCRIPTION = "This is the Orc castle!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcKing implements King { public class OrcKing implements King {
static final String DESCRIPTION = "This is the Orc king!"; static final String DESCRIPTION = "This is the Orc king!";
@Override @Override
public String getDescription() { public String getDescription() {
return DESCRIPTION; return DESCRIPTION;
} }
} }

View File

@ -7,16 +7,15 @@ package com.iluwatar.abstractfactory;
*/ */
public class OrcKingdomFactory implements KingdomFactory { public class OrcKingdomFactory implements KingdomFactory {
public Castle createCastle() { public Castle createCastle() {
return new OrcCastle(); return new OrcCastle();
} }
public King createKing() { public King createKing() {
return new OrcKing(); return new OrcKing();
} }
public Army createArmy() {
return new OrcArmy();
}
public Army createArmy() {
return new OrcArmy();
}
} }

View File

@ -1,4 +1,5 @@
package com.iluwatar.abstractfactory; package com.iluwatar.abstractfactory;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -7,71 +8,71 @@ import org.junit.Test;
public class AppTest { public class AppTest {
private App app = new App(); private App app = new App();
private KingdomFactory elfFactory; private KingdomFactory elfFactory;
private KingdomFactory orcFactory; private KingdomFactory orcFactory;
@Before @Before
public void setUp() { public void setUp() {
elfFactory = app.getElfKingdomFactory(); elfFactory = app.getElfKingdomFactory();
orcFactory = app.getOrcKingdomFactory(); orcFactory = app.getOrcKingdomFactory();
} }
@Test @Test
public void king() { public void king() {
final King elfKing = app.getKing(elfFactory); final King elfKing = app.getKing(elfFactory);
assertTrue(elfKing instanceof ElfKing); assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
final King orcKing = app.getKing(orcFactory); final King orcKing = app.getKing(orcFactory);
assertTrue(orcKing instanceof OrcKing); assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
} }
@Test @Test
public void castle() { public void castle() {
final Castle elfCastle = app.getCastle(elfFactory); final Castle elfCastle = app.getCastle(elfFactory);
assertTrue(elfCastle instanceof ElfCastle); assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
final Castle orcCastle = app.getCastle(orcFactory); final Castle orcCastle = app.getCastle(orcFactory);
assertTrue(orcCastle instanceof OrcCastle); assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
} }
@Test @Test
public void army() { public void army() {
final Army elfArmy = app.getArmy(elfFactory); final Army elfArmy = app.getArmy(elfFactory);
assertTrue(elfArmy instanceof ElfArmy); assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
final Army orcArmy = app.getArmy(orcFactory); final Army orcArmy = app.getArmy(orcFactory);
assertTrue(orcArmy instanceof OrcArmy); assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
} }
@Test @Test
public void createElfKingdom() { public void createElfKingdom() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final King king = app.getKing(); final King king = app.getKing();
final Castle castle = app.getCastle(); final Castle castle = app.getCastle();
final Army army = app.getArmy(); final Army army = app.getArmy();
assertTrue(king instanceof ElfKing); assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle); assertTrue(castle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof ElfArmy); assertTrue(army instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, army.getDescription()); assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
} }
@Test @Test
public void createOrcKingdom() { public void createOrcKingdom() {
app.createKingdom(orcFactory); app.createKingdom(orcFactory);
final King king = app.getKing(); final King king = app.getKing();
final Castle castle = app.getCastle(); final Castle castle = app.getCastle();
final Army army = app.getArmy(); final Army army = app.getArmy();
assertTrue(king instanceof OrcKing); assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle); assertTrue(castle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof OrcArmy); assertTrue(army instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, army.getDescription()); assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
} }
} }

View File

@ -9,6 +9,8 @@ tags:
- Gang Of Four - Gang Of Four
--- ---
**Also known as:** Wrapper
**Intent:** Convert the interface of a class into another interface the clients **Intent:** Convert the interface of a class into another interface the clients
expect. Adapter lets classes work together that couldn't otherwise because of expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces. incompatible interfaces.

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>adapter</artifactId> <artifactId>adapter</artifactId>
<dependencies> <dependencies>
@ -14,5 +14,10 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,31 +1,28 @@
package com.iluwatar.adapter; package com.iluwatar.adapter;
/** /**
* * An adapter helps two incompatible interfaces to work together. This is the real world definition
* An adapter helps two incompatible interfaces to work together. This is the real * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
* world definition for an adapter. Interfaces may be incompatible but the inner * The Adapter design pattern allows otherwise incompatible classes to work together by converting
* functionality should suit the need. The Adapter design pattern allows otherwise * the interface of one class into an interface expected by the clients.
* 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.
* <p>
* The Adapter ({@link GnomeEngineer}) converts the interface of the target class
* ({@link GoblinGlider}) into a suitable one expected by the client
* ({@link GnomeEngineeringManager}).
* *
* <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.
*
* <p>The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
* {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
* ).
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point.
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
Engineer manager = new GnomeEngineeringManager(); public static void main(String[] args) {
manager.operateDevice(); Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
} manager.operateDevice();
}
} }

View File

@ -7,6 +7,5 @@ package com.iluwatar.adapter;
*/ */
public interface Engineer { public interface Engineer {
void operateDevice(); void operateDevice();
} }

View File

@ -2,23 +2,22 @@ package com.iluwatar.adapter;
/** /**
* *
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into * Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into {@link Engineer}
* {@link Engineer} interface expected by the client ({@link GnomeEngineeringManager}). * interface expected by the client ({@link GnomeEngineeringManager}).
* *
*/ */
public class GnomeEngineer implements Engineer { public class GnomeEngineer implements Engineer {
private GoblinGlider glider; private GoblinGlider glider;
public GnomeEngineer() { public GnomeEngineer() {
glider = new GoblinGlider(); glider = new GoblinGlider();
} }
@Override
public void operateDevice() {
glider.attachGlider();
glider.gainSpeed();
glider.takeOff();
}
@Override
public void operateDevice() {
glider.attachGlider();
glider.gainSpeed();
glider.takeOff();
}
} }

View File

@ -1,20 +1,26 @@
package com.iluwatar.adapter; package com.iluwatar.adapter;
/** /**
*
* GnomeEngineering manager uses {@link Engineer} to operate devices. * GnomeEngineering manager uses {@link Engineer} to operate devices.
*
*/ */
public class GnomeEngineeringManager implements Engineer { public class GnomeEngineeringManager implements Engineer {
private Engineer engineer; private Engineer engineer;
public GnomeEngineeringManager() { public GnomeEngineeringManager() {
engineer = new GnomeEngineer();
}
@Override }
public void operateDevice() {
engineer.operateDevice(); public GnomeEngineeringManager(Engineer engineer) {
} this.engineer = engineer;
}
@Override
public void operateDevice() {
engineer.operateDevice();
}
public void setEngineer(Engineer engineer) {
this.engineer = engineer;
}
} }

View File

@ -7,15 +7,15 @@ package com.iluwatar.adapter;
*/ */
public class GoblinGlider { public class GoblinGlider {
public void attachGlider() { public void attachGlider() {
System.out.println("Glider attached."); System.out.println("Glider attached.");
} }
public void gainSpeed() { public void gainSpeed() {
System.out.println("Gaining speed."); System.out.println("Gaining speed.");
} }
public void takeOff() { public void takeOff() {
System.out.println("Lift-off!"); System.out.println("Lift-off!");
} }
} }

View File

@ -0,0 +1,68 @@
package com.iluwatar.adapter;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
/**
* 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.
*
* <p>The Adapter ({@link GnomeEngineer}) converts the interface
* of the target class ({@link GoblinGlider}) into a suitable one expected by
* the client ({@link GnomeEngineeringManager}
* ).
*/
public class AdapterPatternTest {
private Map<String, Object> beans;
private static final String ENGINEER_BEAN = "engineer";
private static final String MANAGER_BEAN = "manager";
/**
* This method runs before the test execution and sets the bean objects in the beans Map.
*/
@Before
public void setup() {
beans = new HashMap<>();
GnomeEngineer gnomeEngineer = spy(new GnomeEngineer());
beans.put(ENGINEER_BEAN, gnomeEngineer);
GnomeEngineeringManager manager = new GnomeEngineeringManager();
manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN));
beans.put(MANAGER_BEAN, manager);
}
/**
* This test asserts that when we call operateDevice() method on a manager bean, it is internally
* calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer})
* converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected
* by the client ({@link GnomeEngineeringManager} ).
*/
@Test
public void testAdapter() {
Engineer manager = (Engineer) beans.get(MANAGER_BEAN);
// when manager is asked to operate device
manager.operateDevice();
// Manager internally calls the engineer object to operateDevice
Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN);
verify(engineer).operateDevice();
}
}

View File

@ -1,19 +0,0 @@
package com.iluwatar.adapter;
import org.junit.Test;
import com.iluwatar.adapter.App;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>async-method-invocation</artifactId> <artifactId>async-method-invocation</artifactId>
<dependencies> <dependencies>

View File

@ -4,23 +4,24 @@ import java.util.concurrent.Callable;
/** /**
* This application demonstrates the async method invocation pattern. Key parts of the pattern are * 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>AsyncResult</code> which is an intermediate container for an asynchronously evaluated
* <code>AsyncCallback</code> which can be provided to be executed on task completion and * 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. * <code>AsyncExecutor</code> that manages the execution of the async tasks.
* <p> * <p>
* The main method shows example flow of async invocations. The main thread starts multiple tasks with * The main method shows example flow of async invocations. The main thread starts multiple tasks
* variable durations and then continues its own work. When the main thread has done it's job it collects * with variable durations and then continues its own work. When the main thread has done it's job
* the results of the async tasks. Two of the tasks are handled with callbacks, meaning the callbacks are * it collects the results of the async tasks. Two of the tasks are handled with callbacks, meaning
* executed immediately when the tasks complete. * the callbacks are executed immediately when the tasks complete.
* <p> * <p>
* Noteworthy difference of thread usage between the async results and callbacks is that the async results * Noteworthy difference of thread usage between the async results and callbacks is that the async
* are collected in the main thread but the callbacks are executed within the worker threads. This should be * results are collected in the main thread but the callbacks are executed within the worker
* noted when working with thread pools. * threads. This should be noted when working with thread pools.
* <p> * <p>
* Java provides its own implementations of async method invocation pattern. FutureTask, CompletableFuture * Java provides its own implementations of async method invocation pattern. FutureTask,
* and ExecutorService are the real world implementations of this pattern. But due to the nature of parallel * CompletableFuture and ExecutorService are the real world implementations of this pattern. But due
* programming, the implementations are not trivial. This example does not take all possible scenarios into * to the nature of parallel programming, the implementations are not trivial. This example does not
* account but rather provides a simple version that helps to understand the pattern. * take all possible scenarios into account but rather provides a simple version that helps to
* understand the pattern.
* *
* @see AsyncResult * @see AsyncResult
* @see AsyncCallback * @see AsyncCallback
@ -32,66 +33,68 @@ import java.util.concurrent.Callable;
*/ */
public class App { public class App {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// construct a new executor that will run async tasks // construct a new executor that will run async tasks
AsyncExecutor executor = new ThreadAsyncExecutor(); AsyncExecutor executor = new ThreadAsyncExecutor();
// start few async tasks with varying processing times, two last with callback handlers // start few async tasks with varying processing times, two last with callback handlers
AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500)); AsyncResult<Integer> asyncResult1 = executor.startProcess(lazyval(10, 500));
AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300)); AsyncResult<String> asyncResult2 = executor.startProcess(lazyval("test", 300));
AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700)); AsyncResult<Long> asyncResult3 = executor.startProcess(lazyval(50L, 700));
AsyncResult<Integer> asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Callback result 4")); AsyncResult<Integer> asyncResult4 =
AsyncResult<String> asyncResult5 = executor.startProcess(lazyval("callback", 600), callback("Callback result 5")); 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 // 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 Thread.sleep(350); // Oh boy I'm working hard here
log("Some hard work done"); log("Some hard work done");
// wait for completion of the tasks // wait for completion of the tasks
Integer result1 = executor.endProcess(asyncResult1); Integer result1 = executor.endProcess(asyncResult1);
String result2 = executor.endProcess(asyncResult2); String result2 = executor.endProcess(asyncResult2);
Long result3 = executor.endProcess(asyncResult3); Long result3 = executor.endProcess(asyncResult3);
asyncResult4.await(); asyncResult4.await();
asyncResult5.await(); asyncResult5.await();
// log the results of the tasks, callbacks log immediately when complete // log the results of the tasks, callbacks log immediately when complete
log("Result 1: " + result1); log("Result 1: " + result1);
log("Result 2: " + result2); log("Result 2: " + result2);
log("Result 3: " + result3); log("Result 3: " + result3);
} }
/** /**
* Creates a callable that lazily evaluates to given value with artificial delay. * Creates a callable that lazily evaluates to given value with artificial delay.
* *
* @param value value to evaluate * @param value value to evaluate
* @param delayMillis artificial delay in milliseconds * @param delayMillis artificial delay in milliseconds
* @return new callable for lazy evaluation * @return new callable for lazy evaluation
*/ */
private static <T> Callable<T> lazyval(T value, long delayMillis) { private static <T> Callable<T> lazyval(T value, long delayMillis) {
return () -> { return () -> {
Thread.sleep(delayMillis); Thread.sleep(delayMillis);
log("Task completed with: " + value); log("Task completed with: " + value);
return value; return value;
}; };
} }
/** /**
* Creates a simple callback that logs the complete status of the async result. * Creates a simple callback that logs the complete status of the async result.
* *
* @param name callback name * @param name callback name
* @return new async callback * @return new async callback
*/ */
private static <T> AsyncCallback<T> callback(String name) { private static <T> AsyncCallback<T> callback(String name) {
return (value, ex) -> { return (value, ex) -> {
if (ex.isPresent()) { if (ex.isPresent()) {
log(name + " failed: " + ex.map(Exception::getMessage).orElse("")); log(name + " failed: " + ex.map(Exception::getMessage).orElse(""));
} else { } else {
log(name + ": " + value); log(name + ": " + value);
} }
}; };
} }
private static void log(String msg) { private static void log(String msg) {
System.out.println(String.format("[%1$-10s] - %2$s", Thread.currentThread().getName(), msg)); System.out.println(String.format("[%1$-10s] - %2$s", Thread.currentThread().getName(), msg));
} }
} }

View File

@ -11,12 +11,11 @@ import java.util.Optional;
*/ */
public interface AsyncCallback<T> { public interface AsyncCallback<T> {
/** /**
* Complete handler which is executed when async task is completed or fails execution. * Complete handler which is executed when async task is completed or fails execution.
* *
* @param value the evaluated value from async task, undefined when execution fails * @param value the evaluated value from async task, undefined when execution fails
* @param ex empty value if execution succeeds, some exception if executions fails * @param ex empty value if execution succeeds, some exception if executions fails
*/ */
void onComplete(T value, Optional<Exception> ex); void onComplete(T value, Optional<Exception> ex);
} }

View File

@ -10,33 +10,32 @@ import java.util.concurrent.ExecutionException;
*/ */
public interface AsyncExecutor { public interface AsyncExecutor {
/** /**
* Starts processing of an async task. Returns immediately with async result. * Starts processing of an async task. Returns immediately with async result.
* *
* @param task task to be executed asynchronously * @param task task to be executed asynchronously
* @return async result for the task * @return async result for the task
*/ */
<T> AsyncResult<T> startProcess(Callable<T> task); <T> AsyncResult<T> startProcess(Callable<T> task);
/** /**
* Starts processing of an async task. Returns immediately with async result. Executes callback * Starts processing of an async task. Returns immediately with async result. Executes callback
* when the task is completed. * when the task is completed.
* *
* @param task task to be executed asynchronously * @param task task to be executed asynchronously
* @param callback callback to be executed on task completion * @param callback callback to be executed on task completion
* @return async result for the task * @return async result for the task
*/ */
<T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback); <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback);
/**
* Ends processing of an async task. Blocks the current thread if necessary and returns the
* evaluated value of the completed task.
*
* @param asyncResult async result of a task
* @return evaluated value of the completed task
* @throws ExecutionException if execution has failed, containing the root cause
* @throws InterruptedException if the execution is interrupted
*/
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
/**
* Ends processing of an async task. Blocks the current thread if necessary and returns the
* evaluated value of the completed task.
*
* @param asyncResult async result of a task
* @return evaluated value of the completed task
* @throws ExecutionException if execution has failed, containing the root cause
* @throws InterruptedException if the execution is interrupted
*/
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
} }

View File

@ -10,26 +10,26 @@ import java.util.concurrent.ExecutionException;
*/ */
public interface AsyncResult<T> { public interface AsyncResult<T> {
/** /**
* Status of the async task execution. * Status of the async task execution.
* *
* @return <code>true</code> if execution is completed or failed * @return <code>true</code> if execution is completed or failed
*/ */
boolean isCompleted(); boolean isCompleted();
/** /**
* Gets the value of completed async task. * Gets the value of completed async task.
* *
* @return evaluated value or throws ExecutionException if execution has failed * @return evaluated value or throws ExecutionException if execution has failed
* @throws ExecutionException if execution has failed, containing the root cause * @throws ExecutionException if execution has failed, containing the root cause
* @throws IllegalStateException if execution is not completed * @throws IllegalStateException if execution is not completed
*/ */
T getValue() throws ExecutionException; T getValue() throws ExecutionException;
/** /**
* Blocks the current thread until the async task is completed. * Blocks the current thread until the async task is completed.
* *
* @throws InterruptedException if the execution is interrupted * @throws InterruptedException if the execution is interrupted
*/ */
void await() throws InterruptedException; void await() throws InterruptedException;
} }

View File

@ -12,116 +12,117 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public class ThreadAsyncExecutor implements AsyncExecutor { public class ThreadAsyncExecutor implements AsyncExecutor {
/** Index for thread naming */ /** Index for thread naming */
private final AtomicInteger idx = new AtomicInteger(0); private final AtomicInteger idx = new AtomicInteger(0);
@Override @Override
public <T> AsyncResult<T> startProcess(Callable<T> task) { public <T> AsyncResult<T> startProcess(Callable<T> task) {
return startProcess(task, null); return startProcess(task, null);
} }
@Override @Override
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) { public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
CompletableResult<T> result = new CompletableResult<>(callback); CompletableResult<T> result = new CompletableResult<>(callback);
new Thread(() -> { new Thread(() -> {
try { try {
result.setValue(task.call()); result.setValue(task.call());
} catch (Exception ex) { } catch (Exception ex) {
result.setException(ex); result.setException(ex);
} }
}, "executor-" + idx.incrementAndGet()).start(); }, "executor-" + idx.incrementAndGet()).start();
return result; return result;
} }
@Override @Override
public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException { public <T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException,
if (asyncResult.isCompleted()) { InterruptedException {
return asyncResult.getValue(); if (asyncResult.isCompleted()) {
} else { return asyncResult.getValue();
asyncResult.await(); } else {
return asyncResult.getValue(); asyncResult.await();
} return asyncResult.getValue();
} }
}
/** /**
* Simple implementation of async result that allows completing it successfully with a value * Simple implementation of async result that allows completing it successfully with a value or
* or exceptionally with an exception. A really simplified version from its real life cousins * exceptionally with an exception. A really simplified version from its real life cousins
* FutureTask and CompletableFuture. * FutureTask and CompletableFuture.
* *
* @see java.util.concurrent.FutureTask * @see java.util.concurrent.FutureTask
* @see java.util.concurrent.CompletableFuture * @see java.util.concurrent.CompletableFuture
*/ */
private static class CompletableResult<T> implements AsyncResult<T> { private static class CompletableResult<T> implements AsyncResult<T> {
static final int RUNNING = 1; static final int RUNNING = 1;
static final int FAILED = 2; static final int FAILED = 2;
static final int COMPLETED = 3; static final int COMPLETED = 3;
final Object lock; final Object lock;
final Optional<AsyncCallback<T>> callback; final Optional<AsyncCallback<T>> callback;
volatile int state = RUNNING; volatile int state = RUNNING;
T value; T value;
Exception exception; Exception exception;
CompletableResult(AsyncCallback<T> callback) { CompletableResult(AsyncCallback<T> callback) {
this.lock = new Object(); this.lock = new Object();
this.callback = Optional.ofNullable(callback); this.callback = Optional.ofNullable(callback);
} }
/** /**
* Sets the value from successful execution and executes callback if available. Notifies * Sets the value from successful execution and executes callback if available. Notifies any
* any thread waiting for completion. * thread waiting for completion.
* *
* @param value value of the evaluated task * @param value value of the evaluated task
*/ */
void setValue(T value) { void setValue(T value) {
this.value = value; this.value = value;
this.state = COMPLETED; this.state = COMPLETED;
this.callback.ifPresent(ac -> ac.onComplete(value, Optional.<Exception>empty())); this.callback.ifPresent(ac -> ac.onComplete(value, Optional.<Exception>empty()));
synchronized (lock) { synchronized (lock) {
lock.notifyAll(); lock.notifyAll();
} }
} }
/** /**
* Sets the exception from failed execution and executes callback if available. Notifies * Sets the exception from failed execution and executes callback if available. Notifies any
* any thread waiting for completion. * thread waiting for completion.
* *
* @param exception exception of the failed task * @param exception exception of the failed task
*/ */
void setException(Exception exception) { void setException(Exception exception) {
this.exception = exception; this.exception = exception;
this.state = FAILED; this.state = FAILED;
this.callback.ifPresent(ac -> ac.onComplete(null, Optional.of(exception))); this.callback.ifPresent(ac -> ac.onComplete(null, Optional.of(exception)));
synchronized (lock) { synchronized (lock) {
lock.notifyAll(); lock.notifyAll();
} }
} }
@Override @Override
public boolean isCompleted() { public boolean isCompleted() {
return (state > RUNNING); return (state > RUNNING);
} }
@Override @Override
public T getValue() throws ExecutionException { public T getValue() throws ExecutionException {
if (state == COMPLETED) { if (state == COMPLETED) {
return value; return value;
} else if (state == FAILED) { } else if (state == FAILED) {
throw new ExecutionException(exception); throw new ExecutionException(exception);
} else { } else {
throw new IllegalStateException("Execution not completed yet"); throw new IllegalStateException("Execution not completed yet");
} }
} }
@Override @Override
public void await() throws InterruptedException { public void await() throws InterruptedException {
synchronized (lock) { synchronized (lock) {
if (!isCompleted()) { if (!isCompleted()) {
lock.wait(); lock.wait();
} }
} }
} }
} }
} }

View File

@ -9,10 +9,9 @@ import org.junit.Test;
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }
} }

View File

@ -9,6 +9,8 @@ tags:
- Gang Of Four - Gang Of Four
--- ---
**Also known as:** Handle/Body
**Intent:** Decouple an abstraction from its implementation so that the two can **Intent:** Decouple an abstraction from its implementation so that the two can
vary independently. vary independently.

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>bridge</artifactId> <artifactId>bridge</artifactId>
<dependencies> <dependencies>

View File

@ -2,41 +2,38 @@ package com.iluwatar.bridge;
/** /**
* *
* The Bridge pattern can also be thought of as two layers of abstraction. With Bridge, * The Bridge pattern can also be thought of as two layers of abstraction. With Bridge, you can
* you can decouple an abstraction from its implementation so that the two can vary independently. * decouple an abstraction from its implementation so that the two can vary independently.
* <p> * <p>
* In Bridge pattern both abstraction ({@link MagicWeapon}) and implementation * In Bridge pattern both abstraction ({@link MagicWeapon}) and implementation (
* ({@link MagicWeaponImpl}) have their own class hierarchies. The interface of the * {@link MagicWeaponImpl}) have their own class hierarchies. The interface of the implementations
* implementations can be changed without affecting the clients. * can be changed without affecting the clients.
* *
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon( public static void main(String[] args) {
new Excalibur()); BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
blindingMagicWeapon.wield(); blindingMagicWeapon.wield();
blindingMagicWeapon.blind(); blindingMagicWeapon.blind();
blindingMagicWeapon.swing(); blindingMagicWeapon.swing();
blindingMagicWeapon.unwield(); blindingMagicWeapon.unwield();
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon( FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
new Mjollnir()); flyingMagicWeapon.wield();
flyingMagicWeapon.wield(); flyingMagicWeapon.fly();
flyingMagicWeapon.fly(); flyingMagicWeapon.swing();
flyingMagicWeapon.swing(); flyingMagicWeapon.unwield();
flyingMagicWeapon.unwield();
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon( SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
new Stormbringer()); soulEatingMagicWeapon.wield();
soulEatingMagicWeapon.wield(); soulEatingMagicWeapon.swing();
soulEatingMagicWeapon.swing(); soulEatingMagicWeapon.eatSoul();
soulEatingMagicWeapon.eatSoul(); soulEatingMagicWeapon.unwield();
soulEatingMagicWeapon.unwield(); }
}
} }

View File

@ -7,32 +7,31 @@ package com.iluwatar.bridge;
*/ */
public class BlindingMagicWeapon extends MagicWeapon { public class BlindingMagicWeapon extends MagicWeapon {
public BlindingMagicWeapon(BlindingMagicWeaponImpl imp) { public BlindingMagicWeapon(BlindingMagicWeaponImpl imp) {
super(imp); super(imp);
} }
@Override @Override
public BlindingMagicWeaponImpl getImp() { public BlindingMagicWeaponImpl getImp() {
return (BlindingMagicWeaponImpl) imp; return (BlindingMagicWeaponImpl) imp;
} }
@Override @Override
public void wield() { public void wield() {
getImp().wieldImp(); getImp().wieldImp();
} }
@Override @Override
public void swing() { public void swing() {
getImp().swingImp(); getImp().swingImp();
} }
@Override @Override
public void unwield() { public void unwield() {
getImp().unwieldImp(); getImp().unwieldImp();
} }
public void blind() {
getImp().blindImp();
}
public void blind() {
getImp().blindImp();
}
} }

View File

@ -7,6 +7,6 @@ package com.iluwatar.bridge;
*/ */
public abstract class BlindingMagicWeaponImpl extends MagicWeaponImpl { public abstract class BlindingMagicWeaponImpl extends MagicWeaponImpl {
public abstract void blindImp(); public abstract void blindImp();
} }

View File

@ -7,25 +7,23 @@ package com.iluwatar.bridge;
*/ */
public class Excalibur extends BlindingMagicWeaponImpl { public class Excalibur extends BlindingMagicWeaponImpl {
@Override @Override
public void wieldImp() { public void wieldImp() {
System.out.println("wielding Excalibur"); System.out.println("wielding Excalibur");
} }
@Override @Override
public void swingImp() { public void swingImp() {
System.out.println("swinging Excalibur"); System.out.println("swinging Excalibur");
} }
@Override @Override
public void unwieldImp() { public void unwieldImp() {
System.out.println("unwielding Excalibur"); System.out.println("unwielding Excalibur");
} }
@Override
public void blindImp() {
System.out
.println("bright light streams from Excalibur blinding the enemy");
}
@Override
public void blindImp() {
System.out.println("bright light streams from Excalibur blinding the enemy");
}
} }

View File

@ -7,31 +7,31 @@ package com.iluwatar.bridge;
*/ */
public class FlyingMagicWeapon extends MagicWeapon { public class FlyingMagicWeapon extends MagicWeapon {
public FlyingMagicWeapon(FlyingMagicWeaponImpl imp) { public FlyingMagicWeapon(FlyingMagicWeaponImpl imp) {
super(imp); super(imp);
} }
public FlyingMagicWeaponImpl getImp() { public FlyingMagicWeaponImpl getImp() {
return (FlyingMagicWeaponImpl) imp; return (FlyingMagicWeaponImpl) imp;
} }
@Override @Override
public void wield() { public void wield() {
getImp().wieldImp(); getImp().wieldImp();
} }
@Override @Override
public void swing() { public void swing() {
getImp().swingImp(); getImp().swingImp();
} }
@Override @Override
public void unwield() { public void unwield() {
getImp().unwieldImp(); getImp().unwieldImp();
} }
public void fly() { public void fly() {
getImp().flyImp(); getImp().flyImp();
} }
} }

View File

@ -7,6 +7,6 @@ package com.iluwatar.bridge;
*/ */
public abstract class FlyingMagicWeaponImpl extends MagicWeaponImpl { public abstract class FlyingMagicWeaponImpl extends MagicWeaponImpl {
public abstract void flyImp(); public abstract void flyImp();
} }

View File

@ -7,20 +7,19 @@ package com.iluwatar.bridge;
*/ */
public abstract class MagicWeapon { public abstract class MagicWeapon {
protected MagicWeaponImpl imp; protected MagicWeaponImpl imp;
public MagicWeapon(MagicWeaponImpl imp) { public MagicWeapon(MagicWeaponImpl imp) {
this.imp = imp; this.imp = imp;
} }
public abstract void wield(); public abstract void wield();
public abstract void swing(); public abstract void swing();
public abstract void unwield(); public abstract void unwield();
public MagicWeaponImpl getImp() {
return imp;
}
public MagicWeaponImpl getImp() {
return imp;
}
} }

View File

@ -7,10 +7,10 @@ package com.iluwatar.bridge;
*/ */
public abstract class MagicWeaponImpl { public abstract class MagicWeaponImpl {
public abstract void wieldImp(); public abstract void wieldImp();
public abstract void swingImp(); public abstract void swingImp();
public abstract void unwieldImp(); public abstract void unwieldImp();
} }

View File

@ -7,25 +7,23 @@ package com.iluwatar.bridge;
*/ */
public class Mjollnir extends FlyingMagicWeaponImpl { public class Mjollnir extends FlyingMagicWeaponImpl {
@Override @Override
public void wieldImp() { public void wieldImp() {
System.out.println("wielding Mjollnir"); System.out.println("wielding Mjollnir");
} }
@Override @Override
public void swingImp() { public void swingImp() {
System.out.println("swinging Mjollnir"); System.out.println("swinging Mjollnir");
} }
@Override @Override
public void unwieldImp() { public void unwieldImp() {
System.out.println("unwielding Mjollnir"); System.out.println("unwielding Mjollnir");
} }
@Override
public void flyImp() {
System.out
.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
}
@Override
public void flyImp() {
System.out.println("Mjollnir hits the enemy in the air and returns back to the owner's hand");
}
} }

View File

@ -7,32 +7,32 @@ package com.iluwatar.bridge;
*/ */
public class SoulEatingMagicWeapon extends MagicWeapon { public class SoulEatingMagicWeapon extends MagicWeapon {
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImpl imp) { public SoulEatingMagicWeapon(SoulEatingMagicWeaponImpl imp) {
super(imp); super(imp);
} }
@Override @Override
public SoulEatingMagicWeaponImpl getImp() { public SoulEatingMagicWeaponImpl getImp() {
return (SoulEatingMagicWeaponImpl) imp; return (SoulEatingMagicWeaponImpl) imp;
} }
@Override @Override
public void wield() { public void wield() {
getImp().wieldImp(); getImp().wieldImp();
} }
@Override @Override
public void swing() { public void swing() {
getImp().swingImp(); getImp().swingImp();
} }
@Override @Override
public void unwield() { public void unwield() {
getImp().unwieldImp(); getImp().unwieldImp();
} }
public void eatSoul() { public void eatSoul() {
getImp().eatSoulImp(); getImp().eatSoulImp();
} }
} }

View File

@ -7,6 +7,6 @@ package com.iluwatar.bridge;
*/ */
public abstract class SoulEatingMagicWeaponImpl extends MagicWeaponImpl { public abstract class SoulEatingMagicWeaponImpl extends MagicWeaponImpl {
public abstract void eatSoulImp(); public abstract void eatSoulImp();
} }

View File

@ -7,24 +7,23 @@ package com.iluwatar.bridge;
*/ */
public class Stormbringer extends SoulEatingMagicWeaponImpl { public class Stormbringer extends SoulEatingMagicWeaponImpl {
@Override @Override
public void wieldImp() { public void wieldImp() {
System.out.println("wielding Stormbringer"); System.out.println("wielding Stormbringer");
} }
@Override @Override
public void swingImp() { public void swingImp() {
System.out.println("swinging Stormbringer"); System.out.println("swinging Stormbringer");
} }
@Override @Override
public void unwieldImp() { public void unwieldImp() {
System.out.println("unwielding Stormbringer"); System.out.println("unwielding Stormbringer");
} }
@Override
public void eatSoulImp() {
System.out.println("Stormbringer devours the enemy's soul");
}
@Override
public void eatSoulImp() {
System.out.println("Stormbringer devours the enemy's soul");
}
} }

View File

@ -11,9 +11,9 @@ import com.iluwatar.bridge.App;
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() { public void test() {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }
} }

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>builder</artifactId> <artifactId>builder</artifactId>
<dependencies> <dependencies>

View File

@ -1,55 +1,55 @@
package com.iluwatar.builder; package com.iluwatar.builder;
import com.iluwatar. builder.Hero.HeroBuilder; import com.iluwatar.builder.Hero.HeroBuilder;
/** /**
* *
* The intention of the Builder pattern is to find a solution to the telescoping * The intention of the Builder pattern is to find a solution to the telescoping constructor
* constructor anti-pattern. The telescoping constructor anti-pattern occurs when the * anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object
* increase of object constructor parameter combination leads to an exponential list * constructor parameter combination leads to an exponential list of constructors. Instead of using
* of constructors. Instead of using numerous constructors, the builder pattern uses * numerous constructors, the builder pattern uses another object, a builder, that receives each
* another object, a builder, that receives each initialization parameter step by step * initialization parameter step by step and then returns the resulting constructed object at once.
* and then returns the resulting constructed object at once.
* <p> * <p>
* The Builder pattern has another benefit. It can be used for objects that contain * The Builder pattern has another benefit. It can be used for objects that contain flat data (html
* flat data (html code, SQL query, X.509 certificate...), that is to say, data that * code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This
* can't be easily edited. This type of data cannot be edited step by step and must * type of data cannot be edited step by step and must be edited at once. The best way to construct
* be edited at once. The best way to construct such an object is to use a builder * such an object is to use a builder class.
* class.
* <p> * <p>
* In this example we have the Builder pattern variation as described by Joshua Bloch in * In this example we have the Builder pattern variation as described by Joshua Bloch in Effective
* Effective Java 2nd Edition. * Java 2nd Edition.
* <p> * <p>
* We want to build {@link Hero} objects, but its construction is complex because of the * We want to build {@link Hero} objects, but its construction is complex because of the many
* many parameters needed. To aid the user we introduce {@link HeroBuilder} class. * parameters needed. To aid the user we introduce {@link HeroBuilder} class. {@link HeroBuilder}
* {@link HeroBuilder} takes the minimum parameters to build {@link Hero} object in its * takes the minimum parameters to build {@link Hero} object in its constructor. After that
* constructor. After that additional configuration for the {@link Hero} object can be * additional configuration for the {@link Hero} object can be done using the fluent
* done using the fluent {@link HeroBuilder} interface. When configuration is ready the * {@link HeroBuilder} interface. When configuration is ready the build method is called to receive
* build method is called to receive the final {@link Hero} object. * the final {@link Hero} object.
* *
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
public static void main(String[] args) {
Hero mage = new HeroBuilder(Profession.MAGE, "Riobard") Hero mage =
.withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER) new HeroBuilder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK)
.build(); .withWeapon(Weapon.DAGGER).build();
System.out.println(mage); System.out.println(mage);
Hero warrior = new HeroBuilder(Profession.WARRIOR, "Amberjill") Hero warrior =
.withHairColor(HairColor.BLOND) new HeroBuilder(Profession.WARRIOR, "Amberjill").withHairColor(HairColor.BLOND)
.withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL) .withHairType(HairType.LONG_CURLY).withArmor(Armor.CHAIN_MAIL).withWeapon(Weapon.SWORD)
.withWeapon(Weapon.SWORD).build(); .build();
System.out.println(warrior); System.out.println(warrior);
Hero thief = new HeroBuilder(Profession.THIEF, "Desmond") Hero thief =
.withHairType(HairType.BALD).withWeapon(Weapon.BOW).build(); new HeroBuilder(Profession.THIEF, "Desmond").withHairType(HairType.BALD)
System.out.println(thief); .withWeapon(Weapon.BOW).build();
System.out.println(thief);
} }
} }

View File

@ -7,16 +7,16 @@ package com.iluwatar.builder;
*/ */
public enum Armor { public enum Armor {
CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail"); CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");
private String title; private String title;
Armor(String title) { Armor(String title) {
this.title = title; this.title = title;
} }
@Override @Override
public String toString() { public String toString() {
return title; return title;
} }
} }

View File

@ -7,11 +7,11 @@ package com.iluwatar.builder;
*/ */
public enum HairColor { public enum HairColor {
WHITE, BLOND, RED, BROWN, BLACK; WHITE, BLOND, RED, BROWN, BLACK;
@Override @Override
public String toString() { public String toString() {
return name().toLowerCase(); return name().toLowerCase();
} }
} }

View File

@ -7,16 +7,17 @@ package com.iluwatar.builder;
*/ */
public enum HairType { 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; private String title;
HairType(String title) { HairType(String title) {
this.title = title; this.title = title;
} }
@Override @Override
public String toString() { public String toString() {
return title; return title;
} }
} }

View File

@ -7,123 +7,122 @@ package com.iluwatar.builder;
*/ */
public class Hero { public class Hero {
private final Profession profession; private final Profession profession;
private final String name; private final String name;
private final HairType hairType; private final HairType hairType;
private final HairColor hairColor; private final HairColor hairColor;
private final Armor armor; private final Armor armor;
private final Weapon weapon; private final Weapon weapon;
public Profession getProfession() { public Profession getProfession() {
return profession; return profession;
} }
public String getName() { public String getName() {
return name; return name;
} }
public HairType getHairType() { public HairType getHairType() {
return hairType; return hairType;
} }
public HairColor getHairColor() { public HairColor getHairColor() {
return hairColor; return hairColor;
} }
public Armor getArmor() { public Armor getArmor() {
return armor; return armor;
} }
public Weapon getWeapon() { public Weapon getWeapon() {
return weapon; return weapon;
} }
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("This is a "); sb.append("This is a ");
sb.append(profession); sb.append(profession);
sb.append(" named "); sb.append(" named ");
sb.append(name); sb.append(name);
if (hairColor != null || hairType != null) { if (hairColor != null || hairType != null) {
sb.append(" with "); sb.append(" with ");
if (hairColor != null) { if (hairColor != null) {
sb.append(hairColor); sb.append(hairColor);
sb.append(" "); sb.append(" ");
} }
if (hairType != null) { if (hairType != null) {
sb.append(hairType); sb.append(hairType);
sb.append(" "); sb.append(" ");
} }
sb.append(hairType != HairType.BALD ? "hair" : "head"); sb.append(hairType != HairType.BALD ? "hair" : "head");
} }
if (armor != null) { if (armor != null) {
sb.append(" wearing "); sb.append(" wearing ");
sb.append(armor); sb.append(armor);
} }
if (weapon != null) { if (weapon != null) {
sb.append(" and wielding a "); sb.append(" and wielding a ");
sb.append(weapon); sb.append(weapon);
} }
sb.append("."); sb.append(".");
return sb.toString(); return sb.toString();
} }
private Hero(HeroBuilder builder) { private Hero(HeroBuilder builder) {
this.profession = builder.profession; this.profession = builder.profession;
this.name = builder.name; this.name = builder.name;
this.hairColor = builder.hairColor; this.hairColor = builder.hairColor;
this.hairType = builder.hairType; this.hairType = builder.hairType;
this.weapon = builder.weapon; this.weapon = builder.weapon;
this.armor = builder.armor; this.armor = builder.armor;
} }
/** /**
* *
* The builder class. * The builder class.
* *
*/ */
public static class HeroBuilder { public static class HeroBuilder {
private final Profession profession; private final Profession profession;
private final String name; private final String name;
private HairType hairType; private HairType hairType;
private HairColor hairColor; private HairColor hairColor;
private Armor armor; private Armor armor;
private Weapon weapon; private Weapon weapon;
public HeroBuilder(Profession profession, String name) { public HeroBuilder(Profession profession, String name) {
if (profession == null || name == null) { if (profession == null || name == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException("profession and name can not be null");
"profession and name can not be null"); }
} this.profession = profession;
this.profession = profession; this.name = name;
this.name = name; }
}
public HeroBuilder withHairType(HairType hairType) { public HeroBuilder withHairType(HairType hairType) {
this.hairType = hairType; this.hairType = hairType;
return this; return this;
} }
public HeroBuilder withHairColor(HairColor hairColor) { public HeroBuilder withHairColor(HairColor hairColor) {
this.hairColor = hairColor; this.hairColor = hairColor;
return this; return this;
} }
public HeroBuilder withArmor(Armor armor) { public HeroBuilder withArmor(Armor armor) {
this.armor = armor; this.armor = armor;
return this; return this;
} }
public HeroBuilder withWeapon(Weapon weapon) { public HeroBuilder withWeapon(Weapon weapon) {
this.weapon = weapon; this.weapon = weapon;
return this; return this;
} }
public Hero build() { public Hero build() {
return new Hero(this); return new Hero(this);
} }
} }
} }

View File

@ -7,11 +7,10 @@ package com.iluwatar.builder;
*/ */
public enum Profession { public enum Profession {
WARRIOR, THIEF, MAGE, PRIEST; WARRIOR, THIEF, MAGE, PRIEST;
@Override
public String toString() {
return name().toLowerCase();
}
@Override
public String toString() {
return name().toLowerCase();
}
} }

View File

@ -7,11 +7,10 @@ package com.iluwatar.builder;
*/ */
public enum Weapon { public enum Weapon {
DAGGER, SWORD, AXE, WARHAMMER, BOW; DAGGER, SWORD, AXE, WARHAMMER, BOW;
@Override
public String toString() {
return name().toLowerCase();
}
@Override
public String toString() {
return name().toLowerCase();
}
} }

View File

@ -2,7 +2,7 @@ package com.iluwatar.builder;
import org.junit.Test; import org.junit.Test;
import com.iluwatar. builder.App; import com.iluwatar.builder.App;
/** /**
* *
@ -11,9 +11,9 @@ import com.iluwatar. builder.App;
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() { public void test() {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }
} }

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>business-delegate</artifactId> <artifactId>business-delegate</artifactId>
<dependencies> <dependencies>
@ -15,5 +15,10 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,35 +1,40 @@
package com.iluwatar.business.delegate; 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. * <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
* By using the pattern we gain loose coupling between the tiers. The Business Delegate encapsulates * retrieved through service lookups. The Business Delegate itself may contain business logic too
* knowledge about how to locate, connect to, and interact with the business objects that make up * potentially tying together multiple service calls, exception handling, retrying etc.
* the application. *
* <p> * <p>In this example the client ({@link Client}) utilizes a business delegate (
* Some of the services the Business Delegate uses are instantiated directly, and some can be retrieved * {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
* through service lookups. The Business Delegate itself may contain business logic too potentially tying * service and makes the service call.
* 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.
*
*/ */
public class App { public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType(ServiceType.EJB);
Client client = new Client(businessDelegate); /**
client.doTask(); * Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
businessDelegate.setServiceType(ServiceType.JMS); BusinessDelegate businessDelegate = new BusinessDelegate();
client.doTask(); BusinessLookup businessLookup = new BusinessLookup();
} businessLookup.setEjbService(new EjbService());
businessLookup.setJmsService(new JmsService());
businessDelegate.setLookupService(businessLookup);
businessDelegate.setServiceType(ServiceType.EJB);
Client client = new Client(businessDelegate);
client.doTask();
businessDelegate.setServiceType(ServiceType.JMS);
client.doTask();
}
} }

View File

@ -1,22 +1,24 @@
package com.iluwatar.business.delegate; package com.iluwatar.business.delegate;
/** /**
*
* BusinessDelegate separates the presentation and business tiers * BusinessDelegate separates the presentation and business tiers
*
*/ */
public class BusinessDelegate { public class BusinessDelegate {
private BusinessLookup lookupService = new BusinessLookup();
private BusinessService businessService;
private ServiceType serviceType;
public void setServiceType(ServiceType serviceType) { private BusinessLookup lookupService;
this.serviceType = serviceType; private BusinessService businessService;
} private ServiceType serviceType;
public void doTask() { public void setLookupService(BusinessLookup businessLookup) {
businessService = lookupService.getBusinessService(serviceType); this.lookupService = businessLookup;
businessService.doProcessing(); }
}
public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
}
public void doTask() {
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
} }

View File

@ -1,17 +1,31 @@
package com.iluwatar.business.delegate; package com.iluwatar.business.delegate;
/** /**
* * Class for performing service lookups.
* Class for performing service lookups
*
*/ */
public class BusinessLookup { public class BusinessLookup {
public BusinessService getBusinessService(ServiceType serviceType) { private EjbService ejbService;
if (serviceType.equals(ServiceType.EJB)) {
return new EjbService(); private JmsService jmsService;
} else {
return new JmsService(); /**
} * @param serviceType Type of service instance to be returned.
} * @return Service instance.
*/
public BusinessService getBusinessService(ServiceType serviceType) {
if (serviceType.equals(ServiceType.EJB)) {
return ejbService;
} else {
return jmsService;
}
}
public void setJmsService(JmsService jmsService) {
this.jmsService = jmsService;
}
public void setEjbService(EjbService ejbService) {
this.ejbService = ejbService;
}
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.business.delegate;
*/ */
public interface BusinessService { public interface BusinessService {
void doProcessing(); void doProcessing();
} }

View File

@ -7,13 +7,13 @@ package com.iluwatar.business.delegate;
*/ */
public class Client { public class Client {
private BusinessDelegate businessDelegate; private BusinessDelegate businessDelegate;
public Client(BusinessDelegate businessDelegate) { public Client(BusinessDelegate businessDelegate) {
this.businessDelegate = businessDelegate; this.businessDelegate = businessDelegate;
} }
public void doTask() { public void doTask() {
businessDelegate.doTask(); businessDelegate.doTask();
} }
} }

View File

@ -7,8 +7,8 @@ package com.iluwatar.business.delegate;
*/ */
public class EjbService implements BusinessService { public class EjbService implements BusinessService {
@Override @Override
public void doProcessing() { public void doProcessing() {
System.out.println("EjbService is now processing"); System.out.println("EjbService is now processing");
} }
} }

View File

@ -7,8 +7,8 @@ package com.iluwatar.business.delegate;
*/ */
public class JmsService implements BusinessService { public class JmsService implements BusinessService {
@Override @Override
public void doProcessing() { public void doProcessing() {
System.out.println("JmsService is now processing"); System.out.println("JmsService is now processing");
} }
} }

View File

@ -6,6 +6,6 @@ package com.iluwatar.business.delegate;
* *
*/ */
public enum ServiceType { public enum ServiceType {
EJB, JMS; EJB, JMS;
} }

View File

@ -1,19 +0,0 @@
package com.iluwatar.business.delegate;
import org.junit.Test;
import com.iluwatar.business.delegate.App;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,78 @@
package com.iluwatar.business.delegate;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
/**
* 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.
*/
public class BusinessDelegateTest {
private EjbService ejbService;
private JmsService jmsService;
private BusinessLookup businessLookup;
private BusinessDelegate businessDelegate;
/**
* This method sets up the instance variables of this test class. It is executed before the
* execution of every test.
*/
@Before
public void setup() {
ejbService = spy(new EjbService());
jmsService = spy(new JmsService());
businessLookup = spy(new BusinessLookup());
businessLookup.setEjbService(ejbService);
businessLookup.setJmsService(jmsService);
businessDelegate = spy(new BusinessDelegate());
businessDelegate.setLookupService(businessLookup);
}
/**
* 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.
*/
@Test
public void testBusinessDelegate() {
// setup a client object
Client client = new Client(businessDelegate);
// set the service type
businessDelegate.setServiceType(ServiceType.EJB);
// action
client.doTask();
// verifying that the businessDelegate was used by client during doTask() method.
verify(businessDelegate).doTask();
verify(ejbService).doProcessing();
// set the service type
businessDelegate.setServiceType(ServiceType.JMS);
// action
client.doTask();
// verifying that the businessDelegate was used by client during doTask() method.
verify(businessDelegate, times(2)).doTask();
verify(jmsService).doProcessing();
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>caching</artifactId> <artifactId>caching</artifactId>
<dependencies> <dependencies>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>callback</artifactId> <artifactId>callback</artifactId>
<dependencies> <dependencies>

View File

@ -2,20 +2,21 @@ package com.iluwatar.callback;
/** /**
* *
* Callback pattern is more native for functional languages where functions are treated as first-class citizens. * Callback pattern is more native for functional languages where functions are treated as
* Prior to Java 8 callbacks can be simulated using simple (alike command) interfaces. * first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command)
* interfaces.
* *
*/ */
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
Task task = new SimpleTask(); Task task = new SimpleTask();
Callback callback = new Callback() { Callback callback = new Callback() {
@Override @Override
public void call() { public void call() {
System.out.println("I'm done now."); System.out.println("I'm done now.");
} }
}; };
task.executeWith(callback); task.executeWith(callback);
} }
} }

View File

@ -7,5 +7,5 @@ package com.iluwatar.callback;
*/ */
public interface Callback { public interface Callback {
public void call(); public void call();
} }

View File

@ -7,9 +7,8 @@ package com.iluwatar.callback;
*/ */
public class SimpleTask extends Task { public class SimpleTask extends Task {
@Override @Override
public void execute() { public void execute() {
System.out.println("Perform some important activity and after call the callback method."); System.out.println("Perform some important activity and after call the callback method.");
} }
} }

View File

@ -7,12 +7,12 @@ package com.iluwatar.callback;
*/ */
public abstract class Task { public abstract class Task {
public final void executeWith(Callback callback) { public final void executeWith(Callback callback) {
execute(); execute();
if (callback != null) { if (callback != null) {
callback.call(); callback.call();
} }
} }
public abstract void execute(); public abstract void execute();
} }

View File

@ -5,35 +5,35 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Add a field as a counter. Every time the callback method is called increment this * Add a field as a counter. Every time the callback method is called increment this field. Unit
* field. Unit test checks that the field is being incremented. * test checks that the field is being incremented.
* *
* Could be done with mock objects as well where the call method call is verified. * Could be done with mock objects as well where the call method call is verified.
*/ */
public class AppTest { public class AppTest {
private Integer callingCount = 0; private Integer callingCount = 0;
@Test @Test
public void test() { public void test() {
Callback callback = new Callback() { Callback callback = new Callback() {
@Override @Override
public void call() { public void call() {
callingCount++; callingCount++;
} }
}; };
Task task = new SimpleTask(); Task task = new SimpleTask();
assertEquals("Initial calling count of 0", new Integer(0), callingCount); assertEquals("Initial calling count of 0", new Integer(0), callingCount);
task.executeWith(callback); task.executeWith(callback);
assertEquals("Callback called once", new Integer(1), callingCount); assertEquals("Callback called once", new Integer(1), callingCount);
task.executeWith(callback); task.executeWith(callback);
assertEquals("Callback called twice", new Integer(2), callingCount); assertEquals("Callback called twice", new Integer(2), callingCount);
} }
} }

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>chain</artifactId> <artifactId>chain</artifactId>
<dependencies> <dependencies>

View File

@ -2,31 +2,30 @@ package com.iluwatar.chain;
/** /**
* *
* The Chain of Responsibility pattern is a design pattern consisting of command * The Chain of Responsibility pattern is a design pattern consisting of command objects and a
* objects and a series of processing objects. Each processing object contains * series of processing objects. Each processing object contains logic that defines the types of
* logic that defines the types of command objects that it can handle; the rest are * command objects that it can handle; the rest are passed to the next processing object in the
* passed to the next processing object in the chain. A mechanism also exists for * chain. A mechanism also exists for adding new processing objects to the end of this chain.
* adding new processing objects to the end of this chain.
* <p> * <p>
* In this example we organize the request handlers ({@link RequestHandler}) into a * In this example we organize the request handlers ({@link RequestHandler}) into a chain where each
* chain where each handler has a chance to act on the request on its turn. Here * handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) makes
* the king ({@link OrcKing}) makes requests and the military orcs ({@link OrcCommander}, * requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier})
* {@link OrcOfficer}, {@link OrcSoldier}) form the handler chain. * form the handler chain.
* *
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
public static void main(String[] args) {
OrcKing king = new OrcKing(); OrcKing king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
"torture prisoner")); king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
} }
} }

View File

@ -7,21 +7,21 @@ package com.iluwatar.chain;
*/ */
public class OrcCommander extends RequestHandler { public class OrcCommander extends RequestHandler {
public OrcCommander(RequestHandler handler) { public OrcCommander(RequestHandler handler) {
super(handler); super(handler);
} }
@Override @Override
public void handleRequest(Request req) { public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) { if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
printHandling(req); printHandling(req);
} else { } else {
super.handleRequest(req); super.handleRequest(req);
} }
} }
@Override @Override
public String toString() { public String toString() {
return "Orc commander"; return "Orc commander";
} }
} }

View File

@ -7,18 +7,18 @@ package com.iluwatar.chain;
*/ */
public class OrcKing { public class OrcKing {
RequestHandler chain; RequestHandler chain;
public OrcKing() { public OrcKing() {
buildChain(); buildChain();
} }
private void buildChain() { private void buildChain() {
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null))); chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
} }
public void makeRequest(Request req) { public void makeRequest(Request req) {
chain.handleRequest(req); chain.handleRequest(req);
} }
} }

View File

@ -7,22 +7,22 @@ package com.iluwatar.chain;
*/ */
public class OrcOfficer extends RequestHandler { public class OrcOfficer extends RequestHandler {
public OrcOfficer(RequestHandler handler) { public OrcOfficer(RequestHandler handler) {
super(handler); super(handler);
} }
@Override @Override
public void handleRequest(Request req) { public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) { if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
printHandling(req); printHandling(req);
} else { } else {
super.handleRequest(req); super.handleRequest(req);
} }
} }
@Override @Override
public String toString() { public String toString() {
return "Orc officer"; return "Orc officer";
} }
} }

View File

@ -7,21 +7,21 @@ package com.iluwatar.chain;
*/ */
public class OrcSoldier extends RequestHandler { public class OrcSoldier extends RequestHandler {
public OrcSoldier(RequestHandler handler) { public OrcSoldier(RequestHandler handler) {
super(handler); super(handler);
} }
@Override @Override
public void handleRequest(Request req) { public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) { if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
printHandling(req); printHandling(req);
} else { } else {
super.handleRequest(req); super.handleRequest(req);
} }
} }
@Override @Override
public String toString() { public String toString() {
return "Orc soldier"; return "Orc soldier";
} }
} }

View File

@ -7,32 +7,32 @@ package com.iluwatar.chain;
*/ */
public class Request { public class Request {
private String requestDescription; private String requestDescription;
private RequestType requestType; private RequestType requestType;
public Request(RequestType requestType, String requestDescription) { public Request(RequestType requestType, String requestDescription) {
this.setRequestType(requestType); this.setRequestType(requestType);
this.setRequestDescription(requestDescription); this.setRequestDescription(requestDescription);
} }
public String getRequestDescription() { public String getRequestDescription() {
return requestDescription; return requestDescription;
} }
public void setRequestDescription(String requestDescription) { public void setRequestDescription(String requestDescription) {
this.requestDescription = requestDescription; this.requestDescription = requestDescription;
} }
public RequestType getRequestType() { public RequestType getRequestType() {
return requestType; return requestType;
} }
public void setRequestType(RequestType requestType) { public void setRequestType(RequestType requestType) {
this.requestType = requestType; this.requestType = requestType;
} }
@Override @Override
public String toString() { public String toString() {
return getRequestDescription(); return getRequestDescription();
} }
} }

View File

@ -7,22 +7,22 @@ package com.iluwatar.chain;
*/ */
public abstract class RequestHandler { public abstract class RequestHandler {
private RequestHandler next; private RequestHandler next;
public RequestHandler(RequestHandler next) { public RequestHandler(RequestHandler next) {
this.next = next; this.next = next;
} }
public void handleRequest(Request req) { public void handleRequest(Request req) {
if (next != null) { if (next != null) {
next.handleRequest(req); next.handleRequest(req);
} }
} }
protected void printHandling(Request req) { protected void printHandling(Request req) {
System.out.println(this + " handling request \"" + req + "\""); System.out.println(this + " handling request \"" + req + "\"");
} }
@Override @Override
public abstract String toString(); public abstract String toString();
} }

View File

@ -7,6 +7,6 @@ package com.iluwatar.chain;
*/ */
public enum RequestType { public enum RequestType {
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
} }

View File

@ -11,9 +11,9 @@ import com.iluwatar.chain.App;
*/ */
public class AppTest { public class AppTest {
@Test @Test
public void test() { public void test() {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }
} }

View File

@ -9,6 +9,8 @@ tags:
- Gang Of Four - Gang Of Four
--- ---
**Also known as:** Action, Transaction
**Intent:** Encapsulate a request as an object, thereby letting you **Intent:** Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log requests, and parameterize clients with different requests, queue or log requests, and
support undoable operations. support undoable operations.

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>command</artifactId> <artifactId>command</artifactId>
<dependencies> <dependencies>
@ -14,5 +14,10 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -2,52 +2,54 @@ package com.iluwatar.command;
/** /**
* *
* The Command pattern is a behavioral design pattern in which an object is used to encapsulate all information * The Command pattern is a behavioral design pattern in which an object is used to encapsulate all
* needed to perform an action or trigger an event at a later time. This information includes the method name, * information needed to perform an action or trigger an event at a later time. This information
* the object that owns the method and values for the method parameters. * includes the method name, the object that owns the method and values for the method parameters.
* <p> * <p>
* Four terms always associated with the command pattern are command, receiver, invoker and client. A command * Four terms always associated with the command pattern are command, receiver, invoker and client.
* object (spell) knows about the receiver (target) and invokes a method of the receiver. Values for parameters of * A command object (spell) knows about the receiver (target) and invokes a method of the receiver.
* the receiver method are stored in the command. The receiver then does the work. An invoker object (wizard) * Values for parameters of the receiver method are stored in the command. The receiver then does
* knows how to execute a command, and optionally does bookkeeping about the command execution. The invoker * the work. An invoker object (wizard) knows how to execute a command, and optionally does
* does not know anything about a concrete command, it knows only about command interface. Both an invoker object * bookkeeping about the command execution. The invoker does not know anything about a concrete
* and several command objects are held by a client object (app). The client decides which commands to execute at * command, it knows only about command interface. Both an invoker object and several command
* which points. To execute a command, it passes the command object to the invoker object. * 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> * <p>
* In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of the previous * In other words, in this example the wizard casts spells on the goblin. The wizard keeps track of
* spells cast, so it is easy to undo them. In addition, the wizard keeps track of the spells undone, so they * the previous spells cast, so it is easy to undo them. In addition, the wizard keeps track of the
* can be redone. * spells undone, so they can be redone.
* *
* *
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
Wizard wizard = new Wizard(); public static void main(String[] args) {
Goblin goblin = new Goblin(); Wizard wizard = new Wizard();
Goblin goblin = new Goblin();
goblin.printStatus(); goblin.printStatus();
wizard.castSpell(new ShrinkSpell(), goblin); wizard.castSpell(new ShrinkSpell(), goblin);
goblin.printStatus(); goblin.printStatus();
wizard.castSpell(new InvisibilitySpell(), goblin); wizard.castSpell(new InvisibilitySpell(), goblin);
goblin.printStatus(); goblin.printStatus();
wizard.undoLastSpell(); wizard.undoLastSpell();
goblin.printStatus(); goblin.printStatus();
wizard.undoLastSpell(); wizard.undoLastSpell();
goblin.printStatus(); goblin.printStatus();
wizard.redoLastSpell(); wizard.redoLastSpell();
goblin.printStatus(); goblin.printStatus();
wizard.redoLastSpell(); wizard.redoLastSpell();
goblin.printStatus(); goblin.printStatus();
} }
} }

View File

@ -7,13 +7,13 @@ package com.iluwatar.command;
*/ */
public abstract class Command { public abstract class Command {
public abstract void execute(Target target); public abstract void execute(Target target);
public abstract void undo(); public abstract void undo();
public abstract void redo(); public abstract void redo();
@Override @Override
public abstract String toString(); public abstract String toString();
} }

View File

@ -7,14 +7,14 @@ package com.iluwatar.command;
*/ */
public class Goblin extends Target { public class Goblin extends Target {
public Goblin() { public Goblin() {
setSize(Size.NORMAL); setSize(Size.NORMAL);
setVisibility(Visibility.VISIBLE); setVisibility(Visibility.VISIBLE);
} }
@Override @Override
public String toString() { public String toString() {
return "Goblin"; return "Goblin";
} }
} }

View File

@ -7,30 +7,30 @@ package com.iluwatar.command;
*/ */
public class InvisibilitySpell extends Command { public class InvisibilitySpell extends Command {
private Target target; private Target target;
@Override @Override
public void execute(Target target) { public void execute(Target target) {
target.setVisibility(Visibility.INVISIBLE); target.setVisibility(Visibility.INVISIBLE);
this.target = target; this.target = target;
} }
@Override @Override
public void undo() { public void undo() {
if (target != null) { if (target != null) {
target.setVisibility(Visibility.VISIBLE); target.setVisibility(Visibility.VISIBLE);
} }
} }
@Override @Override
public void redo() { public void redo() {
if (target != null) { if (target != null) {
target.setVisibility(Visibility.INVISIBLE); target.setVisibility(Visibility.INVISIBLE);
} }
} }
@Override @Override
public String toString() { public String toString() {
return "Invisibility spell"; return "Invisibility spell";
} }
} }

View File

@ -7,32 +7,32 @@ package com.iluwatar.command;
*/ */
public class ShrinkSpell extends Command { public class ShrinkSpell extends Command {
private Size oldSize; private Size oldSize;
private Target target; private Target target;
@Override @Override
public void execute(Target target) { public void execute(Target target) {
oldSize = target.getSize(); oldSize = target.getSize();
target.setSize(Size.SMALL); target.setSize(Size.SMALL);
this.target = target; this.target = target;
} }
@Override @Override
public void undo() { public void undo() {
if (oldSize != null && target != null) { if (oldSize != null && target != null) {
Size temp = target.getSize(); Size temp = target.getSize();
target.setSize(oldSize); target.setSize(oldSize);
oldSize = temp; oldSize = temp;
} }
} }
@Override @Override
public void redo() { public void redo() {
undo(); undo();
} }
@Override @Override
public String toString() { public String toString() {
return "Shrink spell"; return "Shrink spell";
} }
} }

View File

@ -7,16 +7,16 @@ package com.iluwatar.command;
*/ */
public enum Size { public enum Size {
SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED(""); SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED("");
private String title;
Size(String title) { private String title;
this.title = title;
}
@Override Size(String title) {
public String toString() { this.title = title;
return title; }
}
@Override
public String toString() {
return title;
}
} }

View File

@ -7,32 +7,32 @@ package com.iluwatar.command;
*/ */
public abstract class Target { public abstract class Target {
private Size size; private Size size;
private Visibility visibility; private Visibility visibility;
public Size getSize() { public Size getSize() {
return size; return size;
} }
public void setSize(Size size) { public void setSize(Size size) {
this.size = size; this.size = size;
} }
public Visibility getVisibility() { public Visibility getVisibility() {
return visibility; return visibility;
} }
public void setVisibility(Visibility visibility) { public void setVisibility(Visibility visibility) {
this.visibility = visibility; this.visibility = visibility;
} }
@Override @Override
public abstract String toString(); public abstract String toString();
public void printStatus() { public void printStatus() {
System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, System.out.println(String.format("%s, [size=%s] [visibility=%s]", this, getSize(),
getSize(), getVisibility())); getVisibility()));
System.out.println(); System.out.println();
} }
} }

View File

@ -7,16 +7,16 @@ package com.iluwatar.command;
*/ */
public enum Visibility { public enum Visibility {
VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED(""); VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED("");
private String title; private String title;
Visibility(String title) { Visibility(String title) {
this.title = title; this.title = title;
} }
@Override @Override
public String toString() { public String toString() {
return title; return title;
} }
} }

View File

@ -10,38 +10,37 @@ import java.util.LinkedList;
*/ */
public class Wizard { public class Wizard {
private Deque<Command> undoStack = new LinkedList<>(); private Deque<Command> undoStack = new LinkedList<>();
private Deque<Command> redoStack = new LinkedList<>(); private Deque<Command> redoStack = new LinkedList<>();
public Wizard() { public Wizard() {}
}
public void castSpell(Command command, Target target) { public void castSpell(Command command, Target target) {
System.out.println(this + " casts " + command + " at " + target); System.out.println(this + " casts " + command + " at " + target);
command.execute(target); command.execute(target);
undoStack.offerLast(command); undoStack.offerLast(command);
} }
public void undoLastSpell() { public void undoLastSpell() {
if (!undoStack.isEmpty()) { if (!undoStack.isEmpty()) {
Command previousSpell = undoStack.pollLast(); Command previousSpell = undoStack.pollLast();
redoStack.offerLast(previousSpell); redoStack.offerLast(previousSpell);
System.out.println(this + " undoes " + previousSpell); System.out.println(this + " undoes " + previousSpell);
previousSpell.undo(); previousSpell.undo();
} }
} }
public void redoLastSpell() { public void redoLastSpell() {
if (!redoStack.isEmpty()) { if (!redoStack.isEmpty()) {
Command previousSpell = redoStack.pollLast(); Command previousSpell = redoStack.pollLast();
undoStack.offerLast(previousSpell); undoStack.offerLast(previousSpell);
System.out.println(this + " redoes " + previousSpell); System.out.println(this + " redoes " + previousSpell);
previousSpell.redo(); previousSpell.redo();
} }
} }
@Override @Override
public String toString() { public String toString() {
return "Wizard"; return "Wizard";
} }
} }

View File

@ -1,19 +0,0 @@
package com.iluwatar.command;
import org.junit.Test;
import com.iluwatar.command.App;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,71 @@
package com.iluwatar.command;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* 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.
*/
public class CommandTest {
private static final String GOBLIN = "Goblin";
/**
* This test verifies that when 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, it also verifies that the
* wizard keeps track of the spells undone, so they can be redone.
*/
@Test
public void testCommand() {
Wizard wizard = new Wizard();
Goblin goblin = new Goblin();
wizard.castSpell(new ShrinkSpell(), goblin);
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
wizard.castSpell(new InvisibilitySpell(), goblin);
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.INVISIBLE);
wizard.undoLastSpell();
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
wizard.undoLastSpell();
verifyGoblin(goblin, GOBLIN, Size.NORMAL, Visibility.VISIBLE);
wizard.redoLastSpell();
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE);
wizard.redoLastSpell();
verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.INVISIBLE);
}
/**
* This method asserts that the passed goblin object has the name as expectedName, size as
* expectedSize and visibility as expectedVisibility.
*
* @param goblin a goblin object whose state is to be verified against other parameters
* @param expectedName expectedName of the goblin
* @param expectedSize expected size of the goblin
* @param expectedVisibilty exepcted visibility of the goblin
*/
private void verifyGoblin(Goblin goblin, String expectedName, Size expectedSize,
Visibility expectedVisibilty) {
assertEquals("Goblin's name must be same as expectedName", expectedName, goblin.toString());
assertEquals("Goblin's size must be same as expectedSize", expectedSize, goblin.getSize());
assertEquals("Goblin's visibility must be same as expectedVisibility", expectedVisibilty,
goblin.getVisibility());
}
}

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.7.0</version> <version>1.8.0</version>
</parent> </parent>
<artifactId>composite</artifactId> <artifactId>composite</artifactId>
<dependencies> <dependencies>

View File

@ -1,33 +1,34 @@
package com.iluwatar.composite; package com.iluwatar.composite;
/** /**
* The Composite pattern is a partitioning design pattern. The Composite pattern * The Composite pattern is a partitioning design pattern. The Composite pattern describes that a
* describes that a group of objects is to be treated in the same way as a single * group of objects is to be treated in the same way as a single instance of an object. The intent
* instance of an object. The intent of a composite is to "compose" objects into * of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
* tree structures to represent part-whole hierarchies. Implementing the Composite * Implementing the Composite pattern lets clients treat individual objects and compositions
* pattern lets clients treat individual objects and compositions uniformly. * uniformly.
* <p> * <p>
* In this example we have sentences composed of words composed of letters. All of * In this example we have sentences composed of words composed of letters. All of the objects can
* the objects can be treated through the same interface ({@link LetterComposite}). * be treated through the same interface ({@link LetterComposite}).
* *
*/ */
public class App { public class App {
/** /**
* Program entry point * Program entry point
* @param args command line args *
*/ * @param args command line args
public static void main(String[] args) { */
System.out.println("Message from the orcs: "); public static void main(String[] args) {
System.out.println("Message from the orcs: ");
LetterComposite orcMessage = new Messenger().messageFromOrcs(); LetterComposite orcMessage = new Messenger().messageFromOrcs();
orcMessage.print(); orcMessage.print();
System.out.println("\n"); System.out.println("\n");
System.out.println("Message from the elves: "); System.out.println("Message from the elves: ");
LetterComposite elfMessage = new Messenger().messageFromElves(); LetterComposite elfMessage = new Messenger().messageFromElves();
elfMessage.print(); elfMessage.print();
} }
} }

Some files were not shown because too many files have changed in this diff Show More