Skip to content

Commit 7ec19e7

Browse files
committed
Code Refactoring
1 parent 5cd253e commit 7ec19e7

File tree

15 files changed

+187
-151
lines changed

15 files changed

+187
-151
lines changed

Adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Adapter {
1717
}
1818

1919
// USAGE:
20-
const adapter = new InterfaceAdapter();
21-
adapter.request('param');
20+
const adapter = new InterfaceAdapter();
21+
adapter.request('param');
2222
// OUTPUT:
2323
// "Request made in old interface."

Bridge.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class Commander {
4343
}
4444

4545
// USAGE:
46-
const commanderA = new AirForceCommander(new Pilot());
47-
const commanderB = new SpecialForceCommander(new Soldier());
48-
49-
commanderA.order();
50-
commanderB.order();
46+
const commanderA = new AirForceCommander(new Pilot());
47+
const commanderB = new SpecialForceCommander(new Soldier());
48+
49+
commanderA.order();
50+
commanderB.order();
5151
// OUTPUT:
5252
// "Air Force commander make order"
5353
// "Fly"

Composite.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ interface ArmyObject {
5454
}
5555

5656
// USAGE:
57-
const team = new Team('Seal Team 6');
58-
const specialSquad = new Team('Seal Team 6 - Special Squad');
59-
60-
const soldierJoe = new Soldier('Joe');
61-
const soldierJames = new Soldier('James');
62-
const soldierRoy = new Soldier('Roy');
63-
team.addSoldier(soldierJoe);
64-
team.addSoldier(soldierJames);
65-
team.addSoldier(soldierRoy);
66-
67-
const specialForceTommy = new Soldier('Tommy');
68-
69-
specialSquad.addSoldier(specialForceTommy);
70-
71-
team.operate();
72-
specialSquad.operate();
73-
74-
team.soldierGone(soldierJames);
57+
const team = new Team('Seal Team 6');
58+
const specialSquad = new Team('Seal Team 6 - Special Squad');
59+
60+
const soldierJoe = new Soldier('Joe');
61+
const soldierJames = new Soldier('James');
62+
const soldierRoy = new Soldier('Roy');
63+
team.addSoldier(soldierJoe);
64+
team.addSoldier(soldierJames);
65+
team.addSoldier(soldierRoy);
66+
67+
const specialForceTommy = new Soldier('Tommy');
68+
69+
specialSquad.addSoldier(specialForceTommy);
70+
71+
team.operate();
72+
specialSquad.operate();
73+
74+
team.soldierGone(soldierJames);
7575
// OUTPUT:
7676
// "Soldier: Joe comes in Seal Team 6"
7777
// "Soldier: James comes in Seal Team 6"

Decorator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ interface Coffee {
3838
}
3939

4040
// USAGE:
41-
const general = new GeneralCoffee();
42-
const withBubble = new BubbleDecorator(general);
43-
const withMilk = new MilkDecorator(withBubble);
44-
console.log(`Total: ${withMilk.cost()}`);
41+
const general = new GeneralCoffee();
42+
const withBubble = new BubbleDecorator(general);
43+
const withMilk = new MilkDecorator(withBubble);
44+
console.log(`Total: ${withMilk.cost()}`);
4545
// OUTPUT:
4646
// "Total: 17"

Facade.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class EngineSystem {
3737
}
3838

3939
// USAGE:
40-
const operation = new RocketTestingOperation();
41-
operation.operationStart();
40+
const operation = new RocketTestingOperation();
41+
operation.operationStart();
4242
// OUTPUT:
4343
// "Check system situations"
4444
// "Oxygen will be generated"

FactoryMethod.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ interface Product {
3838
}
3939

4040
// USAGE:
41-
const factory = new ProductFactory();
42-
const product1 = factory.createProduct('product-a');
43-
const product2 = factory.createProduct('product-b');
44-
const product3 = factory.createProduct('product-c');
45-
product1.doSomething();
46-
product2.doSomething();
47-
product3.doSomething();
41+
const factory = new ProductFactory();
42+
const product1 = factory.createProduct('product-a');
43+
const product2 = factory.createProduct('product-b');
44+
const product3 = factory.createProduct('product-c');
45+
product1.doSomething();
46+
product2.doSomething();
47+
product3.doSomething();
4848
// OUTPUT:
4949
// "Product A do this"
5050
// "Product A do this"

Flyweight.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ interface Action {
4848
}
4949

5050
// USAGE:
51-
const start = Math.floor(Date.now());
52-
for (let i = 0; i < 1000; i++) {
53-
// new Soldier('normal-set', i); // create 1m real soldiers
54-
SoldierAcademy.getSoldier('normal-set', i); // create 1 soldier
55-
}
56-
const end = Math.floor(Date.now());
57-
console.log(end - start);
51+
const start = Math.floor(Date.now());
52+
for (let i = 0; i < 1000; i++) {
53+
// new Soldier('normal-set', i); // create 1m real soldiers
54+
SoldierAcademy.getSoldier('normal-set', i); // create 1 soldier
55+
}
56+
const end = Math.floor(Date.now());
57+
console.log(end - start);
5858
// OUTPUT:
5959
// "new soldier 0"
6060
// "shared soldier 1"

Interpreter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class AddExpression implements IntegerExpression {
6666
}
6767
}
6868

69+
// USAGE:
6970
let context = new IntegerContext();
7071

7172
let a = new IntegerVariableExpression("A");
@@ -79,4 +80,6 @@ context.assign(b, 1);
7980
context.assign(c, 3);
8081

8182
const result = expression.evaluate(context);
82-
console.log(result); //6
83+
console.log(result);
84+
// OUTPUT:
85+
//6

Iterator.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
1-
type Novella = {
2-
name: string;
3-
};
1+
interface MyIterator {
2+
next(): any;
3+
hasNext(): boolean;
4+
}
5+
6+
interface Aggregator {
7+
createIterator(): MyIterator;
8+
}
49

