https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance

解答1

Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type. So the goose is more or less cooked once you derive from TypeA.

My acid test for the above is:

  • Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected? Indicates Inheritance.

e.g. A Cessna biplane will expose the complete interface of an airplane, if not more. So that makes it fit to derive from Airplane.

  • Does TypeB want only some/part of the behavior exposed by TypeA? Indicates need for Composition.

e.g. A Bird may need only the fly behavior of an Airplane. In this case, it makes sense to extract it out as an interface / class / both and make it a member of both classes.

Update: Just came back to my answer and it seems now that it is incomplete without a specific mention of Barbara Liskov's Liskov Substitution Principle as a test for 'Should I be inheriting from this type?'

解答2

Think of containment as a has a relationship. A car "has an" engine, a person "has a" name, etc.

Think of inheritance as an is a relationship. A car "is a" vehicle, a person "is a" mammal, etc.

I take no credit for this approach. I took it straight from the Second Edition of Code Complete by Steve McConnell, Section 6.3.

组合优于继承 Composition over inheritance的更多相关文章

  1. Effective Java 第三版——18. 组合优于继承

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  2. Head First 设计模式--1策略模式 组合优于继承

    策略模式:第一了算法族,分别封装起来,让他们之间可以互相替换,次模式让算法的变化独立于使用算法的客户. 首先看个错误的面向对象. 假如我们需要写一个关于鸭子的程序,各种类型的鸭子.第一想到的就是建一个 ...

  3. 28-React state提升、组件组合或继承

    Lifting State Up state提升 对于在React应用程序中更改的任何数据,应该有一个单一的数据源.通常,都是将state添加到需要渲染的组件.如果其他组件也需要它,您可以将其提升到最 ...

  4. 用组合取代继承能为 Activity 带来什么

    用组合取代继承能为 Activity 带来什么 原文链接 : Composition over Inheritance,What it means for your Activities 原文作者 : ...

  5. java-组合优于继承

    组合和继承.都能实现对类的扩展. 差别例如以下表所看到的 组合 继承 has-a关系 is-a关系 执行期决定 编译期决定 不破坏封装,总体和局部松耦合 破坏封装,子类依赖父类 支持扩展,任意添加组合 ...

  6. 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)

    坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...

  7. Java(Android)编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)

    1.组合和继承之间的选择 组合和继承都允许在新的类中放置子对象,组合是显式的这样做,而继承则是隐式的做. 组合技术通常用于想在新类中使用现有类的功能而非它的接口这种情形.即在新类中嵌入某个对象,让其实 ...

  8. oc随笔二:组合、继承

    在oc中如果没有使用ARC的话,手动管理内存一定要注意处理好“野指针”,通常我们在释放指针的指向的地址时,都要将指针赋值为nil,这样能有效的防止野指针.常用的关键字:retain.assign .s ...

  9. (笔记):组合and继承之访问限制(一)

    下面在介绍组合与继承之前,先介绍一下访问限制,访问限制:public.protected.private三者是按照授权的大小排序的.这里有个博客,对这三者有了经典的诠释.http://blog.csd ...

随机推荐

  1. Bootstrap插件架构 基于元素自定义属性的布局规则

    w HTML布局规则 Javascript实现步骤 插件调用方法

  2. Webpack4.x 入门

    概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...

  3. docker-compose部署zk集群、kafka集群以及kafka-manager,及其遇到的问题和解决

    zk集群docker-compose.yml 1.新建网络 docker network create --driver bridge --subnet --gateway 172.23.0.1 zo ...

  4. C#生成电子印章源码

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  5. sscanf,sscanf

    在#include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明: sscan ...

  6. 简明python教程八----输入/输出

    通过创建一个file类的对象来打开一个文件,分别使用file类的read.readline或write方法来读写文件. 最后调用一个close方法来告诉Python我们完成了对文件的使用. poem= ...

  7. WCF学习记录(一)

    在这里先简单记录下WCF配置的过程,关于WCF详解,下次再做具体描述. 1. Contract及其实现 a. [ServiceContract(Namespace = "http://www ...

  8. 『NiFi 自定义 Processor 无法获取到 Logger』问题解决

    一.概述 自定义的 Processor 继承了 AbstractProcessor,而 AbstractProcessor 继承了 AbstractSessionFactoryProcessor ,g ...

  9. Codeforces Round #430 (Div. 2) C. Ilya And The Tree

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

  10. rdesktop install notes

    在centos7上安装rdesktop来访问windows桌面,需要安装EPEL源,另外还有需要安装专门的YUM源. rpm -Uvh https://dl.fedoraproject.org/pub ...