Merge branch 'master' into event-sourcing
@ -36,4 +36,4 @@ notifications:
|
||||
on_failure: always # options: [always|never|change] default: always
|
||||
on_start: never # options: [always|never|change] default: always
|
||||
|
||||
sudo: false # route the build to the container-based infrastructure for a faster build
|
||||
sudo: required
|
@ -18,7 +18,107 @@ Kit
|
||||
Provide an interface for creating families of related or dependent
|
||||
objects without specifying their concrete classes.
|
||||
|
||||

|
||||
## Explanation
|
||||
Real world example
|
||||
|
||||
> To create a kingdom we need objects with common theme. Elven kingdom needs an Elven king, Elven castle and Elven army whereas Orcish kingdom needs an Orcish king, Orcish castle and Orcish army. There is a dependency between the objects in the kingdom.
|
||||
|
||||
In plain words
|
||||
|
||||
> A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the kingdom
|
||||
|
||||
```
|
||||
public interface Castle {
|
||||
String getDescription();
|
||||
}
|
||||
public interface King {
|
||||
String getDescription();
|
||||
}
|
||||
public interface Army {
|
||||
String getDescription();
|
||||
}
|
||||
|
||||
// Elven implementations ->
|
||||
public class ElfCastle implements Castle {
|
||||
static final String DESCRIPTION = "This is the Elven castle!";
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
public class ElfKing implements King {
|
||||
static final String DESCRIPTION = "This is the Elven king!";
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
public class ElfArmy implements Army {
|
||||
static final String DESCRIPTION = "This is the Elven Army!";
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
||||
|
||||
// Orcish implementations similarly...
|
||||
|
||||
```
|
||||
|
||||
Then we have the abstraction and implementations for the kingdom factory
|
||||
|
||||
```
|
||||
public interface KingdomFactory {
|
||||
Castle createCastle();
|
||||
King createKing();
|
||||
Army createArmy();
|
||||
}
|
||||
|
||||
public class ElfKingdomFactory implements KingdomFactory {
|
||||
public Castle createCastle() {
|
||||
return new ElfCastle();
|
||||
}
|
||||
public King createKing() {
|
||||
return new ElfKing();
|
||||
}
|
||||
public Army createArmy() {
|
||||
return new ElfArmy();
|
||||
}
|
||||
}
|
||||
|
||||
public class OrcKingdomFactory implements KingdomFactory {
|
||||
public Castle createCastle() {
|
||||
return new OrcCastle();
|
||||
}
|
||||
public King createKing() {
|
||||
return new OrcKing();
|
||||
}
|
||||
public Army createArmy() {
|
||||
return new OrcArmy();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now we have our abstract factory that lets us make family of related objects i.e. Elven kingdom factory creates Elven castle, king and army etc.
|
||||
|
||||
```
|
||||
KingdomFactory factory = new ElfKingdomFactory();
|
||||
Castle castle = factory.createCastle();
|
||||
King king = factory.createKing();
|
||||
Army army = factory.createArmy();
|
||||
|
||||
castle.getDescription(); // Output: This is the Elven castle!
|
||||
king.getDescription(); // Output: This is the Elven king!
|
||||
army.getDescription(); // Output: This is the Elven Army!
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Abstract Factory pattern when
|
||||
@ -41,9 +141,6 @@ Use the Abstract Factory pattern when
|
||||
|
||||
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
|
||||
|
||||
|
||||
|
||||
|
||||
## Real world examples
|
||||
|
||||
* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
|
||||
|
Before Width: | Height: | Size: 21 KiB |
@ -1,159 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<interface id="1" language="java" name="com.iluwatar.abstractfactory.Army" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Army.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="70" width="161" x="1407" y="300"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="2" language="java" name="com.iluwatar.abstractfactory.KingdomFactory" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/KingdomFactory.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="161" x="1001" y="300"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="3" language="java" name="com.iluwatar.abstractfactory.ElfCastle" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfCastle.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="96" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.abstractfactory.ElfKingdomFactory" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKingdomFactory.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="161" x="900" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.abstractfactory.ElfKing" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKing.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="498" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="6" language="java" name="com.iluwatar.abstractfactory.King" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/King.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="70" width="161" x="599" y="300"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="7" language="java" name="com.iluwatar.abstractfactory.OrcCastle" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcCastle.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="297" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="8" language="java" name="com.iluwatar.abstractfactory.OrcKing" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKing.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="699" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="9" language="java" name="com.iluwatar.abstractfactory.ElfArmy" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfArmy.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="1306" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="10" language="java" name="com.iluwatar.abstractfactory.OrcArmy" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcArmy.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="161" x="1507" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="11" language="java" name="com.iluwatar.abstractfactory.Castle" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Castle.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="70" width="161" x="197" y="300"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="12" language="java" name="com.iluwatar.abstractfactory.OrcKingdomFactory" project="abstract-factory"
|
||||
file="/abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKingdomFactory.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="165" x="1101" y="118"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="13">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<realization id="14">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="11"/>
|
||||
</realization>
|
||||
<realization id="15">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<realization id="16">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="11"/>
|
||||
</realization>
|
||||
<realization id="17">
|
||||
<end type="SOURCE" refId="10"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<realization id="18">
|
||||
<end type="SOURCE" refId="12"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<realization id="19">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<realization id="20">
|
||||
<end type="SOURCE" refId="9"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</realization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,89 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.abstractfactory {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
- army : Army
|
||||
- castle : Castle
|
||||
- king : King
|
||||
+ App()
|
||||
+ createKingdom(factory : KingdomFactory)
|
||||
+ getArmy() : Army
|
||||
~ getArmy(factory : KingdomFactory) : Army
|
||||
+ getCastle() : Castle
|
||||
~ getCastle(factory : KingdomFactory) : Castle
|
||||
+ getKing() : King
|
||||
~ getKing(factory : KingdomFactory) : King
|
||||
+ main(args : String[]) {static}
|
||||
- setArmy(army : Army)
|
||||
- setCastle(castle : Castle)
|
||||
- setKing(king : King)
|
||||
}
|
||||
interface Army {
|
||||
+ getDescription() : String {abstract}
|
||||
}
|
||||
interface Castle {
|
||||
+ getDescription() : String {abstract}
|
||||
}
|
||||
class ElfArmy {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ ElfArmy()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class ElfCastle {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ ElfCastle()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class ElfKing {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ ElfKing()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class ElfKingdomFactory {
|
||||
+ ElfKingdomFactory()
|
||||
+ createArmy() : Army
|
||||
+ createCastle() : Castle
|
||||
+ createKing() : King
|
||||
}
|
||||
interface King {
|
||||
+ getDescription() : String {abstract}
|
||||
}
|
||||
interface KingdomFactory {
|
||||
+ createArmy() : Army {abstract}
|
||||
+ createCastle() : Castle {abstract}
|
||||
+ createKing() : King {abstract}
|
||||
}
|
||||
class OrcArmy {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ OrcArmy()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class OrcCastle {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ OrcCastle()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class OrcKing {
|
||||
~ DESCRIPTION : String {static}
|
||||
+ OrcKing()
|
||||
+ getDescription() : String
|
||||
}
|
||||
class OrcKingdomFactory {
|
||||
+ OrcKingdomFactory()
|
||||
+ createArmy() : Army
|
||||
+ createCastle() : Castle
|
||||
+ createKing() : King
|
||||
}
|
||||
}
|
||||
App --> "-castle" Castle
|
||||
App --> "-king" King
|
||||
App --> "-army" Army
|
||||
ElfArmy ..|> Army
|
||||
ElfCastle ..|> Castle
|
||||
ElfKing ..|> King
|
||||
ElfKingdomFactory ..|> KingdomFactory
|
||||
OrcArmy ..|> Army
|
||||
OrcCastle ..|> Castle
|
||||
OrcKing ..|> King
|
||||
OrcKingdomFactory ..|> KingdomFactory
|
||||
@enduml
|
Before Width: | Height: | Size: 57 KiB |
@ -19,10 +19,85 @@ Convert the interface of a class into another interface the clients
|
||||
expect. Adapter lets classes work together that couldn't otherwise because of
|
||||
incompatible interfaces.
|
||||
|
||||

|
||||
## Explanation
|
||||
|
||||
## General usage of Adapter Pattern:
|
||||
+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||
Real world example
|
||||
|
||||
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
|
||||
> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
|
||||
> Yet another example would be a translator translating words spoken by one person to another
|
||||
|
||||
In plain words
|
||||
|
||||
> Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Consider a captain that can only use rowing boats and cannot sail at all.
|
||||
|
||||
First we have interfaces `RowingBoat` and `FishingBoat`
|
||||
|
||||
```
|
||||
public interface RowingBoat {
|
||||
void row();
|
||||
}
|
||||
|
||||
public class FishingBoat {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
|
||||
public void sail() {
|
||||
LOGGER.info("The fishing boat is sailing");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And captain expects an implementation of `RowingBoat` interface to be able to move
|
||||
|
||||
```
|
||||
public class Captain implements RowingBoat {
|
||||
|
||||
private RowingBoat rowingBoat;
|
||||
|
||||
public Captain(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void row() {
|
||||
rowingBoat.row();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
|
||||
|
||||
```
|
||||
public class FishingBoatAdapter implements RowingBoat {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
|
||||
|
||||
private FishingBoat boat;
|
||||
|
||||
public FishingBoatAdapter() {
|
||||
boat = new FishingBoat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void row() {
|
||||
boat.sail();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And now the `Captain` can use the `FishingBoat` to escape the pirates.
|
||||
|
||||
```
|
||||
Captain captain = new Captain(new FishingBoatAdapter());
|
||||
captain.row();
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Adapter pattern when
|
||||
@ -30,6 +105,7 @@ Use the Adapter pattern when
|
||||
* you want to use an existing class, and its interface does not match the one you need
|
||||
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
||||
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
|
||||
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||
|
||||
## Consequences:
|
||||
Class and object adapters have different trade-offs. A class adapter
|
||||
|
Before Width: | Height: | Size: 12 KiB |
@ -1,70 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.adapter.FishingBoat" project="adapter"
|
||||
file="/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="656" y="355"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.adapter.Captain" project="adapter"
|
||||
file="/adapter/src/main/java/com/iluwatar/adapter/Captain.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="228" y="185"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.adapter.BattleFishingBoat" project="adapter"
|
||||
file="/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="463" y="357"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="4" language="java" name="com.iluwatar.adapter.BattleShip" project="adapter"
|
||||
file="/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="466" y="170"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<realization id="5">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</realization>
|
||||
<association id="6">
|
||||
<end type="SOURCE" refId="3" navigable="false">
|
||||
<attribute id="7" name="boat"/>
|
||||
<multiplicity id="8" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="9">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</realization>
|
||||
<association id="10">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="11" name="battleship"/>
|
||||
<multiplicity id="12" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,37 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.adapter {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class BattleFishingBoat {
|
||||
- LOGGER : Logger {static}
|
||||
- boat : FishingBoat
|
||||
+ BattleFishingBoat()
|
||||
+ fire()
|
||||
+ move()
|
||||
}
|
||||
interface BattleShip {
|
||||
+ fire() {abstract}
|
||||
+ move() {abstract}
|
||||
}
|
||||
class Captain {
|
||||
- battleship : BattleShip
|
||||
+ Captain()
|
||||
+ Captain(battleship : BattleShip)
|
||||
+ fire()
|
||||
+ move()
|
||||
+ setBattleship(battleship : BattleShip)
|
||||
}
|
||||
class FishingBoat {
|
||||
- LOGGER : Logger {static}
|
||||
+ FishingBoat()
|
||||
+ fish()
|
||||
+ sail()
|
||||
}
|
||||
}
|
||||
BattleFishingBoat --> "-boat" FishingBoat
|
||||
Captain --> "-battleship" BattleShip
|
||||
BattleFishingBoat ..|> BattleShip
|
||||
Captain ..|> BattleShip
|
||||
@enduml
|
@ -34,14 +34,14 @@ package com.iluwatar.adapter;
|
||||
* object. This example uses the object adapter approach.
|
||||
*
|
||||
* <p>
|
||||
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class (
|
||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ).
|
||||
* The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class (
|
||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link RowingBoat} ).
|
||||
*
|
||||
* <p>
|
||||
* The story of this implementation is this. <br>
|
||||
* Pirates are coming! we need a {@link BattleShip} to fight! We have a {@link FishingBoat} and our
|
||||
* Pirates are coming! we need a {@link RowingBoat} to flee! We have a {@link FishingBoat} and our
|
||||
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The
|
||||
* captain needs a battleship which can fire and move. The spec is in {@link BattleShip}. We will
|
||||
* captain needs a rowing boat which he can operate. The spec is in {@link RowingBoat}. We will
|
||||
* use the Adapter pattern to reuse {@link FishingBoat}.
|
||||
*
|
||||
*/
|
||||
@ -53,8 +53,8 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Captain captain = new Captain(new BattleFishingBoat());
|
||||
captain.move();
|
||||
captain.fire();
|
||||
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well
|
||||
Captain captain = new Captain(new FishingBoatAdapter());
|
||||
captain.row();
|
||||
}
|
||||
}
|
||||
|
@ -23,33 +23,26 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
* The Captain uses {@link BattleShip} to fight. <br>
|
||||
* The Captain uses {@link RowingBoat} to sail. <br>
|
||||
* This is the client in the pattern.
|
||||
*/
|
||||
public class Captain implements BattleShip {
|
||||
public class Captain implements RowingBoat {
|
||||
|
||||
private BattleShip battleship;
|
||||
private RowingBoat rowingBoat;
|
||||
|
||||
public Captain() {
|
||||
public Captain() {}
|
||||
|
||||
public Captain(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
}
|
||||
|
||||
public Captain(BattleShip battleship) {
|
||||
this.battleship = battleship;
|
||||
}
|
||||
|
||||
public void setBattleship(BattleShip battleship) {
|
||||
this.battleship = battleship;
|
||||
public void setRowingBoat(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
battleship.fire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
battleship.move();
|
||||
public void row() {
|
||||
rowingBoat.row();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Device class (adaptee in the pattern). We want to reuse this class
|
||||
* Device class (adaptee in the pattern). We want to reuse this class.
|
||||
* Fishing boat moves by sailing.
|
||||
*
|
||||
*/
|
||||
public class FishingBoat {
|
||||
@ -35,11 +36,7 @@ public class FishingBoat {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
|
||||
|
||||
public void sail() {
|
||||
LOGGER.info("The Boat is moving to that place");
|
||||
}
|
||||
|
||||
public void fish() {
|
||||
LOGGER.info("fishing ...");
|
||||
LOGGER.info("The fishing boat is sailing");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,35 +22,22 @@
|
||||
*/
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
|
||||
* interface expected by the client ({@link Captain}). <br>
|
||||
* In this case we added a new function fire to suit the interface. We are reusing the
|
||||
* {@link FishingBoat} without changing itself. The Adapter class can just map the functions of the
|
||||
* Adaptee or add, delete features of the Adaptee.
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat}
|
||||
* interface expected by the client ({@link Captain}).
|
||||
*
|
||||
*/
|
||||
public class BattleFishingBoat implements BattleShip {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BattleFishingBoat.class);
|
||||
public class FishingBoatAdapter implements RowingBoat {
|
||||
|
||||
private FishingBoat boat;
|
||||
|
||||
public BattleFishingBoat() {
|
||||
public FishingBoatAdapter() {
|
||||
boat = new FishingBoat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
LOGGER.info("fire!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
public void row() {
|
||||
boat.sail();
|
||||
}
|
||||
}
|
@ -24,13 +24,11 @@ package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
* The interface expected by the client.<br>
|
||||
* A Battleship can fire and move.
|
||||
* A rowing boat is rowed to move.
|
||||
*
|
||||
*/
|
||||
public interface BattleShip {
|
||||
public interface RowingBoat {
|
||||
|
||||
void fire();
|
||||
|
||||
void move();
|
||||
void row();
|
||||
|
||||
}
|
@ -39,9 +39,9 @@ public class AdapterPatternTest {
|
||||
|
||||
private Map<String, Object> beans;
|
||||
|
||||
private static final String BATTLESHIP_BEAN = "engineer";
|
||||
private static final String FISHING_BEAN = "fisher";
|
||||
|
||||
private static final String CAPTAIN_BEAN = "captain";
|
||||
private static final String ROWING_BEAN = "captain";
|
||||
|
||||
/**
|
||||
* This method runs before the test execution and sets the bean objects in the beans Map.
|
||||
@ -50,34 +50,29 @@ public class AdapterPatternTest {
|
||||
public void setup() {
|
||||
beans = new HashMap<>();
|
||||
|
||||
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
|
||||
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
|
||||
FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter());
|
||||
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
||||
|
||||
Captain captain = new Captain();
|
||||
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
|
||||
beans.put(CAPTAIN_BEAN, captain);
|
||||
captain.setRowingBoat((FishingBoatAdapter) beans.get(FISHING_BEAN));
|
||||
beans.put(ROWING_BEAN, captain);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test asserts that when we use the move() method on a captain bean(client), it is
|
||||
* internally calling move method on the battleship object. The Adapter ({@link BattleFishingBoat}
|
||||
* This test asserts that when we use the row() method on a captain bean(client), it is
|
||||
* internally calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter}
|
||||
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
||||
* expected by the client ({@link Captain} ).
|
||||
*/
|
||||
@Test
|
||||
public void testAdapter() {
|
||||
BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN);
|
||||
RowingBoat captain = (RowingBoat) beans.get(ROWING_BEAN);
|
||||
|
||||
// when captain moves
|
||||
captain.move();
|
||||
captain.row();
|
||||
|
||||
// the captain internally calls the battleship object to move
|
||||
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
|
||||
verify(battleship).move();
|
||||
|
||||
// same with above with firing
|
||||
captain.fire();
|
||||
verify(battleship).fire();
|
||||
|
||||
RowingBoat adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
||||
verify(adapter).row();
|
||||
}
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ public class ThreadAsyncExecutor implements AsyncExecutor {
|
||||
@Override
|
||||
public void await() throws InterruptedException {
|
||||
synchronized (lock) {
|
||||
if (!isCompleted()) {
|
||||
while (!isCompleted()) {
|
||||
lock.wait();
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,103 @@ Separate the construction of a complex object from its
|
||||
representation so that the same construction process can create different
|
||||
representations.
|
||||
|
||||

|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> Imagine a character generator for a role playing game. The easiest option is to let computer create the character for you. But if you want to select the character details like profession, gender, hair color etc. the character generation becomes a step-by-step process that completes when all the selections are ready.
|
||||
|
||||
In plain words
|
||||
|
||||
> Allows you to create different flavors of an object while avoiding constructor pollution. Useful when there could be several flavors of an object. Or when there are a lot of steps involved in creation of an object.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern.
|
||||
|
||||
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point or the other we have all seen a constructor like below:
|
||||
|
||||
```
|
||||
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
|
||||
}
|
||||
```
|
||||
|
||||
As you can see the number of constructor parameters can quickly get out of hand and it might become difficult to understand the arrangement of parameters. Plus this parameter list could keep on growing if you would want to add more options in future. This is called telescoping constructor anti-pattern.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create
|
||||
|
||||
```
|
||||
public final class Hero {
|
||||
private final Profession profession;
|
||||
private final String name;
|
||||
private final HairType hairType;
|
||||
private final HairColor hairColor;
|
||||
private final Armor armor;
|
||||
private final Weapon weapon;
|
||||
|
||||
private Hero(Builder builder) {
|
||||
this.profession = builder.profession;
|
||||
this.name = builder.name;
|
||||
this.hairColor = builder.hairColor;
|
||||
this.hairType = builder.hairType;
|
||||
this.weapon = builder.weapon;
|
||||
this.armor = builder.armor;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And then we have the builder
|
||||
|
||||
```
|
||||
public static class Builder {
|
||||
private final Profession profession;
|
||||
private final String name;
|
||||
private HairType hairType;
|
||||
private HairColor hairColor;
|
||||
private Armor armor;
|
||||
private Weapon weapon;
|
||||
|
||||
public Builder(Profession profession, String name) {
|
||||
if (profession == null || name == null) {
|
||||
throw new IllegalArgumentException("profession and name can not be null");
|
||||
}
|
||||
this.profession = profession;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Builder withHairType(HairType hairType) {
|
||||
this.hairType = hairType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withHairColor(HairColor hairColor) {
|
||||
this.hairColor = hairColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withArmor(Armor armor) {
|
||||
this.armor = armor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withWeapon(Weapon weapon) {
|
||||
this.weapon = weapon;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Hero build() {
|
||||
return new Hero(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And then it can be used as:
|
||||
|
||||
```
|
||||
Hero mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Builder pattern when
|
||||
|
Before Width: | Height: | Size: 52 KiB |
@ -1,162 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<enumeration id="1" language="java" name="com.iluwatar.builder.HairType" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/HairType.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="214" width="189" x="177" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="2" language="java" name="com.iluwatar.builder.Weapon" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Weapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="171" x="406" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="3" language="java" name="com.iluwatar.builder.HairColor" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/HairColor.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="144" x="-204" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="4" language="java" name="com.iluwatar.builder.Hero.Builder" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="234" x="20" y="183"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<enumeration id="5" language="java" name="com.iluwatar.builder.Armor" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Armor.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="145" x="617" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<enumeration id="6" language="java" name="com.iluwatar.builder.Profession" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Profession.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="178" width="157" x="-20" y="419"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="7" language="java" name="com.iluwatar.builder.Hero" project="builder"
|
||||
file="/builder/src/main/java/com/iluwatar/builder/Hero.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="232" width="178" x="275" y="-89"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="8">
|
||||
<bendpoint x="284" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="9" name="hairType"/>
|
||||
<multiplicity id="10" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="11">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="12" name="weapon"/>
|
||||
<multiplicity id="13" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="14">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="15" name="profession"/>
|
||||
<multiplicity id="16" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="17">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="18" name="hairColor"/>
|
||||
<multiplicity id="19" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="20">
|
||||
<bendpoint x="350" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="21" name="weapon"/>
|
||||
<multiplicity id="22" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="23">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="24" name="hairType"/>
|
||||
<multiplicity id="25" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="1" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<nesting id="26">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</nesting>
|
||||
<association id="27">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="28" name="armor"/>
|
||||
<multiplicity id="29" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="30">
|
||||
<bendpoint x="-11" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="31" name="profession"/>
|
||||
<multiplicity id="32" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="33">
|
||||
<bendpoint x="-32" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="34" name="hairColor"/>
|
||||
<multiplicity id="35" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="36">
|
||||
<bendpoint x="455" y="183"/>
|
||||
<end type="SOURCE" refId="7" navigable="false">
|
||||
<attribute id="37" name="armor"/>
|
||||
<multiplicity id="38" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,100 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.builder {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
enum Armor {
|
||||
+ CHAIN_MAIL {static}
|
||||
+ CLOTHES {static}
|
||||
+ LEATHER {static}
|
||||
+ PLATE_MAIL {static}
|
||||
- title : String
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Armor {static}
|
||||
+ values() : Armor[] {static}
|
||||
}
|
||||
enum HairColor {
|
||||
+ BLACK {static}
|
||||
+ BLOND {static}
|
||||
+ BROWN {static}
|
||||
+ RED {static}
|
||||
+ WHITE {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : HairColor {static}
|
||||
+ values() : HairColor[] {static}
|
||||
}
|
||||
enum HairType {
|
||||
+ BALD {static}
|
||||
+ CURLY {static}
|
||||
+ LONG_CURLY {static}
|
||||
+ LONG_STRAIGHT {static}
|
||||
+ SHORT {static}
|
||||
- title : String
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : HairType {static}
|
||||
+ values() : HairType[] {static}
|
||||
}
|
||||
class Hero {
|
||||
- armor : Armor
|
||||
- hairColor : HairColor
|
||||
- hairType : HairType
|
||||
- name : String
|
||||
- profession : Profession
|
||||
- weapon : Weapon
|
||||
- Hero(builder : Builder)
|
||||
+ getArmor() : Armor
|
||||
+ getHairColor() : HairColor
|
||||
+ getHairType() : HairType
|
||||
+ getName() : String
|
||||
+ getProfession() : Profession
|
||||
+ getWeapon() : Weapon
|
||||
+ toString() : String
|
||||
}
|
||||
class Builder {
|
||||
- armor : Armor
|
||||
- hairColor : HairColor
|
||||
- hairType : HairType
|
||||
- name : String
|
||||
- profession : Profession
|
||||
- weapon : Weapon
|
||||
+ Builder(profession : Profession, name : String)
|
||||
+ build() : Hero
|
||||
+ withArmor(armor : Armor) : Builder
|
||||
+ withHairColor(hairColor : HairColor) : Builder
|
||||
+ withHairType(hairType : HairType) : Builder
|
||||
+ withWeapon(weapon : Weapon) : Builder
|
||||
}
|
||||
enum Profession {
|
||||
+ MAGE {static}
|
||||
+ PRIEST {static}
|
||||
+ THIEF {static}
|
||||
+ WARRIOR {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Profession {static}
|
||||
+ values() : Profession[] {static}
|
||||
}
|
||||
enum Weapon {
|
||||
+ AXE {static}
|
||||
+ BOW {static}
|
||||
+ DAGGER {static}
|
||||
+ SWORD {static}
|
||||
+ WARHAMMER {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : Weapon {static}
|
||||
+ values() : Weapon[] {static}
|
||||
}
|
||||
}
|
||||
Builder ..+ Hero
|
||||
Hero --> "-profession" Profession
|
||||
Hero --> "-armor" Armor
|
||||
Builder --> "-hairColor" HairColor
|
||||
Builder --> "-weapon" Weapon
|
||||
Builder --> "-hairType" HairType
|
||||
Hero --> "-hairColor" HairColor
|
||||
Builder --> "-profession" Profession
|
||||
Hero --> "-hairType" HairType
|
||||
Hero --> "-weapon" Weapon
|
||||
Builder --> "-armor" Armor
|
||||
@enduml
|
Before Width: | Height: | Size: 104 KiB |
@ -65,8 +65,8 @@ public class DbCustomerDao implements CustomerDao {
|
||||
Connection connection;
|
||||
try {
|
||||
connection = getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); //NOSONAR
|
||||
ResultSet resultSet = statement.executeQuery(); //NOSONAR
|
||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||
Spliterator.ORDERED) {
|
||||
|
||||
@ -82,7 +82,7 @@ public class DbCustomerDao implements CustomerDao {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}, false).onClose(() -> mutedClose(connection));
|
||||
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
|
||||
} catch (SQLException e) {
|
||||
throw new Exception(e.getMessage(), e);
|
||||
}
|
||||
@ -92,8 +92,10 @@ public class DbCustomerDao implements CustomerDao {
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
private void mutedClose(Connection connection) {
|
||||
private void mutedClose(Connection connection, PreparedStatement statement, ResultSet resultSet) {
|
||||
try {
|
||||
resultSet.close();
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
30
data-transfer-object/README.md
Normal file
@ -0,0 +1,30 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Transfer Object
|
||||
folder: data-transfer-object
|
||||
permalink: /patterns/data-transfer-object/
|
||||
categories: Architectural
|
||||
tags:
|
||||
- Java
|
||||
- KISS
|
||||
- YAGNI
|
||||
- Difficulty-Beginner
|
||||
---
|
||||
|
||||
## Intent
|
||||
Pass data with multiple attributes in one shot from client to server,
|
||||
to avoid multiple calls to remote server.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Data Transfer Object pattern when
|
||||
|
||||
* The client is asking for multiple information. And the information is related.
|
||||
* When you want to boost the performance to get resources.
|
||||
* You want reduced number of remote calls.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Design Pattern - Transfer Object Pattern](https://www.tutorialspoint.com/design_pattern/transfer_object_pattern.htm)
|
||||
* [Data Transfer Object](https://msdn.microsoft.com/en-us/library/ff649585.aspx)
|
48
data-transfer-object/etc/data-transfer-object.ucls
Normal file
@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.2.0" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.datatransfer.CustomerClientApp" project="data-transfer-object"
|
||||
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerClientApp.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="145" y="93"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.datatransfer.CustomerDto" project="data-transfer-object"
|
||||
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerDto.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="386" y="329"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.datatransfer.CustomerResource" project="data-transfer-object"
|
||||
file="/data-transfer-object/src/main/java/com/iluwatar/datatransfer/CustomerResource.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="677" y="93"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="4">
|
||||
<end type="SOURCE" refId="3" navigable="false">
|
||||
<attribute id="5" name="customers"/>
|
||||
<multiplicity id="6" minimum="0" maximum="2147483647"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
BIN
data-transfer-object/etc/data-transfer-object.urm.png
Normal file
After Width: | Height: | Size: 19 KiB |
26
data-transfer-object/etc/data-transfer-object.urm.puml
Normal file
@ -0,0 +1,26 @@
|
||||
@startuml
|
||||
package com.iluwatar.datatransfer {
|
||||
class CustomerClientApp {
|
||||
+ CustomerClientApp()
|
||||
+ main(args : String[]) {static}
|
||||
- printCustomerDetails(allCustomers : List<CustomerDto>) {static}
|
||||
}
|
||||
class CustomerDto {
|
||||
- firstName : String
|
||||
- id : String
|
||||
- lastName : String
|
||||
+ CustomerDto(id : String, firstName : String, lastName : String)
|
||||
+ getFirstName() : String
|
||||
+ getId() : String
|
||||
+ getLastName() : String
|
||||
}
|
||||
class CustomerResource {
|
||||
- customers : List<CustomerDto>
|
||||
+ CustomerResource(customers : List<CustomerDto>)
|
||||
+ delete(customerId : String)
|
||||
+ getAllCustomers() : List<CustomerDto>
|
||||
+ save(customer : CustomerDto)
|
||||
}
|
||||
}
|
||||
CustomerResource --> "-customers" CustomerDto
|
||||
@enduml
|
45
data-transfer-object/pom.xml
Normal file
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2016 Gopinath Langote
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.17.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-transfer-object</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Gopinath Langote
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to serve related
|
||||
* information together to avoid multiple call for each piece of information.
|
||||
* <p>
|
||||
* In this example, ({@link CustomerClientApp}) as as customer details consumer i.e. client to request for
|
||||
* customer details to server.
|
||||
* <p>
|
||||
* CustomerResource ({@link CustomerResource}) act as server to serve customer information.
|
||||
* And The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
|
||||
*/
|
||||
public class CustomerClientApp {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerClientApp.class);
|
||||
|
||||
/**
|
||||
* Method as act client and request to server for details.
|
||||
*
|
||||
* @param args program argument.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
List<CustomerDto> customers = new ArrayList<>();
|
||||
CustomerDto customerOne = new CustomerDto("1", "Kelly", "Brown");
|
||||
CustomerDto customerTwo = new CustomerDto("2", "Alfonso", "Bass");
|
||||
customers.add(customerOne);
|
||||
customers.add(customerTwo);
|
||||
|
||||
CustomerResource customerResource = new CustomerResource(customers);
|
||||
|
||||
LOGGER.info("All customers:-");
|
||||
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
LOGGER.info("----------------------------------------------------------");
|
||||
|
||||
LOGGER.info("Deleting customer with id {1}");
|
||||
customerResource.delete(customerOne.getId());
|
||||
allCustomers = customerResource.getAllCustomers();
|
||||
printCustomerDetails(allCustomers);
|
||||
|
||||
LOGGER.info("----------------------------------------------------------");
|
||||
|
||||
LOGGER.info("Adding customer three}");
|
||||
CustomerDto customerThree = new CustomerDto("3", "Lynda", "Blair");
|
||||
customerResource.save(customerThree);
|
||||
allCustomers = customerResource.getAllCustomers();
|
||||
printCustomerDetails(allCustomers);
|
||||
}
|
||||
|
||||
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
|
||||
allCustomers.forEach(customer -> LOGGER.info(customer.getFirstName()));
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Gopinath Langote
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
|
||||
/**
|
||||
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to client
|
||||
* We can send related information together in POJO.
|
||||
* <p>
|
||||
* Dto will not have any business logic in it.
|
||||
*/
|
||||
public class CustomerDto {
|
||||
private final String id;
|
||||
private final String firstName;
|
||||
private final String lastName;
|
||||
|
||||
/**
|
||||
* @param id customer id
|
||||
* @param firstName customer first name
|
||||
* @param lastName customer last name
|
||||
*/
|
||||
public CustomerDto(String id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Gopinath Langote
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The resource class which serves customer information.
|
||||
* This class act as server in the demo. Which has all customer details.
|
||||
*/
|
||||
public class CustomerResource {
|
||||
private List<CustomerDto> customers;
|
||||
|
||||
/**
|
||||
* @param customers initialize resource with existing customers. Act as database.
|
||||
*/
|
||||
public CustomerResource(List<CustomerDto> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return : all customers in list.
|
||||
*/
|
||||
public List<CustomerDto> getAllCustomers() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param customer save new customer to list.
|
||||
*/
|
||||
public void save(CustomerDto customer) {
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param customerId delete customer with id {@code customerId}
|
||||
*/
|
||||
public void delete(String customerId) {
|
||||
customers.removeIf(customer -> customer.getId().equals(customerId));
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Gopinath Langote
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.datatransfer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* tests {@link CustomerResource}.
|
||||
*/
|
||||
public class CustomerResourceTest {
|
||||
@Test
|
||||
public void shouldGetAllCustomers() {
|
||||
CustomerDto customer = new CustomerDto("1", "Melody", "Yates");
|
||||
List<CustomerDto> customers = new ArrayList<>();
|
||||
customers.add(customer);
|
||||
|
||||
CustomerResource customerResource = new CustomerResource(customers);
|
||||
|
||||
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
|
||||
|
||||
assertEquals(allCustomers.size(), 1);
|
||||
assertEquals(allCustomers.get(0).getId(), "1");
|
||||
assertEquals(allCustomers.get(0).getFirstName(), "Melody");
|
||||
assertEquals(allCustomers.get(0).getLastName(), "Yates");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveCustomer() {
|
||||
CustomerDto customer = new CustomerDto("1", "Rita", "Reynolds");
|
||||
CustomerResource customerResource = new CustomerResource(new ArrayList<>());
|
||||
|
||||
customerResource.save(customer);
|
||||
|
||||
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
|
||||
assertEquals(allCustomers.get(0).getId(), "1");
|
||||
assertEquals(allCustomers.get(0).getFirstName(), "Rita");
|
||||
assertEquals(allCustomers.get(0).getLastName(), "Reynolds");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteCustomer() {
|
||||
CustomerDto customer = new CustomerDto("1", "Terry", "Nguyen");
|
||||
List<CustomerDto> customers = new ArrayList<>();
|
||||
customers.add(customer);
|
||||
|
||||
CustomerResource customerResource = new CustomerResource(customers);
|
||||
|
||||
customerResource.delete(customer.getId());
|
||||
|
||||
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
|
||||
assertEquals(allCustomers.size(), 0);
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,49 @@ Define an interface for creating an object, but let subclasses
|
||||
decide which class to instantiate. Factory Method lets a class defer
|
||||
instantiation to subclasses.
|
||||
|
||||

|
||||
## Explanation
|
||||
Real world example
|
||||
|
||||
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.
|
||||
|
||||
In plain words
|
||||
|
||||
> It provides a way to delegate the instantiation logic to child classes.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
|
||||
|
||||
```
|
||||
public interface Blacksmith {
|
||||
Weapon manufactureWeapon(WeaponType weaponType);
|
||||
}
|
||||
|
||||
public class ElfBlacksmith implements Blacksmith {
|
||||
public Weapon manufactureWeapon(WeaponType weaponType) {
|
||||
return new ElfWeapon(weaponType);
|
||||
}
|
||||
}
|
||||
|
||||
public class OrcBlacksmith implements Blacksmith {
|
||||
public Weapon manufactureWeapon(WeaponType weaponType) {
|
||||
return new OrcWeapon(weaponType);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
|
||||
|
||||
```
|
||||
Blacksmith blacksmith = new ElfBlacksmith();
|
||||
blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
// Elvish weapons are created
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Factory Method pattern when
|
||||
|
Before Width: | Height: | Size: 22 KiB |
@ -1,117 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.factory.method.OrcBlacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="280" x="-46" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="2" language="java" name="com.iluwatar.factory.method.Blacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/Blacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="88" width="280" x="114" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<enumeration id="3" language="java" name="com.iluwatar.factory.method.WeaponType" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/WeaponType.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="210" x="793" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="4" language="java" name="com.iluwatar.factory.method.OrcWeapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="178" x="806" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.factory.method.ElfWeapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="172" x="594" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="6" language="java" name="com.iluwatar.factory.method.Weapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/Weapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="70" width="159" x="594" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="7" language="java" name="com.iluwatar.factory.method.ElfBlacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="280" x="274" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="8">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<realization id="9">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<association id="11">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="12" name="weaponType">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="13" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="14">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<association id="15">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="16" name="weaponType">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="17" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,54 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.factory.method {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
- blacksmith : Blacksmith
|
||||
+ App(blacksmith : Blacksmith)
|
||||
+ main(args : String[]) {static}
|
||||
- manufactureWeapons()
|
||||
}
|
||||
interface Blacksmith {
|
||||
+ manufactureWeapon(WeaponType) : Weapon {abstract}
|
||||
}
|
||||
class ElfBlacksmith {
|
||||
+ ElfBlacksmith()
|
||||
+ manufactureWeapon(weaponType : WeaponType) : Weapon
|
||||
}
|
||||
class ElfWeapon {
|
||||
- weaponType : WeaponType
|
||||
+ ElfWeapon(weaponType : WeaponType)
|
||||
+ getWeaponType() : WeaponType
|
||||
+ toString() : String
|
||||
}
|
||||
class OrcBlacksmith {
|
||||
+ OrcBlacksmith()
|
||||
+ manufactureWeapon(weaponType : WeaponType) : Weapon
|
||||
}
|
||||
class OrcWeapon {
|
||||
- weaponType : WeaponType
|
||||
+ OrcWeapon(weaponType : WeaponType)
|
||||
+ getWeaponType() : WeaponType
|
||||
+ toString() : String
|
||||
}
|
||||
interface Weapon {
|
||||
+ getWeaponType() : WeaponType {abstract}
|
||||
}
|
||||
enum WeaponType {
|
||||
+ AXE {static}
|
||||
+ SHORT_SWORD {static}
|
||||
+ SPEAR {static}
|
||||
+ UNDEFINED {static}
|
||||
- title : String
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : WeaponType {static}
|
||||
+ values() : WeaponType[] {static}
|
||||
}
|
||||
}
|
||||
ElfWeapon --> "-weaponType" WeaponType
|
||||
OrcWeapon --> "-weaponType" WeaponType
|
||||
App --> "-blacksmith" Blacksmith
|
||||
ElfBlacksmith ..|> Blacksmith
|
||||
ElfWeapon ..|> Weapon
|
||||
OrcBlacksmith ..|> Blacksmith
|
||||
OrcWeapon ..|> Weapon
|
||||
@enduml
|
Before Width: | Height: | Size: 46 KiB |
@ -25,6 +25,10 @@ package com.iluwatar.model.view.presenter;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Every instance of this class represents the Model component in the Model-View-Presenter
|
||||
@ -32,7 +36,14 @@ import java.io.FileReader;
|
||||
* <p>
|
||||
* It is responsible for reading and loading the contents of a given file.
|
||||
*/
|
||||
public class FileLoader {
|
||||
public class FileLoader implements Serializable{
|
||||
|
||||
/**
|
||||
* Generated serial version UID
|
||||
*/
|
||||
private static final long serialVersionUID = -4745803872902019069L;
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FileLoader.class);
|
||||
|
||||
/**
|
||||
* Indicates if the file is loaded or not.
|
||||
@ -48,7 +59,8 @@ public class FileLoader {
|
||||
* Loads the data of the file specified.
|
||||
*/
|
||||
public String loadData() {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(new File(this.fileName)))) {
|
||||
String dataFileName = this.fileName;
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(new File(dataFileName)))) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
|
||||
@ -60,7 +72,7 @@ public class FileLoader {
|
||||
|
||||
return sb.toString();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("File {} does not exist", dataFileName);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -22,13 +22,20 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.presenter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Every instance of this class represents the Presenter component in the Model-View-Presenter
|
||||
* architectural pattern.
|
||||
* <p>
|
||||
* It is responsible for reacting to the user's actions and update the View component.
|
||||
*/
|
||||
public class FileSelectorPresenter {
|
||||
public class FileSelectorPresenter implements Serializable{
|
||||
|
||||
/**
|
||||
* Generated serial version UID
|
||||
*/
|
||||
private static final long serialVersionUID = 1210314339075855074L;
|
||||
|
||||
/**
|
||||
* The View component that the presenter interacts with.
|
||||
|
@ -22,11 +22,13 @@
|
||||
*/
|
||||
package com.iluwatar.model.view.presenter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* This interface represents the View component in the Model-View-Presenter pattern. It can be
|
||||
* implemented by either the GUI components, or by the Stub.
|
||||
*/
|
||||
public interface FileSelectorView {
|
||||
public interface FileSelectorView extends Serializable{
|
||||
|
||||
/**
|
||||
* Opens the view.
|
||||
|
8
pom.xml
@ -144,7 +144,7 @@
|
||||
<module>marker</module>
|
||||
<module>cqrs</module>
|
||||
<module>event-sourcing</module>
|
||||
|
||||
<module>data-transfer-object</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -461,6 +461,12 @@
|
||||
<skipForProjects>
|
||||
<!-- skip for parent project -->
|
||||
<param>java-design-patterns</param>
|
||||
<param>singleton</param>
|
||||
<param>factory-method</param>
|
||||
<param>abstract-factory</param>
|
||||
<param>builder</param>
|
||||
<param>prototype</param>
|
||||
<param>adapter</param>
|
||||
</skipForProjects>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
@ -15,14 +15,57 @@ tags:
|
||||
Specify the kinds of objects to create using a prototypical
|
||||
instance, and create new objects by copying this prototype.
|
||||
|
||||

|
||||
## Explanation
|
||||
Real world example
|
||||
|
||||
> Remember Dolly? The sheep that was cloned! Lets not get into the details but the key point here is that it is all about cloning.
|
||||
|
||||
In plain words
|
||||
|
||||
> Create object based on an existing object through cloning.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> The prototype pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
|
||||
|
||||
In short, it allows you to create a copy of an existing object and modify it to your needs, instead of going through the trouble of creating an object from scratch and setting it up.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
In Java, it can be easily done by implementing `Cloneable` and overriding `clone` from `Object`
|
||||
|
||||
```
|
||||
class Sheep implements Cloneable {
|
||||
privage String name;
|
||||
public Sheep(String name) { this.name = name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getName() { return name; }
|
||||
@Override
|
||||
public Sheep clone() throws CloneNotSupportedException {
|
||||
return new Sheep(name);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then it can be cloned like below
|
||||
|
||||
```
|
||||
Sheep original = new Sheep("Jolly");
|
||||
System.out.println(original.getName()); // Jolly
|
||||
|
||||
// Clone and modify what is required
|
||||
Sheep cloned = original.clone();
|
||||
cloned.setName("Dolly");
|
||||
System.out.println(cloned.getName()); // Dolly
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Prototype pattern when a system should be independent of how its products are created, composed and represented; and
|
||||
|
||||
* when the classes to instantiate are specified at run-time, for example, by dynamic loading; or
|
||||
* to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or
|
||||
* when the classes to instantiate are specified at run-time, for example, by dynamic loading
|
||||
* to avoid building a class hierarchy of factories that parallels the class hierarchy of products
|
||||
* when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state
|
||||
* when object creation is expensive compared to cloning
|
||||
|
||||
## Real world examples
|
||||
|
||||
|
Before Width: | Height: | Size: 37 KiB |
@ -1,182 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.prototype.ElfMage" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/ElfMage.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="132" x="788" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.prototype.OrcMage" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/OrcMage.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="140" x="960" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.prototype.Mage" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/Mage.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="132" x="665" y="28"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.prototype.Prototype" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/Prototype.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="132" x="493" y="-154"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.prototype.Beast" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/Beast.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="132" x="321" y="28"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="6" language="java" name="com.iluwatar.prototype.ElfWarlord" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="154" x="388" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="7" language="java" name="com.iluwatar.prototype.Warlord" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/Warlord.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="105" width="132" x="493" y="28"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="8" language="java" name="com.iluwatar.prototype.ElfBeast" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="132" x="32" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="9" language="java" name="com.iluwatar.prototype.OrcWarlord" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="166" x="582" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="10" language="java" name="com.iluwatar.prototype.HeroFactoryImpl" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="248" x="665" y="-154"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="11" language="java" name="com.iluwatar.prototype.OrcBeast" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="144" x="204" y="192"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="12" language="java" name="com.iluwatar.prototype.HeroFactory" project="prototype"
|
||||
file="/prototype/src/main/java/com/iluwatar/prototype/HeroFactory.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="164" x="837" y="28"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<generalization id="13">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<generalization id="14">
|
||||
<end type="SOURCE" refId="6"/>
|
||||
<end type="TARGET" refId="7"/>
|
||||
</generalization>
|
||||
<generalization id="15">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<association id="16">
|
||||
<end type="SOURCE" refId="10" navigable="false">
|
||||
<attribute id="17" name="beast"/>
|
||||
<multiplicity id="18" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="19">
|
||||
<end type="SOURCE" refId="10"/>
|
||||
<end type="TARGET" refId="12"/>
|
||||
</realization>
|
||||
<generalization id="20">
|
||||
<end type="SOURCE" refId="9"/>
|
||||
<end type="TARGET" refId="7"/>
|
||||
</generalization>
|
||||
<generalization id="21">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<generalization id="22">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<association id="23">
|
||||
<end type="SOURCE" refId="10" navigable="false">
|
||||
<attribute id="24" name="mage"/>
|
||||
<multiplicity id="25" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="26">
|
||||
<end type="SOURCE" refId="10" navigable="false">
|
||||
<attribute id="27" name="warlord"/>
|
||||
<multiplicity id="28" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="7" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<generalization id="29">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="3"/>
|
||||
</generalization>
|
||||
<generalization id="30">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="31">
|
||||
<end type="SOURCE" refId="11"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,82 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.prototype {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
abstract class Beast {
|
||||
+ Beast()
|
||||
+ clone() : Beast {abstract}
|
||||
}
|
||||
class ElfBeast {
|
||||
+ ElfBeast()
|
||||
+ clone() : Beast
|
||||
+ toString() : String
|
||||
}
|
||||
class ElfMage {
|
||||
+ ElfMage()
|
||||
+ clone() : Mage
|
||||
+ toString() : String
|
||||
}
|
||||
class ElfWarlord {
|
||||
+ ElfWarlord()
|
||||
+ clone() : Warlord
|
||||
+ toString() : String
|
||||
}
|
||||
interface HeroFactory {
|
||||
+ createBeast() : Beast {abstract}
|
||||
+ createMage() : Mage {abstract}
|
||||
+ createWarlord() : Warlord {abstract}
|
||||
}
|
||||
class HeroFactoryImpl {
|
||||
- beast : Beast
|
||||
- mage : Mage
|
||||
- warlord : Warlord
|
||||
+ HeroFactoryImpl(mage : Mage, warlord : Warlord, beast : Beast)
|
||||
+ createBeast() : Beast
|
||||
+ createMage() : Mage
|
||||
+ createWarlord() : Warlord
|
||||
}
|
||||
abstract class Mage {
|
||||
+ Mage()
|
||||
+ clone() : Mage {abstract}
|
||||
}
|
||||
class OrcBeast {
|
||||
+ OrcBeast()
|
||||
+ clone() : Beast
|
||||
+ toString() : String
|
||||
}
|
||||
class OrcMage {
|
||||
+ OrcMage()
|
||||
+ clone() : Mage
|
||||
+ toString() : String
|
||||
}
|
||||
class OrcWarlord {
|
||||
+ OrcWarlord()
|
||||
+ clone() : Warlord
|
||||
+ toString() : String
|
||||
}
|
||||
abstract class Prototype {
|
||||
+ Prototype()
|
||||
+ clone() : Object {abstract}
|
||||
}
|
||||
abstract class Warlord {
|
||||
+ Warlord()
|
||||
+ clone() : Warlord {abstract}
|
||||
}
|
||||
}
|
||||
HeroFactoryImpl --> "-beast" Beast
|
||||
HeroFactoryImpl --> "-warlord" Warlord
|
||||
HeroFactoryImpl --> "-mage" Mage
|
||||
Beast --|> Prototype
|
||||
ElfBeast --|> Beast
|
||||
ElfMage --|> Mage
|
||||
ElfWarlord --|> Warlord
|
||||
HeroFactoryImpl ..|> HeroFactory
|
||||
Mage --|> Prototype
|
||||
OrcBeast --|> Beast
|
||||
OrcMage --|> Mage
|
||||
OrcWarlord --|> Warlord
|
||||
Warlord --|> Prototype
|
||||
@enduml
|
Before Width: | Height: | Size: 95 KiB |
@ -65,6 +65,7 @@ public class AppClient {
|
||||
* @throws IOException if any I/O error occurs.
|
||||
*/
|
||||
public void start() throws IOException {
|
||||
LOGGER.info("Starting logging clients");
|
||||
service.execute(new TcpLoggingClient("Client 1", 6666));
|
||||
service.execute(new TcpLoggingClient("Client 2", 6667));
|
||||
service.execute(new UdpLoggingClient("Client 3", 6668));
|
||||
@ -81,16 +82,17 @@ public class AppClient {
|
||||
try {
|
||||
service.awaitTermination(1000, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("exception awaiting termination", e);
|
||||
}
|
||||
}
|
||||
LOGGER.info("Logging clients stopped");
|
||||
}
|
||||
|
||||
private static void artificialDelayOf(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("sleep interrupted", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,7 +121,7 @@ public class AppClient {
|
||||
PrintWriter writer = new PrintWriter(outputStream);
|
||||
sendLogRequests(writer, socket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("error sending requests", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@ -185,7 +187,7 @@ public class AppClient {
|
||||
artificialDelayOf(100);
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
LOGGER.error("error sending packets", e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ public class NioReactor {
|
||||
LOGGER.info("Reactor started, waiting for events...");
|
||||
eventLoop();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("exception in event loop", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -112,6 +112,7 @@ public class NioReactor {
|
||||
selector.wakeup();
|
||||
reactorMain.awaitTermination(4, TimeUnit.SECONDS);
|
||||
selector.close();
|
||||
LOGGER.info("Reactor stopped");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,7 +207,7 @@ public class NioReactor {
|
||||
try {
|
||||
key.channel().close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
LOGGER.error("error closing channel", e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,10 +24,13 @@ package com.iluwatar.reactor.app;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.iluwatar.reactor.framework.NioReactor;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.reactor.framework.SameThreadDispatcher;
|
||||
import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -36,6 +39,8 @@ import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
|
||||
*/
|
||||
public class ReactorTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReactorTest.class);
|
||||
|
||||
/**
|
||||
* Test the application using pooled thread dispatcher.
|
||||
*
|
||||
@ -44,6 +49,7 @@ public class ReactorTest {
|
||||
*/
|
||||
@Test
|
||||
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
|
||||
LOGGER.info("testAppUsingThreadPoolDispatcher start");
|
||||
App app = new App(new ThreadPoolDispatcher(2));
|
||||
app.start();
|
||||
|
||||
@ -54,12 +60,13 @@ public class ReactorTest {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("sleep interrupted", e);
|
||||
}
|
||||
|
||||
client.stop();
|
||||
|
||||
app.stop();
|
||||
LOGGER.info("testAppUsingThreadPoolDispatcher stop");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,6 +77,7 @@ public class ReactorTest {
|
||||
*/
|
||||
@Test
|
||||
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
|
||||
LOGGER.info("testAppUsingSameThreadDispatcher start");
|
||||
App app = new App(new SameThreadDispatcher());
|
||||
app.start();
|
||||
|
||||
@ -80,11 +88,12 @@ public class ReactorTest {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("sleep interrupted", e);
|
||||
}
|
||||
|
||||
client.stop();
|
||||
|
||||
app.stop();
|
||||
LOGGER.info("testAppUsingSameThreadDispatcher stop");
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,39 @@ tags:
|
||||
Ensure a class only has one instance, and provide a global point of
|
||||
access to it.
|
||||
|
||||

|
||||
|
||||
## Explanation
|
||||
Real world example
|
||||
|
||||
> There can only be one ivory tower where the wizards study their magic. The same enchanted ivory tower is always used by the wizards. Ivory tower here is singleton.
|
||||
|
||||
In plain words
|
||||
|
||||
> Ensures that only one object of a particular class is ever created.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Joshua Bloch, Effective Java 2nd Edition p.18
|
||||
|
||||
> A single-element enum type is the best way to implement a singleton
|
||||
|
||||
```
|
||||
public enum EnumIvoryTower {
|
||||
INSTANCE;
|
||||
}
|
||||
```
|
||||
|
||||
Then in order to use
|
||||
|
||||
```
|
||||
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
|
||||
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
|
||||
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Singleton pattern when
|
||||
@ -40,7 +72,7 @@ Use the Singleton pattern when
|
||||
|
||||
* Violates Single Responsibility Principle (SRP) by controlling their own creation and lifecycle.
|
||||
* Encourages using a global shared instance which prevents an object and resources used by this object from being deallocated.
|
||||
* Creates tightly coupled code that is difficult to test.
|
||||
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
|
||||
* Makes it almost impossible to subclass a Singleton.
|
||||
|
||||
## Credits
|
||||
|
Before Width: | Height: | Size: 22 KiB |
@ -1,104 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.singleton.InitializingOnDemandHolderIdiom.HelperHolder"
|
||||
project="singleton" file="/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java"
|
||||
binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="88" width="133" x="328" y="457"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="2" language="java" name="com.iluwatar.singleton.IvoryTower" project="singleton"
|
||||
file="/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="164" x="114" y="275"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.singleton.InitializingOnDemandHolderIdiom" project="singleton"
|
||||
file="/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="142" width="295" x="328" y="275"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<enumeration id="4" language="java" name="com.iluwatar.singleton.EnumIvoryTower" project="singleton"
|
||||
file="/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="124" width="190" x="663" y="275"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="5" language="java" name="com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower" project="singleton"
|
||||
file="/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="299" x="893" y="275"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="6" language="java" name="com.iluwatar.singleton.ThreadSafeDoubleCheckLocking" project="singleton"
|
||||
file="/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="292" x="1242" y="275"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="2" navigable="false">
|
||||
<attribute id="8" name="instance"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="2" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="10">
|
||||
<end type="SOURCE" refId="6" navigable="false">
|
||||
<attribute id="11" name="INSTANCE"/>
|
||||
<multiplicity id="12" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="6" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<association id="13">
|
||||
<end type="SOURCE" refId="1" navigable="false">
|
||||
<attribute id="14" name="INSTANCE"/>
|
||||
<multiplicity id="15" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<nesting id="16">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</nesting>
|
||||
<association id="17">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="18" name="instance"/>
|
||||
<multiplicity id="19" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="5" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -1,43 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.singleton {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
enum EnumIvoryTower {
|
||||
+ INSTANCE {static}
|
||||
+ toString() : String
|
||||
+ valueOf(name : String) : EnumIvoryTower {static}
|
||||
+ values() : EnumIvoryTower[] {static}
|
||||
}
|
||||
class InitializingOnDemandHolderIdiom {
|
||||
- InitializingOnDemandHolderIdiom()
|
||||
+ getInstance() : InitializingOnDemandHolderIdiom {static}
|
||||
}
|
||||
-class HelperHolder {
|
||||
- INSTANCE : InitializingOnDemandHolderIdiom {static}
|
||||
- HelperHolder()
|
||||
}
|
||||
class IvoryTower {
|
||||
- INSTANCE : IvoryTower {static}
|
||||
- IvoryTower()
|
||||
+ getInstance() : IvoryTower {static}
|
||||
}
|
||||
class ThreadSafeDoubleCheckLocking {
|
||||
- instance : ThreadSafeDoubleCheckLocking {static}
|
||||
- ThreadSafeDoubleCheckLocking()
|
||||
+ getInstance() : ThreadSafeDoubleCheckLocking {static}
|
||||
}
|
||||
class ThreadSafeLazyLoadedIvoryTower {
|
||||
- instance : ThreadSafeLazyLoadedIvoryTower {static}
|
||||
- ThreadSafeLazyLoadedIvoryTower()
|
||||
+ getInstance() : ThreadSafeLazyLoadedIvoryTower {static}
|
||||
}
|
||||
}
|
||||
IvoryTower --> "-INSTANCE" IvoryTower
|
||||
ThreadSafeDoubleCheckLocking --> "-instance" ThreadSafeDoubleCheckLocking
|
||||
ThreadSafeLazyLoadedIvoryTower --> "-instance" ThreadSafeLazyLoadedIvoryTower
|
||||
HelperHolder ..+ InitializingOnDemandHolderIdiom
|
||||
HelperHolder --> "-INSTANCE" InitializingOnDemandHolderIdiom
|
||||
@enduml
|
Before Width: | Height: | Size: 16 KiB |
@ -33,7 +33,12 @@ public final class ThreadSafeLazyLoadedIvoryTower {
|
||||
|
||||
private static ThreadSafeLazyLoadedIvoryTower instance;
|
||||
|
||||
private ThreadSafeLazyLoadedIvoryTower() {}
|
||||
private ThreadSafeLazyLoadedIvoryTower() {
|
||||
// to prevent instantiating by Reflection call
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance gets created only when it is called for first time. Lazy-loading
|
||||
|