Coder Social home page Coder Social logo

composition-vs-inheritance's Introduction

composition-vs-inheritance

๐Ÿคฏ ๐Ÿญ๐Ÿฌ ๐—ฃ๐—ต๐—ฒ๐—ป๐—ผ๐—บ๐—ฒ๐—ป๐—ฎ๐—น ๐—ฅ๐—ฒ๐—ฎ๐˜€๐—ผ๐—ป๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—ณ๐—ฎ๐˜ƒ๐—ผ๐˜‚๐—ฟ๐—ถ๐—ป๐—ด ๐—–๐—ผ๐—บ๐—ฝ๐—ผ๐˜€๐—ถ๐˜๐—ถ๐—ผ๐—ป ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐—œ๐—ป๐—ต๐—ฒ๐—ฟ๐—ถ๐˜๐—ฎ๐—ป๐—ฐ๐—ฒ

  1. ๐—™๐—น๐—ฒ๐˜…๐—ถ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†: Composition allows for easier combining and altering of behaviours without affecting other parts of the system.

  2. ๐—Ÿ๐—ผ๐—ผ๐˜€๐—ฒ ๐—–๐—ผ๐˜‚๐—ฝ๐—น๐—ถ๐—ป๐—ด: Composition reduces component dependency, making systems more maintainable.

  3. ๐—ข๐˜ƒ๐—ฒ๐—ฟ๐—ฐ๐—ผ๐—บ๐—ฒ๐˜€ ๐—ฆ๐—ถ๐—ป๐—ด๐—น๐—ฒ ๐—œ๐—ป๐—ต๐—ฒ๐—ฟ๐—ถ๐˜๐—ฎ๐—ป๐—ฐ๐—ฒ: While many languages only support inheriting from one class, composition lets an object combine multiple components.

  4. ๐—–๐—น๐—ฒ๐—ฎ๐—ฟ ๐—ฅ๐—ฒ๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€๐—ต๐—ถ๐—ฝ๐˜€: Composition often leads to clearer HAS-A relationships (e.g., a ๐™ฒ๐šŠ๐š› has an ๐™ด๐š—๐š๐š’๐š—๐šŽ) as opposed to the IS-A relationships of Inheritance.

  5. ๐—ฅ๐—ฒ๐˜‚๐˜€๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†: Objects can be reused in various contexts without creating subclass hierarchies.

  6. ๐—”๐˜ƒ๐—ผ๐—ถ๐—ฑ๐˜€ ๐—™๐—ฟ๐—ฎ๐—ด๐—ถ๐—น๐—ฒ ๐—•๐—ฎ๐˜€๐—ฒ ๐—–๐—น๐—ฎ๐˜€๐˜€ ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ: Changes in a base class might unintentionally affect subclasses. Composition mitigates this issue.

  7. ๐——๐˜†๐—ป๐—ฎ๐—บ๐—ถ๐—ฐ ๐—•๐—ฒ๐—ต๐—ฎ๐˜ƒ๐—ถ๐—ผ๐—ฟ: Composition allows changing of operations at runtime by switching out components.

  8. ๐—˜๐—ป๐—ฐ๐—ฎ๐—ฝ๐˜€๐˜‚๐—น๐—ฎ๐˜๐—ถ๐—ผ๐—ป: Composition can better hide the internal details of components.

  9. ๐—ฅ๐—ฒ๐—ฎ๐—ฑ๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†: Overusing Inheritance can cloud code's origin and purpose. Composition tends to be more explicit.

  10. ๐—™๐—ฒ๐˜„๐—ฒ๐—ฟ ๐—ฆ๐—ถ๐—ฑ๐—ฒ ๐—˜๐—ณ๐—ณ๐—ฒ๐—ฐ๐˜๐˜€: Composition reduces unexpected behaviours by containing them within distinct objects.

While Inheritance models "IS-A" relationships, Composition's ๐—™๐—น๐—ฒ๐˜…๐—ถ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜†, ๐—–๐—น๐—ฎ๐—ฟ๐—ถ๐˜๐˜†, and ๐— ๐—ฎ๐—ถ๐—ป๐˜๐—ฎ๐—ถ๐—ป๐—ฎ๐—ฏ๐—ถ๐—น๐—ถ๐˜๐˜† often make it a more suitable choice for modelling "HAS-A" relationships.

image

Discussion

https://www.linkedin.com/feed/update/urn:li:activity:7150741654398713856/

composition-vs-inheritance's People

Contributors

shaqse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.