Skip to content

Commit d186f6d

Browse files
committed
[ts] refactor mediator
1 parent 832a138 commit d186f6d

File tree

1 file changed

+14
-50
lines changed

1 file changed

+14
-50
lines changed

Diff for: mediator-pattern/mediator-pattern.ts

+14-50
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,18 @@
1313
*/
1414

1515
class StockOffer {
16-
private stockShares: number = 0;
17-
private stockSymbol: string = "";
18-
private colleagueCode: number = 0;
19-
2016
public constructor(
21-
stockShares: number,
22-
stockSymbol: string,
23-
colleagueCode: number
24-
) {
25-
this.stockShares = stockShares;
26-
this.stockSymbol = stockSymbol;
27-
this.colleagueCode = colleagueCode;
28-
}
29-
30-
public getStockShares(): number {
31-
return this.stockShares;
32-
}
33-
34-
public getStockSymbol(): string {
35-
return this.stockSymbol;
36-
}
37-
38-
public getColleagueCode(): number {
39-
return this.colleagueCode;
40-
}
17+
readonly stockShares: number,
18+
readonly stockSymbol: string,
19+
readonly colleagueCode: number
20+
) {}
4121
}
4222

4323
abstract class Colleague {
44-
private mediator: Mediator;
45-
private colleagueCode: number;
24+
private colleagueCode: number = -1;
4625

47-
public constructor(mediator: Mediator) {
48-
this.mediator = mediator;
49-
this.mediator.addColleague(this);
26+
public constructor(private readonly mediator: Mediator) {
27+
mediator.addColleague(this);
5028
}
5129

5230
public saleOffer(stock: string, shares: number): void {
@@ -83,27 +61,16 @@ interface Mediator {
8361
}
8462

8563
class StockMediator implements Mediator {
86-
private colleagues: { [key: number]: Colleague };
87-
private stockBuyOffers: Array<StockOffer>;
88-
private stockSaleOffers: Array<StockOffer>;
89-
9064
private colleagueCode: number = 0;
91-
92-
public constructor() {
93-
this.colleagueCode = 0;
94-
this.colleagues = {};
95-
this.stockBuyOffers = [];
96-
this.stockSaleOffers = [];
97-
}
65+
private colleagues: Record<number, Colleague> = {};
66+
private stockBuyOffers: StockOffer[] = [];
67+
private stockSaleOffers: StockOffer[] = [];
9868

9969
saleOffer(stock: string, shares: number, colleagueCode: number): void {
10070
let stockSold: boolean = false;
10171

10272
for (let offer of this.stockBuyOffers) {
103-
if (
104-
offer.getStockSymbol() === "stock" &&
105-
offer.getStockShares() === shares
106-
) {
73+
if (offer.stockSymbol === "stock" && offer.stockShares === shares) {
10774
console.log(
10875
`${shares} shares of ${stock} sold to colleague code ${colleagueCode}`
10976
);
@@ -124,10 +91,7 @@ class StockMediator implements Mediator {
12491
let stockBought: boolean = false;
12592

12693
for (let offer of this.stockSaleOffers) {
127-
if (
128-
offer.getStockSymbol() === stock &&
129-
offer.getStockShares() === shares
130-
) {
94+
if (offer.stockSymbol === stock && offer.stockShares === shares) {
13195
console.log(
13296
`${shares} shares of ${stock} bought by colleague code ${colleagueCode}`
13397
);
@@ -154,12 +118,12 @@ class StockMediator implements Mediator {
154118
getStockOfferings(): void {
155119
console.log("Stocks for sale");
156120
for (let offer of this.stockSaleOffers) {
157-
console.log(`\t- ${offer.getStockShares()} of ${offer.getStockSymbol()}`);
121+
console.log(`\t- ${offer.stockShares} of ${offer.stockSymbol}`);
158122
}
159123

160124
console.log("Stocks buy offers:");
161125
for (let offer of this.stockBuyOffers) {
162-
console.log(`\t- ${offer.getStockShares()} of ${offer.getStockSymbol()}`);
126+
console.log(`\t- ${offer.stockShares} of ${offer.stockSymbol}`);
163127
}
164128
}
165129
}

0 commit comments

Comments
 (0)