Skip to content

Commit 604425a

Browse files
committed
[ts] refactor abstract factory
1 parent e6c114e commit 604425a

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

Diff for: abstract-factory-pattern/abstract-factory-pattern.ts

+36-42
Original file line numberDiff line numberDiff line change
@@ -11,81 +11,80 @@
1111
* @see http://www.newthinktank.com/2012/09/abstract-factory-design-pattern/
1212
*/
1313

14-
class ESWeapon { }
15-
class ESEngine { }
14+
class ESWeapon {}
15+
class ESEngine {}
1616

17-
class ESUFOGun extends ESWeapon { }
18-
class ESUFOEngine extends ESEngine { }
17+
class ESUFOGun extends ESWeapon {}
18+
class ESUFOEngine extends ESEngine {}
1919

20-
class ESUFOBossGun extends ESWeapon { }
21-
class ESUFOBossEngine extends ESEngine { }
20+
class ESUFOBossGun extends ESWeapon {}
21+
class ESUFOBossEngine extends ESEngine {}
2222

2323
abstract class EnemyShip {
24-
private name: string;
24+
#name: string;
2525

2626
// Newly defined objects that represent weapon & engine
27-
// These can be changed easily by assigning new parts
28-
// in UFOEnemyShipFactory or UFOBossEnemyShipFactory
27+
// These can be changed easily by assigning new parts in UFOEnemyShipFactory
28+
// or UFOBossEnemyShipFactory
2929
weapon: ESWeapon;
3030
engine: ESEngine;
3131

32-
public getName(): string {
33-
return this.name;
32+
get name(): string {
33+
return this.#name;
3434
}
35-
public setName(name: string) {
36-
this.name = name;
35+
36+
set name(newName: string) {
37+
this.#name = newName;
3738
}
3839

3940
abstract makeShip(): void;
4041

4142
public followHeroShip(): void {
42-
console.log(this.getName(), "is following the hero at", this.engine);
43+
console.log(this.name, "is following the hero at", this.engine);
4344
}
4445

4546
public displayEnemyShip(): void {
46-
console.log(this.getName(), "is on the screen");
47+
console.log(this.name, "is on the screen");
4748
}
4849

4950
public enemyShipShoots(): void {
50-
console.log(this.getName, "attacks and does", this.weapon);
51+
console.log(this.name, "attacks and does", this.weapon);
5152
}
5253
}
5354

5455
class UFOEnemyShip extends EnemyShip {
55-
shipFactory: EnemyShipFactory;
56+
private readonly shipFactory: EnemyShipFactory;
5657

5758
constructor(shipFactory: EnemyShipFactory) {
5859
super();
5960
this.shipFactory = shipFactory;
6061
}
6162

6263
makeShip(): void {
63-
console.log("making the ship", this.getName());
64+
console.log("making the ship", this.name);
6465
this.weapon = this.shipFactory.addESGun();
6566
this.engine = this.shipFactory.addESEngine();
6667
}
6768
}
6869

6970
class UFOBossEnemyShip extends EnemyShip {
70-
shipFactory: EnemyShipFactory;
71+
private readonly shipFactory: EnemyShipFactory;
7172

7273
constructor(shipFactory: EnemyShipFactory) {
7374
super();
7475
this.shipFactory = shipFactory;
7576
}
7677

7778
makeShip(): void {
78-
console.log("making the ship", this.getName());
79+
console.log("making the ship", this.name);
7980
this.weapon = this.shipFactory.addESGun();
8081
this.engine = this.shipFactory.addESEngine();
8182
}
8283
}
8384

84-
// With an Abstract Factory Pattern you won't
85-
// just build ships, but also all of the components
86-
// for the ships
87-
// Here is where you define the parts that are required
88-
// if an object wants to be an enemy ship
85+
// With an Abstract Factory Pattern you won't just build ships, but also all of
86+
// the components for the ships Here is where you define the parts that are
87+
// required if an object wants to be an enemy ship
8988
interface EnemyShipFactory {
9089
addESGun(): ESWeapon;
9190
addESEngine(): ESEngine;
@@ -112,18 +111,16 @@ class UFOBossEnemyShipFactory implements EnemyShipFactory {
112111
}
113112

114113
abstract class EnemyShipBuilding {
115-
// This acts as an ordering mechanism for creating
116-
// EnemyShips that have a weapon, engine & name
117-
// & nothing else
114+
// This acts as an ordering mechanism for creating EnemyShips that have a weapon, engine & name &
115+
// nothing else
118116

119-
// The specific parts used for engine & weapon depend
120-
// upon the String that is passed to this method
117+
// The specific parts used for engine & weapon depend upon the String that is passed to this
118+
// method
121119

122120
protected abstract makeEnemyShip(typeOfShip: string): EnemyShip;
123121

124-
// When called a new EnemyShip is made. The specific parts
125-
// are based on the String entered. After the ship is made
126-
// we execute multiple methods in the EnemyShip Object
122+
// When called a new EnemyShip is made. The specific parts are based on the String entered. After
123+
// the ship is made we execute multiple methods in the EnemyShip Object
127124
public orderTheShip(typeOfShip: string): EnemyShip {
128125
const theEnemyShip: EnemyShip = this.makeEnemyShip(typeOfShip);
129126

@@ -140,21 +137,18 @@ class UFOEnemyShipBuilding extends EnemyShipBuilding {
140137
protected makeEnemyShip(typeOfShip: string): EnemyShip {
141138
let theEnemyShip: EnemyShip = null;
142139

143-
// If UFO was sent grab use the factory that knows
144-
// what types of weapons and engines a regular UFO
145-
// needs. The EnemyShip object is returned & given a name
140+
// If UFO was sent grab use the factory that knows what types of weapons and engines a regular
141+
// UFO needs. The EnemyShip object is returned & given a name
146142
if (typeOfShip === "UFO") {
147143
const shipPartsFactory: EnemyShipFactory = new UFOEnemyShipFactory();
148144
theEnemyShip = new UFOEnemyShip(shipPartsFactory);
149-
theEnemyShip.setName("UFO Grunt Ship");
145+
theEnemyShip.name = "UFO Grunt Ship";
150146
} else if (typeOfShip === "UFO BOSS") {
151-
// If UFO BOSS was sent grab use the factory that knows
152-
// what types of weapons and engines a Boss UFO
153-
// needs. The EnemyShip object is returned & given a name
154-
147+
// If UFO BOSS was sent grab use the factory that knows what types of weapons and engines a
148+
// Boss UFO needs. The EnemyShip object is returned & given a name
155149
const shipPartsFactory: EnemyShipFactory = new UFOBossEnemyShipFactory();
156150
theEnemyShip = new UFOEnemyShip(shipPartsFactory);
157-
theEnemyShip.setName("UFO Boss Ship");
151+
theEnemyShip.name = "UFO Boss Ship";
158152
}
159153

160154
return theEnemyShip;

0 commit comments

Comments
 (0)