Skip to content

Instantly share code, notes, and snippets.

@Akash52
Created March 31, 2024 21:57
Show Gist options
  • Save Akash52/9a158cd0d0061f994cfac56ef03fceb4 to your computer and use it in GitHub Desktop.
Save Akash52/9a158cd0d0061f994cfac56ef03fceb4 to your computer and use it in GitHub Desktop.
// 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