Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Delegation, Forwarding, Inheritance, Composition, Aggregation, … I'm lost !

Delegation, Forwarding, Inheritance, Composition, Aggregation, … I'm lost !

A overview of these different development design concepts in Ruby.

Ben Colon

March 19, 2014
Tweet

Other Decks in Programming

Transcript

  1. Ben Colon • Senior Software Engineer @ Gatemedia SA •

    Former developer, entrepreneur, lead dev & CTO. • But I’m a DEVELOPER :)) • @bcolon
  2. Design Patterns ? • Wikipedia definition […] A design pattern

    is a general reusable solution to a commonly occurring problem within a given context in software design.
  3. Design Patterns Classification • « The Gang of four »

    classification • Creational patterns • Structural patterns • Behavioral patterns
  4. Design Patterns Classification • Creational patterns • Factory method pattern,

    Abstract factory pattern, Singleton pattern, Builder pattern, Prototype pattern, … • Structural patterns • Adapter pattern, Bridge pattern, Composite pattern, Decorator pattern, Facade pattern, Flyweight pattern, Proxy pattern, … • Behavioral patterns • Chain-of-responsibility pattern, Command pattern, Interpreter pattern, Iterator pattern, Mediator pattern, Memento pattern, Observer pattern, State pattern, Strategy pattern, Template method pattern, Visitor pattern, …
  5. Design Patterns Design « Concepts » • Inheritance, Composition, Aggregation

    • « Reusability » concepts • Delegation, Forwarding • « Behaviour » concepts
  6. Used by design patterns • Creational patterns • Factory method

    pattern, Abstract factory pattern, Singleton pattern, Builder pattern, Prototype pattern, … • Structural patterns • Adapter pattern, Bridge pattern, Composite pattern, Decorator pattern, Facade pattern, Flyweight pattern, Proxy pattern, … • Behavioral patterns • Chain-of-responsibility pattern, Command pattern, Interpreter pattern, Iterator pattern, Mediator pattern, Memento pattern, Observer pattern, State pattern, Strategy pattern, Template method pattern, Visitor pattern, … Delegation Inheritance Composition, Inheritance
  7. Dive into these « reusability » concepts • Inheritance :

    a class is based on another class
 « Is a » relationship • Composition : one object containing another one
 « Has a » relationship • Aggregation : one object using another one
 « Uses a » relationship
  8. Inheritance in Ruby • Ruby has single inheritance. Each class

    has one and only one parent class Implicit Inheritance Override Inheritance Altered Inheritance
  9. Aggregation in Ruby • « Uses a » relationship •

    Wikipedia says « Composition is where a structure directly includes its members, whereas Aggregation is where a structure only contains references to those members. » • In Ruby everything is object, so Aggregation is the same as Composition. • For C++ fans, just a * difference
  10. Dive into these « behavioral » concepts • Delegation on

    Wikipedia « An object, instead of performing one of its stated tasks, delegates that task to an associated helper object. » • Forwarding ? Nothing on Wikipedia but in Delegation page « this mechanism is sometimes referred to as forwarding » :) • DELEGATION = FORWARDING … semantically
  11. Delegation/Forwarding in Ruby • Several librairies/classes in Ruby : •

    Delegator, SimpleDelegator, DelegateClass • Forwardable & SingleForwardable • And in Rails, ActiveSupport::Delegation module
  12. require ‘delegate' 
 vs 
 require 'forwardable' • Delegate 


    allows you to delegate methods to a class,
 by class basis • Forwardable
 allows you to delegate specified methods to a class, 
 by method basis
  13. require 'delegate' • SimpleDelegator • Easiest way. Pass an object

    to the constructor and all methods supported by the object will be delegated
  14. Comparaisons • Delegation vs Forwarding • Composition vs Aggregation •

    Inheritance vs Delegation • Inheritance vs Composition
  15. Bad/Good comparaisons • Delegation vs Forwarding • Composition vs Aggregation

    • Inheritance vs Delegation • Inheritance vs Composition
  16. Inheritance vs Composition • « Favor object composition over class

    inheritance » The Gang of Four • More reusable, more flexible, better testability • A first guideline, « has a » composition, « is a » inheritance • But Liskov Substitution Principle seems to be more accurate
  17. Don’t forget the Mixins • Great tool to build Composition

    • « Mixins are the multiple inheritance facility I had dreamed about » An anonymous C++ developer.
  18. Sources • Wikipedia, StackOverflow & Ruby Documentation • Unleash the

    Secrets of the Standard Library with SimpleDelegator, Forwardable and more. • Re-use in OO: Inheritance, Composition and Mixins • Inheritance Vs. Composition