Share
Embed
Transcript
17 slides
-
Overview • History • OO Principles • Applying OO to
Rails • Exercises Wednesday, August 15, 12 -
History • Gang of Four • Design Patterns: Elements of
Reusable Object-Oriented Software • Agile Movement • http://agilemanifesto.org/principles.html Wednesday, August 15, 12 -
GoF Patterns • Factory • Singleton • Adapter • Decorator
• Facade • Proxy • Singleton • Observer • Strategy • Iterator Wednesday, August 15, 12 -
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 -
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 -
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 -
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 -
Reduce Coupling • Having many collaborators is a maintenance nightmare
• Changes in one object ripple throughout the system Wednesday, August 15, 12 -
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 -
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 -
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 -
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 -
Applied OO • Composition (Reduce Coupling) • Subclasses are tightly
coupled to superclass • Use instances of the class rather than inheritance Wednesday, August 15, 12 -
Applied OO • Delegation (Reduce Coupling) • Composed objects can
be exposed via method calls • “Delegate” calls to wrapped objects Wednesday, August 15, 12 -
References • Design Patterns (GoF) • Design Patterns in Ruby
Wednesday, August 15, 12