5-
type Novellas = {
6-
novellas: Novella[];
10+
class ConcreteIterator implements MyIterator {
11+
private collection: any[] = [];
12+
private position: number = 0;
713

8-
makeIterator(): NovellaIterator {
9-
return new NovellaIterator(novellas)
14+
constructor(collection: any[]) {
15+
this.collection = collection;
1016
}
11-
};
1217

13-
class NovellaIterator {
14-
current = 0;
15-
novellas: Novella[];
18+
public next(): any {
19+
// Error handling is left out
20+
var result = this.collection[this.position];
21+
this.position += 1;
22+
return result;
23+
}
1624

17-
constructor(novellas: Novella[]) {
18-
this.novellas = novellas;
25+
public hasNext(): boolean {
26+
return this.position < this.collection.length;
1927
}
28+
}
29+
30+
class Numbers implements Aggregator {
31+
private collection: number[] = [];
2032

21-
next(): Novella {
22-
return this.novellas.length > this.current++ ? this.novellas[this.current] : null;
33+
constructor(collection: number[]) {
34+
this.collection = collection;
35+
}
36+
public createIterator(): MyIterator {
37+
return new ConcreteIterator(this.collection);
2338
}
2439
}
40+
41+
// USAGE:
42+
var nArray = [1, 7, 21, 657, 3, 2, 765, 13, 65],
43+
numbers: Numbers = new Numbers(nArray),
44+
it: MyIterator = numbers.createIterator();
2545

46+
while (it.hasNext()) {
47+
console.log(it.next());
48+
}
49+
// OUTPUT:
50+
// 1
51+
// 7
52+
// 21
53+
// 657
54+
// 3
55+
// 2
56+
// 765
57+
// 13
58+
// 65

Observer.ts

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
11
interface Observer {
2-
uniqueID: string;
3-
update(): void;
2+
uniqueID: string;
3+
update(): void;
4+
}
5+
6+
class ConcreteObserver implements Observer {
7+
public uniqueID: string
8+
constructor(uniqueID: string) {
9+
this.uniqueID = uniqueID;
410
}
5-
6-
class ConcreteObserver implements Observer {
7-
public uniqueID: string
8-
constructor(uniqueID: string) {
9-
this.uniqueID = uniqueID;
10-
}
11-
12-
update(): void {
13-
console.log(`${this.uniqueID} updates something...`);
14-
}
11+
12+
update(): void {
13+
console.log(`${this.uniqueID} updates something...`);
1514
}
16-
17-
function findObserver(obs: Observer[], uniqueID: string) {
18-
let index = 0;
19-
const existed = obs.some((observer, idx) => {
20-
index = idx;
21-
return observer.uniqueID === uniqueID;
22-
});
23-
return {
24-
existed,
25-
index
26-
};
15+
}
16+
17+
function findObserver(obs: Observer[], uniqueID: string) {
18+
let index = 0;
19+
const existed = obs.some((observer, idx) => {
20+
index = idx;
21+
return observer.uniqueID === uniqueID;
22+
});
23+
return {
24+
existed,
25+
index
26+
};
27+
}
28+
29+
class Subject {
30+
private _observers: Observer[];
31+
constructor() {
32+
this._observers = [];
2733
}
28-
29-
class Subject {
30-
private _observers: Observer[];
31-
constructor() {
32-
this._observers = [];
34+
35+
registerObserver(ob: Observer) {
36+
const id: string = ob.uniqueID;
37+
if (findObserver(this._observers, id).existed) {
38+
return console.log(`Observer ${id} is already in list`);
3339
}
34-
35-
registerObserver(ob: Observer) {
36-
const id: string = ob.uniqueID;
37-
if (findObserver(this._observers, id).existed) {
38-
return console.log(`Observer ${id} is already in list`);
39-
}
40-
this._observers.push(ob);
41-
console.log(`Observer ${ob.uniqueID} is pushed into list`);
42-
console.log(this._observers);
43-
}
44-
45-
removeObserver(uniqueID: string) {
46-
const { existed, index } = findObserver(this._observers, uniqueID);
47-
if (existed) {
48-
this._observers.splice(index, 1);
49-
console.log(`Observer ${uniqueID} is removed from list`);
50-
} else {
51-
console.log('Observer not existed');
52-
}
53-
}
54-
55-
notifyObservers() {
56-
console.log('Subject notify all observers >>');
57-
this._observers.map((observer) => {
58-
observer.update();
59-
});
40+
this._observers.push(ob);
41+
console.log(`Observer ${ob.uniqueID} is pushed into list`);
42+
console.log(this._observers);
43+
}
44+
45+
removeObserver(uniqueID: string) {
46+
const { existed, index } = findObserver(this._observers, uniqueID);
47+
if (existed) {
48+
this._observers.splice(index, 1);
49+
console.log(`Observer ${uniqueID} is removed from list`);
50+
} else {
51+
console.log('Observer not existed');
6052
}
6153
}
54+
55+
notifyObservers() {
56+
console.log('Subject notify all observers >>');
57+
this._observers.map((observer) => {
58+
observer.update();
59+
});
60+
}
61+
}
6262

6363
// USAGE:
64-
const subject = new Subject();
65-
66-
const obA = new ConcreteObserver('A');
67-
const obB = new ConcreteObserver('B');
68-
const obC = new ConcreteObserver('C');
69-
70-
subject.registerObserver(obA);
71-
subject.registerObserver(obA); // already existed
72-
73-
subject.registerObserver(obB);
74-
subject.registerObserver(obC);
75-
76-
subject.notifyObservers();
64+
const subject = new Subject();
65+
66+
const obA = new ConcreteObserver('A');
67+
const obB = new ConcreteObserver('B');
68+
const obC = new ConcreteObserver('C');
69+
70+
subject.registerObserver(obA);
71+
subject.registerObserver(obA); // already existed
72+
73+
subject.registerObserver(obB);
74+
subject.registerObserver(obC);
75+
76+
subject.notifyObservers();
7777
// OUTPUT:
7878
// "Observer A is pushed into list"
7979
// "Observer A is already in list"

0 commit comments

Comments
 (0)