Skip to content

Instantly share code, notes, and snippets.

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;
// After applying Subclass & Override Method
class RegisterSale {
// more code...
public addItem(code: Barcode): void {
const newItem = this.getInventory().getItemForBarCode(code);
this.items.push(newItem);
}
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!!
class Inventory {
private static inventory: Inventory | null = null;
public static getInstance(): Inventory {
if (this.inventory === null) {
this.inventory = new Inventory();
}
return this.inventory;
}
class FileTransactionsRepository implements TransactionsRepository {
public getAll(): Transaction[] {
let transactions: Transaction[] = [];
// read the transactions from some file...
return transactions;
}
}
@trikitrok
trikitrok / ExtractAndOverrideCall.ts
Last active March 21, 2025 00:19
Extract and Override Call example
class Account {
private balance: number;
private readonly logger: AcmeLogger;
constructor(balance: number) {
this.balance = balance;
this.logger = new AcmeLogger();
}
public withdraw(value: number): void {
class Account {
private balance: number;
private readonly logger: AcmeLogger;
constructor(balance: number) {
this.balance = balance;
this.logger = new AcmeLogger();
}
public withdraw(value: number): void {
// 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) {
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
// After steps 1 and 2
class Library {
private readonly display: Display;
constructor(display: Display) {
this.display = display;
}
printBooks(books: Book[], inputReader: AcmeInputReader): void {