11
11
* @see http://www.newthinktank.com/2012/09/abstract-factory-design-pattern/
12
12
*/
13
13
14
- class ESWeapon { }
15
- class ESEngine { }
14
+ class ESWeapon { }
15
+ class ESEngine { }
16
16
17
- class ESUFOGun extends ESWeapon { }
18
- class ESUFOEngine extends ESEngine { }
17
+ class ESUFOGun extends ESWeapon { }
18
+ class ESUFOEngine extends ESEngine { }
19
19
20
- class ESUFOBossGun extends ESWeapon { }
21
- class ESUFOBossEngine extends ESEngine { }
20
+ class ESUFOBossGun extends ESWeapon { }
21
+ class ESUFOBossEngine extends ESEngine { }
22
22
23
23
abstract class EnemyShip {
24
- private name : string ;
24
+ # name: string ;
25
25
26
26
// 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
29
29
weapon : ESWeapon ;
30
30
engine : ESEngine ;
31
31
32
- public getName ( ) : string {
33
- return this . name ;
32
+ get name ( ) : string {
33
+ return this . # name;
34
34
}
35
- public setName ( name : string ) {
36
- this . name = name ;
35
+
36
+ set name ( newName : string ) {
37
+ this . #name = newName ;
37
38
}
38
39
39
40
abstract makeShip ( ) : void ;
40
41
41
42
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 ) ;
43
44
}
44
45
45
46
public displayEnemyShip ( ) : void {
46
- console . log ( this . getName ( ) , "is on the screen" ) ;
47
+ console . log ( this . name , "is on the screen" ) ;
47
48
}
48
49
49
50
public enemyShipShoots ( ) : void {
50
- console . log ( this . getName , "attacks and does" , this . weapon ) ;
51
+ console . log ( this . name , "attacks and does" , this . weapon ) ;
51
52
}
52
53
}
53
54
54
55
class UFOEnemyShip extends EnemyShip {
55
- shipFactory : EnemyShipFactory ;
56
+ private readonly shipFactory : EnemyShipFactory ;
56
57
57
58
constructor ( shipFactory : EnemyShipFactory ) {
58
59
super ( ) ;
59
60
this . shipFactory = shipFactory ;
60
61
}
61
62
62
63
makeShip ( ) : void {
63
- console . log ( "making the ship" , this . getName ( ) ) ;
64
+ console . log ( "making the ship" , this . name ) ;
64
65
this . weapon = this . shipFactory . addESGun ( ) ;
65
66
this . engine = this . shipFactory . addESEngine ( ) ;
66
67
}
67
68
}
68
69
69
70
class UFOBossEnemyShip extends EnemyShip {
70
- shipFactory : EnemyShipFactory ;
71
+ private readonly shipFactory : EnemyShipFactory ;
71
72
72
73
constructor ( shipFactory : EnemyShipFactory ) {
73
74
super ( ) ;
74
75
this . shipFactory = shipFactory ;
75
76
}
76
77
77
78
makeShip ( ) : void {
78
- console . log ( "making the ship" , this . getName ( ) ) ;
79
+ console . log ( "making the ship" , this . name ) ;
79
80
this . weapon = this . shipFactory . addESGun ( ) ;
80
81
this . engine = this . shipFactory . addESEngine ( ) ;
81
82
}
82
83
}
83
84
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
89
88
interface EnemyShipFactory {
90
89
addESGun ( ) : ESWeapon ;
91
90
addESEngine ( ) : ESEngine ;
@@ -112,18 +111,16 @@ class UFOBossEnemyShipFactory implements EnemyShipFactory {
112
111
}
113
112
114
113
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
118
116
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
121
119
122
120
protected abstract makeEnemyShip ( typeOfShip : string ) : EnemyShip ;
123
121
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
127
124
public orderTheShip ( typeOfShip : string ) : EnemyShip {
128
125
const theEnemyShip : EnemyShip = this . makeEnemyShip ( typeOfShip ) ;
129
126
@@ -140,21 +137,18 @@ class UFOEnemyShipBuilding extends EnemyShipBuilding {
140
137
protected makeEnemyShip ( typeOfShip : string ) : EnemyShip {
141
138
let theEnemyShip : EnemyShip = null ;
142
139
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
146
142
if ( typeOfShip === "UFO" ) {
147
143
const shipPartsFactory : EnemyShipFactory = new UFOEnemyShipFactory ( ) ;
148
144
theEnemyShip = new UFOEnemyShip ( shipPartsFactory ) ;
149
- theEnemyShip . setName ( "UFO Grunt Ship" ) ;
145
+ theEnemyShip . name = "UFO Grunt Ship" ;
150
146
} 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
155
149
const shipPartsFactory : EnemyShipFactory = new UFOBossEnemyShipFactory ( ) ;
156
150
theEnemyShip = new UFOEnemyShip ( shipPartsFactory ) ;
157
- theEnemyShip . setName ( "UFO Boss Ship" ) ;
151
+ theEnemyShip . name = "UFO Boss Ship" ;
158
152
}
159
153
160
154
return theEnemyShip ;
0 commit comments