GithubHelp home page GithubHelp logo

classcoupling's Introduction

Class Coupling

Inheritance

  • A kind of relationship between 2 classes that allows one to inherit code from the other.
  • Referred to as 'Is A relationship': a car is a vehicle
  • Pro's: code re-use and polymorphic behaviour

Composition

  • A kind of relationship that allows one class to contain another.
  • Referred to as 'Has-A relationship': A Car has an Engine

Example

In the Inheritance project we have a Car class and Plane class which both inherit from the Vehicle class. The Vehicle class has 2 properties (NumberOfWheels and EngineSize) and has one method (Drive()).

The problem with both classes inheriting from Vehicle, is that a plane cannot drive.

In the Composition project

To overcome this we should use Composition, that way the Drive method should be moved to a seperate Drivable class, the Car class can then re-use the code in the Drivable class and the Plane class would not.

We could then implement a Flyable class which the Plane class can re-use the code but the car would not.

Favour Composition over Inheritance

Problems with inheritance:

  • Easily abused by amateur designers / developers
  • Leads to large complex hierarchies
  • Such hierarchies are very fragile and a change may affect many classes
  • Results in tight coupling
  • Benefits of composition:
  • Flexible
  • Leads to loose coupling
  • Having said all that, it doesn’t mean inheritance should be avoided at all times. In fact, it’s great to use inheritance when dealing with very stable classes on top of small hierarchies. As the hierarchy grows (or variations of classes increase), the hierarchy, however, becomes fragile. And that’s where composition can give you a better design.

classcoupling's People

Watchers

James Cloos 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.