- Mastering Angular Components
- Gion Kunz
- 383字
- 2021-07-23 17:23:37
Composability
Composition is a special kind of reusability. You don't extend an existing component, but you create a new, larger component by composing many smaller components together into a system of components.
In OOP languages, composition is often used to get around the multiple inheritance issues that most OOP languages have. Subclass polymorphism is always great until you reach the point where your design does not match the latest requirements in your project. Let's look at a simple example that illustrates this problem.
You have a Fisher class and a Developer class, both of which hold specific behaviors. Now, you'd want to create a FishingDeveloper class that inherits from both Fisher and Developer. Unless you're using a language that supports multiple inheritance (such as C++, which does this to a certain extent), you will not be able to reuse this functionality using inheritance. There is no way to tell the language that your new class should inherit from both superclasses. Using composition, you can easily solve this problem. Instead of using inheritance, you're composing a new FishingDeveloper class that delegates all behavior to an internal Developer and Fisher instance:
interface IDeveloper {
code(): void;
}
interface IFisher {
fish(): void;
}
class Developer implements IDeveloper {
constructor(private name: string) {}
code(): void {
console.log(`${this.name} writes some code!`);
}
}
class Fisher implements IFisher {
constructor(private name: string) {}
fish(): void {
console.log(`${this.name} catches a big fish!`);
}
}
class FishingDeveloper implements IFisher, IDeveloper {
constructor(private name: string) {
this.name = name;
this.developerStuff = new Developer(name);
this.fisherStuff = new Fisher(name);
}
code(): void {
this.developerStuff.code();
}
fish(): void {
this.fisherStuff.fish();
}
}
var bob: FishingDeveloper = new FishingDeveloper('Bob');
bob.code();
bob.fish();
Experience has taught us that composition is probably the most efficient way to reuse code. In contrast to inheritance, decoration, and other approaches to gain reusability, composition is probably the least intrusive and the most flexible.
Recent versions of some languages also support a pattern called traits, that is, mixins. Traits allow you to reuse certain functionality and attributes from other classes in a way that is similar to multiple inheritance.
If we think about the concept of composition, it's nothing more than designing organisms. We have the two Developer and Fisher organisms, and we unify their behaviors into a single FishingDeveloper organism.
- 異構(gòu)基因共表達(dá)網(wǎng)絡(luò)的分析方法
- 計(jì)算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- Windows Server 2012 Hyper-V虛擬化管理實(shí)踐
- 面向5G-Advanced的關(guān)鍵技術(shù)
- Master Apache JMeter:From Load Testing to DevOps
- React Cookbook
- 網(wǎng)絡(luò)工程實(shí)施技術(shù)與方案大全
- 設(shè)備監(jiān)控技術(shù)詳解
- Implementing NetScaler VPX?
- 物聯(lián)網(wǎng)基礎(chǔ)及應(yīng)用
- SRv6網(wǎng)絡(luò)部署指南
- Cisco無線局域網(wǎng)配置基礎(chǔ)
- 黑客與反黑工具使用詳解
- 5G新型多址技術(shù)
- 學(xué)術(shù)虛擬社區(qū)用戶社會化交互行為研究