object design-part-2

14
Dhaval Dalal software-artisan.com Part II: Moving Towards a Simple Design OBJECT DESIGN

Upload: dhaval-dalal

Post on 17-Dec-2014

254 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Object design-part-2

Dhaval Dalal software-artisan.com

Part II: Moving Towards a Simple Design

OBJECT DESIGN

Page 2: Object design-part-2

OBJECTCREATION

A E

D

C

B

Page 3: Object design-part-2

WAYS TO FULFILL DEPENDENCIES

Create the collaborator within a constructor/method.

Perform a look-up for a collaborator.

Pass the collaborator from outside.

using a Constructor.

using a Setter.

Page 4: Object design-part-2

4

INJECT DEPENDENCIES

First of all, avoid concrete instantiation of collaborator objects within a constructor/method of an object.

Avoid making call to ‘new’

If you still have to do it, then encapsulate call to ‘new’ by using

A static Factory Method

Factory.

Builder.

Page 5: Object design-part-2

5

INJECT DEPENDENCIES/CLOSURES

Second option, do a “look-up” for the collaborator object

Use a Service Locator.

Third option, inject concrete implementation of a collaborator in to an object at run-time.

Use a DI Container.

Fourth option, inject code block instead of a collaborator.

Page 6: Object design-part-2

6

A GENERAL GUIDELINEShort Answer

Dependency Elimination is better than Dependency Inversion

Long AnswerFavor Closure Injection

(where its possible and sensible) over

Dependency Injection over

Look-up over

Concrete Instantiation.

Page 7: Object design-part-2

SETTER OR CONSTRUCTOR INJECTION?

A tell tale sign of exposing internal implementation manifests as bunch of getters and setters on the object.

Setters and Getters break the Command/Query Pattern (Tell, Don’t ask principle)

Inject collaborators in Constructors.

Page 8: Object design-part-2

THE SIDE EFFECT?

Too many parameters in the Constructor!

Page 9: Object design-part-2

<<STEREOTYPICAL RELATIONS>> OBJECT AND ITS

COLLABORATORSReal Dependency

Policy

Part

Notifications

Page 10: Object design-part-2

GUIDELINES OF THUMB

Pass only Real Dependencies through constructors.

Without which an object cannot fulfill its responsibilities.

For Policies and Parts

Provide Defaults and allow them to be set through Setters.

For Notifications

Provide NULL/EMPTY Listeners as default and allow new ones to be added/removed using “Adders/Removers”

Page 11: Object design-part-2

I STILL HAVE A BIG PARAMETER LIST?

Too many things going on or have more than one concept in that object (SRP violation).

Break-up the object.

This will collapse the parameter list.

Page 12: Object design-part-2

12

THE RESULT?

Fewer unintended consequences from code changes and more flexibility in your systems.

Creates Objects that are isolated from each other.

Makes large scale re-structuring of the system possible.

Page 13: Object design-part-2

13

VALUE OBJECTS

Must have value semantics.

Immutability

Value once set, cannot be changed.

Operations on them, returns new value objects

Object’s Identity is its value

Equality

Strive to reuse value objects

Page 14: Object design-part-2

REFERENCES

Agile Principles, Patterns and Practices

Robert C. Martin and Micah Martin

Object Design

Rebecca Wirfs-Brock and Alan McKean

Alec Sharp

The Pragmatic Programmers

Mock Roles, not Objects

Paper by Steve Freeman, Nat Pryce, Tim Mackinnon, Joe Walnes

InfoQ presentation by

Steve Freeman and Nat Pryce

Head-First Design Patterns Book

Venkat Subramaniam’s Blog