Object design-part-2

636 views
580 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
636
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Object design-part-2

  1. 1. Dhaval Dalalsoftware-artisan.comPart II: Moving Towards a Simple DesignOBJECT DESIGN
  2. 2. OBJECTCREATIONA EDCB
  3. 3. WAYS TO FULFILLDEPENDENCIESCreate the collaborator within a constructor/method.Perform a look-up for a collaborator.Pass the collaborator from outside.using a Constructor.using a Setter.
  4. 4. 4INJECT DEPENDENCIESFirst of all, avoid concrete instantiation ofcollaborator objects within a constructor/method ofan object.Avoid making call to ‘new’If you still have to do it, then encapsulate call to ‘new’by usingA static Factory MethodFactory.Builder.
  5. 5. 5INJECT DEPENDENCIES/CLOSURESSecond option, do a “look-up” for the collaboratorobjectUse a Service Locator.Third option, inject concrete implementation of acollaborator in to an object at run-time.Use a DI Container.Fourth option, inject code block instead of acollaborator.
  6. 6. 6A GENERAL GUIDELINEShort AnswerDependency Elimination is better than DependencyInversionLong AnswerFavor Closure Injection(where its possible and sensible)overDependency InjectionoverLook-upoverConcrete Instantiation.
  7. 7. SETTER OR CONSTRUCTORINJECTION?A tell tale sign of exposing internal implementationmanifests as bunch of getters and setters on theobject.Setters and Getters break the Command/QueryPattern (Tell, Don’t ask principle)Inject collaborators in Constructors.
  8. 8. THE SIDE EFFECT?Too many parameters in the Constructor!
  9. 9. <<STEREOTYPICAL RELATIONS>>OBJECT AND ITSCOLLABORATORSReal DependencyPolicyPartNotifications
  10. 10. GUIDELINES OF THUMBPass only Real Dependencies through constructors.Without which an object cannot fulfill its responsibilities.For Policies and PartsProvide Defaults and allow them to be set through Setters.For NotificationsProvide NULL/EMPTY Listeners as default and allow new onesto be added/removed using “Adders/Removers”
  11. 11. I STILL HAVE A BIGPARAMETER LIST?Too many things going on orhave more than one concept in that object (SRP violation).Break-up the object.This will collapse the parameter list.
  12. 12. 12THE RESULT?Fewer unintended consequences from code changesand more flexibility in your systems.Creates Objects that are isolated from each other.Makes large scale re-structuring of the system possible.
  13. 13. 13VALUE OBJECTSMust have value semantics.ImmutabilityValue once set, cannot be changed.Operations on them, returns new value objectsObject’s Identity is its valueEqualityStrive to reuse value objects
  14. 14. REFERENCESAgile Principles, Patterns andPracticesRobert C. Martin and MicahMartinObject DesignRebecca Wirfs-Brock and AlanMcKeanAlec SharpThe Pragmatic ProgrammersMock Roles, not ObjectsPaper by Steve Freeman, NatPryce,Tim Mackinnon, JoeWalnesInfoQ presentation bySteve Freeman and Nat PryceHead-First Design Patterns BookVenkat Subramaniam’s Blog

×