15 minpresentation
Bridge Pattern Structure
Bridge Pattern Structure
0 / 6 completedBasic Implementation
typescript1// Implementor interface2interface Renderer {3 renderShape(shape: string): void;4 renderColor(color: string): void;5}67// Concrete Implementors8class ScreenRenderer implements Renderer {9 renderShape(shape: string): void {10 console.log(`Drawing ${shape} on screen`);11 }12 renderColor(color: string): void {13 console.log(`Applying ${color} on screen`);14 }15}1617class PrintRenderer implements Renderer {18 renderShape(shape: string): void {19 console.log(`Printing ${shape}`);20 }21 renderColor(color: string): void {22 console.log(`Printing in ${color}`);23 }24}2526// Abstraction27abstract class Shape {28 constructor(protected renderer: Renderer) {}29 abstract draw(): void;30}3132// Refined Abstractions33class Circle extends Shape {34 draw(): void {35 this.renderer.renderShape('circle');36 this.renderer.renderColor('blue');37 }38}3940class Square extends Shape {41 draw(): void {42 this.renderer.renderShape('square');43 this.renderer.renderColor('red');44 }45}
Step 1 of 6
← → NavigateSpace: Skip / NextEnter: Next