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

Object-Oriented Design

Avatar for bjreath bjreath
August 08, 2012

Object-Oriented Design

Avatar for bjreath

bjreath

August 08, 2012
Tweet

More Decks by bjreath

Other Decks in Programming

Transcript

  1. Overview • History • OO Principles • Applying OO to

    Rails • Exercises Wednesday, August 15, 12
  2. History • Gang of Four • Design Patterns: Elements of

    Reusable Object-Oriented Software • Agile Movement • http://agilemanifesto.org/principles.html Wednesday, August 15, 12
  3. GoF Patterns • Factory • Singleton • Adapter • Decorator

    • Facade • Proxy • Singleton • Observer • Strategy • Iterator Wednesday, August 15, 12
  4. Motivation • The only constant is change • Making changes

    to the application shouldn’t be hindered by our practices • Predicting change is hard Wednesday, August 15, 12
  5. Principles • Single Responsibility (SRP) • Small Methods • Reduce

    Coupling • Limit Use of Globals and Singletons • Small Interfaces with Simple Signatures • Prefer Composition over Inheritance Wednesday, August 15, 12
  6. Single Responsibility • Every object in the system should perform

    one conceptual “responsibility” • Classes should be small and focused • The less an object does, the easier it is to change (or remove) Wednesday, August 15, 12
  7. Small Methods • Rambling, conditional logic is hard to follow

    • Algorithms should be thought about and broken down into steps • Steps should be given names • Individual steps can be ‘stubbed’ in tests • Steps can be reused from different algorithms Wednesday, August 15, 12
  8. Reduce Coupling • Having many collaborators is a maintenance nightmare

    • Changes in one object ripple throughout the system Wednesday, August 15, 12
  9. Applied OO • Presenter Pattern (Applied SRP) • Take view-specific

    logic out of models • Place in custom formatting object • Make use of view layer helpers Wednesday, August 15, 12
  10. Applied OO • Service Pattern (Applied SRP) • Remove business

    logic from AR • Move into application-specific business logic objects • Detach from AR concerns such as callbacks, validations, etc. Wednesday, August 15, 12
  11. Applied OO • Strategy Pattern (Coping with Change) • Abstract

    algorithms into “Strategy” classes • Calculations can be performed in one way initially, but new “strategies” can be plugged in should requirements change Wednesday, August 15, 12
  12. Applied OO • Dependency Injection (Code to Interface) • Pass

    dependencies into object, through constructor or setter method • Implementations can be changed at runtime • Tests can stub out heavyweight implementations Wednesday, August 15, 12
  13. Applied OO • Composition (Reduce Coupling) • Subclasses are tightly

    coupled to superclass • Use instances of the class rather than inheritance Wednesday, August 15, 12
  14. Applied OO • Delegation (Reduce Coupling) • Composed objects can

    be exposed via method calls • “Delegate” calls to wrapped objects Wednesday, August 15, 12