encapsulation codecampiasi 16 oct 2010

31
Encapsulation the good, the bad, the ugly Eigel Silviu-Horea, [email protected] www.endava.com 16 Octombrie 2010

Upload: codecampiasi

Post on 29-Nov-2014

868 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Encapsulation CodeCampIasi 16 oct 2010

Encapsulationthe good, the bad, the uglyEigel Silviu-Horea, [email protected]

www.endava.com

16 Octombrie 2010

Page 2: Encapsulation CodeCampIasi 16 oct 2010

AgendaFrom functional decomposition to OOPEncapsulationFrom bad and ugly to “The Good”

Building up encapsulationHandling variation

Conclusions

Page 3: Encapsulation CodeCampIasi 16 oct 2010

Some valuesCohesion

How closely the operations in a module are related

Coupling The strength of a connection between two modules

Page 4: Encapsulation CodeCampIasi 16 oct 2010

Some problemsRequirements Always ChangeChange is necessaryNew variation to an existing theme

Page 5: Encapsulation CodeCampIasi 16 oct 2010

Back in the daysProfessor and students Functional decompositionLasagna

Page 6: Encapsulation CodeCampIasi 16 oct 2010

OOPObjects as data with methodsClasses are things with responsibilities

AbstractionEncapsulationInheritancePolymorphism

Page 7: Encapsulation CodeCampIasi 16 oct 2010

The bad in its “definition”

Usually defined as data hiding

Page 8: Encapsulation CodeCampIasi 16 oct 2010

The ugliness is in our usageStill functionally decomposingCaused by it’s natural roots

Data hiding viewData - functionality separation

Page 9: Encapsulation CodeCampIasi 16 oct 2010

E as hidingInstead think of E asHiding – any kind of hidingImplementationDerived classesDesign detailsInstantiation rules

Hide things Why? What you hide you can change

Page 10: Encapsulation CodeCampIasi 16 oct 2010

E as hiding

Page 11: Encapsulation CodeCampIasi 16 oct 2010

The evil of the global variableThe lowest degree of encapsulation

Foo.field same as Global.fieldIgnores all the efforts of C#/Java creators

Tight coupling

Page 12: Encapsulation CodeCampIasi 16 oct 2010

The evil of the global variable

To depend on field a Foo is necessary

Coupling through field requires a reference to the same instance of Foo

Removing ‘public’ is another encapsulating action

Page 13: Encapsulation CodeCampIasi 16 oct 2010

The evil of the global variable

Page 14: Encapsulation CodeCampIasi 16 oct 2010

E of member identityBar coupled to: field’ ’s valuefield is an integerfield is an instance member of Foo

Identity coupling: A knows that B exists

Page 15: Encapsulation CodeCampIasi 16 oct 2010

E of member identity

Page 16: Encapsulation CodeCampIasi 16 oct 2010

E of member identityCreate method/property that E field ’s nature

Bar is coupled only to the fact that Prop is an integer property

Bar is decoupled from the fact that:field is an integerfield is in Foofield is stored anywhere at all

Page 17: Encapsulation CodeCampIasi 16 oct 2010

E of member identity

Page 18: Encapsulation CodeCampIasi 16 oct 2010

Self-Encapsulating membersStandard practice for accessing a field within the class itself is to refer to it directly

Page 19: Encapsulation CodeCampIasi 16 oct 2010

E of typeHiding of entire types

CalcUser contains no mention of Adder or Multiplier

Calculator E its subclasses

GOF – “design to interfaces”

Page 20: Encapsulation CodeCampIasi 16 oct 2010

E of designSomeone has to new Adder or MultiplierSomeone has to decide which one to buildClient having this responsibility

Breaks the E of type Lose modularity

Object factory

Page 21: Encapsulation CodeCampIasi 16 oct 2010

E of design

The Strategy Pattern itself is not E If design changesHide the design

Context requests Strategy from StrategyFactory instead of Client

Have a factory build the Context object handing proper Strategy

Page 22: Encapsulation CodeCampIasi 16 oct 2010

E of variationAnimalsTypes of moving

WalkingFlying

SwitchesSubclassesMore variation

HerbivoresCarnivores

Page 23: Encapsulation CodeCampIasi 16 oct 2010

E of variation

Page 24: Encapsulation CodeCampIasi 16 oct 2010

Difficulty of E reference objects**Demo**

Page 25: Encapsulation CodeCampIasi 16 oct 2010

Difficulty of E reference objectsmyFoo is not fully encapsulated

Bar ’s behavior depends on the state of Foo reference

Client can break E if it retains the Foo reference

Page 26: Encapsulation CodeCampIasi 16 oct 2010

Breaking E

Page 27: Encapsulation CodeCampIasi 16 oct 2010

Breaking ETell don’t ask

Still exposes internal structure of the component

Client has to manage explicitly

Hide how money are displayed

Page 28: Encapsulation CodeCampIasi 16 oct 2010

Issues in EWe might say:

“Encapsulate the name of the application’s log file in the PricingPolicy class.”

Recast in terms of information hiding:“Hide the name of the application’s log file in the PricingPolicy class.”

Information can be hidden in the wrong place

Page 29: Encapsulation CodeCampIasi 16 oct 2010

Values in EPromotesHigh cohesionFreedom in changing thingsClarityLow couplingTestability

Page 30: Encapsulation CodeCampIasi 16 oct 2010

ResourcesDesign Patterns Explained A New Perspective on Object-Oriented Design Design Patterns Elements of Reusable Object-Oriented SoftwareCode Complete

Page 31: Encapsulation CodeCampIasi 16 oct 2010

Please fill the evaluation form

Thank you very much

Eigel Silviu-Horea, [email protected]