Encapsulation
-
Upload
eigelsi -
Category
Technology
-
view
528 -
download
2
description
Transcript of Encapsulation
![Page 1: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/1.jpg)
Encapsulationthe good, the bad, the uglyEigel Silviu-Horea, [email protected]
www.endava.com
16 Octombrie 2010
![Page 2: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/2.jpg)
AgendaFrom functional decomposition to OOPEncapsulationFrom bad and ugly to “The Good”
Building up encapsulationHandling variation
Conclusions
![Page 3: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/3.jpg)
Some valuesCohesion
How closely the operations in a module are related
Coupling The strength of a connection between two modules
![Page 4: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/4.jpg)
Some problemsRequirements Always ChangeChange is necessaryNew variation to an existing theme
![Page 5: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/5.jpg)
Back in the daysProfessor and students Functional decompositionLasagna
![Page 6: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/6.jpg)
OOPObjects as data with methodsClasses are things with responsibilities
AbstractionEncapsulationInheritancePolymorphism
![Page 7: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/7.jpg)
The bad in its “definition”
Usually defined as data hiding
![Page 8: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/8.jpg)
The ugliness is in our usageStill functionally decomposingCaused by it’s natural roots
Data hiding viewData - functionality separation
![Page 9: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/10.jpg)
E as hiding
![Page 11: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/13.jpg)
The evil of the global variable
![Page 14: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/15.jpg)
E of member identity
![Page 16: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/17.jpg)
E of member identity
![Page 18: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/18.jpg)
Self-Encapsulating membersStandard practice for accessing a field within the class itself is to refer to it directly
![Page 19: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/22.jpg)
E of variationAnimalsTypes of moving
WalkingFlying
SwitchesSubclassesMore variation
HerbivoresCarnivores
![Page 23: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/23.jpg)
E of variation
![Page 24: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/24.jpg)
Difficulty of E reference objects**Demo**
![Page 25: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/26.jpg)
Breaking E
![Page 27: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/29.jpg)
Values in EPromotesHigh cohesionFreedom in changing thingsClarityLow couplingTestability
![Page 30: Encapsulation](https://reader033.fdocuments.us/reader033/viewer/2022061120/54673114af7959ba5e8b6977/html5/thumbnails/30.jpg)
ResourcesDesign Patterns Explained A New Perspective on Object-Oriented Design Design Patterns Elements of Reusable Object-Oriented SoftwareCode Complete