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

OOP Principles

OOP Principles

Avatar for Ash Isaac

Ash Isaac

June 30, 2015
Tweet

More Decks by Ash Isaac

Other Decks in Programming

Transcript

  1. Why OOP? OOP benefits Developers We naturally think/talk in terms

    of objects, attributes, behaviors, interactions, events - Should present a useful Model of the real world - Expressive: exposes how things work in plain language - Helps reason about your problem domain - Handles increasing complexity Samples: github / Cart
  2. What is OOP? Using Classes: Encapsulation, Inheritance, Polymorphism (blah blah…)

    OOP Principles 1) Single Responsibility 2) Encapsulation 3) Semantic Modeling 4) Loose Coupling 5) Types of Objects
  3. OOP Principles 1) Single Responsibility: • Small, single task Classes

    + Small single-task Methods • A class should have only one reason to change • Separate Queries (->get*) & Commands (->update*) • Use Factory classes ( CartFactory::Create() )
  4. OOP Principles 2) Encapsulation: • Expose "Public API" for class,

    Hide implementation -- default to ‘protected’ not public • Self-validating objects
  5. OOP Principles 3) Semantic Modeling: • Semantic Naming (avoid names

    like "xxxManager") • Model Relationships ("Is-A", "Has-A" etc) Favor composition over inheritance -- unless you are building a framework
  6. OOP Principles 4) Loose Coupling: • Modules -- high cohesion,

    internal dependencies • Use interfaces // Type Hinting • Dependency Inversion: Constructor/Setter injection • Use Events // Commands // Services
  7. OOP Principles 5) Types of Objects: • Entities -- have

    an identity and a lifecycle • Value Objects -- descriptive, immutable data objects • Aggregates -- Composite Objects w/Root Entity • Collections & Repositories