Class Composition
in
Software design
TypeScript's versatility
Inheritance and Polymorphism
Composition in Angular
Demo
X - @GeorgeKaplan_G
Born in Madrid.
Senior Frontend Architect @ Fever
Angular lover.
Likes cinema, books, and food.
Paradigms
Principles
Patterns
abstract class Animal {
abstract makeSound(): void;
move(distanceInMeters: number): void {
console.log(`This animal moved ${distanceInMeters} meters.`);
}
}
class Dog extends Animal {
makeSound(): void {
console.log('The dog barks.');
}
}
class Cat extends Animal {
makeSound(): void {
console.log('The cat meows.');
}
}
const dog = new Dog();
const cat = new Cat();
dog.makeSound(); // Output: The dog barks.
dog.move(10); // Output: This animal moved 10 meters.
cat.makeSound(); // Output: The cat meows.
cat.move(5); // Output: This animal moved 5 meters.
Allows us to apply Angular directives to a component's host element from within this component's TypeScript class.
It does not allow us to use properties or methods from any host directive directly in the component class.
@Component({
standalone: true,
[...],
hostDirectives: [MenuBehaviorDirective],
})
export class AdminMenuComponent {
menuOpen: boolean = inject(MenuBehaviorDirective).menuOpen;
}
Use mixins for class compositions that involve either adding or replacing class members. However, do use decorators for edge cases that may involve initializing or modifying functionality.