This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MessageRouter { | |
public route(message: Message): void { | |
//!! ouch... x( | |
ExternalRouter.getInstance().sendMessage(message); | |
} | |
} | |
export class ExternalRouter { // another Singleton! x( | |
private static instance: ExternalRouter | null = null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After applying Subclass & Override Method | |
class RegisterSale { | |
// more code... | |
public addItem(code: Barcode): void { | |
const newItem = this.getInventory().getItemForBarCode(code); | |
this.items.push(newItem); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegisterSale { | |
// more code... | |
public addItem(code: Barcode): void { | |
//!! After Extract Method | |
const newItem = this.getInventory().getItemForBarCode(code); | |
this.items.push(newItem); | |
} | |
//!! Extracted method. Now we can apply Subclass & Override Method here!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Inventory { | |
private static inventory: Inventory | null = null; | |
public static getInstance(): Inventory { | |
if (this.inventory === null) { | |
this.inventory = new Inventory(); | |
} | |
return this.inventory; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileTransactionsRepository implements TransactionsRepository { | |
public getAll(): Transaction[] { | |
let transactions: Transaction[] = []; | |
// read the transactions from some file... | |
return transactions; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account { | |
private balance: number; | |
private readonly logger: AcmeLogger; | |
constructor(balance: number) { | |
this.balance = balance; | |
this.logger = new AcmeLogger(); | |
} | |
public withdraw(value: number): void { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account { | |
private balance: number; | |
private readonly logger: AcmeLogger; | |
constructor(balance: number) { | |
this.balance = balance; | |
this.logger = new AcmeLogger(); | |
} | |
public withdraw(value: number): void { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// New interface comes from applying Extract Interface to the wrapper | |
interface LibraryData { | |
getLibraryName(): string; | |
} | |
// Notice how we renamed the wrapper to free the interface name | |
class AcmeInputReaderLibraryData implements LibraryData { | |
private readonly inputReader: AcmeInputReader ; | |
constructor(inputReader: AcmeInputReader) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Library { | |
private display: Display; | |
constructor(display: Display) { | |
this.display = display; | |
} | |
printBooks(books: Book[], inputReader: AcmeInputReader): void { | |
const libraryData = new LibraryData(inputReader); | |
const libraryName = libraryData.getLibraryName(); // <- moved method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After steps 1 and 2 | |
class Library { | |
private readonly display: Display; | |
constructor(display: Display) { | |
this.display = display; | |
} | |
printBooks(books: Book[], inputReader: AcmeInputReader): void { |