Reformat rest of the design patterns - Issue #224
This commit is contained in:
@ -2,44 +2,43 @@ package com.iluwatar.prototype;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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. This pattern is used to:
|
||||
* - avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
|
||||
* - avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword)
|
||||
* 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. This pattern is used to: - avoid subclasses of an object creator in the client
|
||||
* application, like the abstract factory pattern does. - avoid the inherent cost of creating a new
|
||||
* object in the standard way (e.g., using the 'new' keyword)
|
||||
* <p>
|
||||
* In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by
|
||||
* cloning the existing ones. The factory's prototype objects are given as constructor parameters.
|
||||
* In this example we have a factory class ({@link HeroFactoryImpl}) producing objects by cloning
|
||||
* the existing ones. The factory's prototype objects are given as constructor parameters.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
HeroFactory factory;
|
||||
Mage mage;
|
||||
Warlord warlord;
|
||||
Beast beast;
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
HeroFactory factory;
|
||||
Mage mage;
|
||||
Warlord warlord;
|
||||
Beast beast;
|
||||
|
||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(),
|
||||
new ElfBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
|
||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(),
|
||||
new OrcBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
}
|
||||
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
|
||||
mage = factory.createMage();
|
||||
warlord = factory.createWarlord();
|
||||
beast = factory.createBeast();
|
||||
System.out.println(mage);
|
||||
System.out.println(warlord);
|
||||
System.out.println(beast);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public abstract class Beast extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Beast clone() throws CloneNotSupportedException;
|
||||
@Override
|
||||
public abstract Beast clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class ElfBeast extends Beast {
|
||||
|
||||
public ElfBeast() {
|
||||
}
|
||||
public ElfBeast() {}
|
||||
|
||||
public ElfBeast(ElfBeast beast) {
|
||||
}
|
||||
public ElfBeast(ElfBeast beast) {}
|
||||
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new ElfBeast(this);
|
||||
}
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new ElfBeast(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven eagle";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven eagle";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class ElfMage extends Mage {
|
||||
|
||||
public ElfMage() {
|
||||
}
|
||||
public ElfMage() {}
|
||||
|
||||
public ElfMage(ElfMage mage) {
|
||||
}
|
||||
public ElfMage(ElfMage mage) {}
|
||||
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new ElfMage(this);
|
||||
}
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new ElfMage(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven mage";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven mage";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class ElfWarlord extends Warlord {
|
||||
|
||||
public ElfWarlord() {
|
||||
}
|
||||
public ElfWarlord() {}
|
||||
|
||||
public ElfWarlord(ElfWarlord warlord) {
|
||||
}
|
||||
public ElfWarlord(ElfWarlord warlord) {}
|
||||
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new ElfWarlord(this);
|
||||
}
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new ElfWarlord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven warlord";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Elven warlord";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public interface HeroFactory {
|
||||
|
||||
Mage createMage();
|
||||
Mage createMage();
|
||||
|
||||
Warlord createWarlord();
|
||||
Warlord createWarlord();
|
||||
|
||||
Beast createBeast();
|
||||
Beast createBeast();
|
||||
|
||||
}
|
||||
|
@ -7,38 +7,38 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class HeroFactoryImpl implements HeroFactory {
|
||||
|
||||
private Mage mage;
|
||||
private Warlord warlord;
|
||||
private Beast beast;
|
||||
private Mage mage;
|
||||
private Warlord warlord;
|
||||
private Beast beast;
|
||||
|
||||
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
|
||||
this.mage = mage;
|
||||
this.warlord = warlord;
|
||||
this.beast = beast;
|
||||
}
|
||||
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
|
||||
this.mage = mage;
|
||||
this.warlord = warlord;
|
||||
this.beast = beast;
|
||||
}
|
||||
|
||||
public Mage createMage() {
|
||||
try {
|
||||
return mage.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Mage createMage() {
|
||||
try {
|
||||
return mage.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Warlord createWarlord() {
|
||||
try {
|
||||
return warlord.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Warlord createWarlord() {
|
||||
try {
|
||||
return warlord.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Beast createBeast() {
|
||||
try {
|
||||
return beast.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public Beast createBeast() {
|
||||
try {
|
||||
return beast.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public abstract class Mage extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Mage clone() throws CloneNotSupportedException;
|
||||
@Override
|
||||
public abstract Mage clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class OrcBeast extends Beast {
|
||||
|
||||
public OrcBeast() {
|
||||
}
|
||||
public OrcBeast() {}
|
||||
|
||||
public OrcBeast(OrcBeast beast) {
|
||||
}
|
||||
public OrcBeast(OrcBeast beast) {}
|
||||
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new OrcBeast(this);
|
||||
}
|
||||
@Override
|
||||
public Beast clone() throws CloneNotSupportedException {
|
||||
return new OrcBeast(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish wolf";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish wolf";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class OrcMage extends Mage {
|
||||
|
||||
public OrcMage() {
|
||||
}
|
||||
public OrcMage() {}
|
||||
|
||||
public OrcMage(OrcMage mage) {
|
||||
}
|
||||
public OrcMage(OrcMage mage) {}
|
||||
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new OrcMage(this);
|
||||
}
|
||||
@Override
|
||||
public Mage clone() throws CloneNotSupportedException {
|
||||
return new OrcMage(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish mage";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish mage";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,20 +7,18 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class OrcWarlord extends Warlord {
|
||||
|
||||
public OrcWarlord() {
|
||||
}
|
||||
public OrcWarlord() {}
|
||||
|
||||
public OrcWarlord(OrcWarlord warlord) {
|
||||
}
|
||||
public OrcWarlord(OrcWarlord warlord) {}
|
||||
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new OrcWarlord(this);
|
||||
}
|
||||
@Override
|
||||
public Warlord clone() throws CloneNotSupportedException {
|
||||
return new OrcWarlord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish warlord";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Orcish warlord";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public abstract class Prototype implements Cloneable {
|
||||
|
||||
@Override
|
||||
public abstract Object clone() throws CloneNotSupportedException;
|
||||
@Override
|
||||
public abstract Object clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public abstract class Warlord extends Prototype {
|
||||
|
||||
@Override
|
||||
public abstract Warlord clone() throws CloneNotSupportedException;
|
||||
@Override
|
||||
public abstract Warlord clone() throws CloneNotSupportedException;
|
||||
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.prototype.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user