Created
March 31, 2024 21:57
-
-
Save Akash52/9a158cd0d0061f994cfac56ef03fceb4 to your computer and use it in GitHub Desktop.
This file contains 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
// Interface defining the behavior of content items | |
interface Content { | |
displayContent(): void; | |
} | |
// Class for displaying text content | |
class TextContent implements Content { | |
constructor(private text: string) {} | |
displayContent(): void { | |
console.log(`Text Content: ${this.text}`); | |
} | |
} | |
// Class for displaying image content | |
class ImageContent implements Content { | |
constructor(private imageUrl: string) {} | |
displayContent(): void { | |
console.log(`Image Content: ${this.imageUrl}`); | |
} | |
} | |
// Content manager class that can display different types of content | |
class ContentManager { | |
private contentItems: Content[] = []; | |
addContent(content: Content): void { | |
this.contentItems.push(content); | |
} | |
displayAllContent(): void { | |
this.contentItems.forEach(content => { | |
content.displayContent(); | |
}); | |
} | |
} | |
// Usage example | |
const manager = new ContentManager(); | |
const textContent = new TextContent("Welcome to our website!"); | |
const imageContent = new ImageContent("https://example.com/image.jpg"); | |
manager.addContent(textContent); | |
manager.addContent(imageContent); | |
manager.displayAllContent(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment