Chapter EightChapter Eight
Expanding Our HorizonsExpanding Our HorizonsKu-Yaw ChangKu-Yaw Chang
[email protected]@mail.dyu.edu.twAssistant Professor, Department of Assistant Professor, Department of
Computer Science and Information EngineeringComputer Science and Information EngineeringDa-Yeh UniversityDa-Yeh University
22Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
33Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OverviewOverview
Fundamental conceptsFundamental concepts ObjectsObjects EncapsulationEncapsulation AbstractAbstract
A new way of seeing object-oriented designA new way of seeing object-oriented design From the perspective that design patterns createFrom the perspective that design patterns create
44Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
55Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
ObjectsObjects
The traditional viewThe traditional view Data with methodsData with methods
Smart dataSmart dataFrom implementation perspectiveFrom implementation perspective
The new viewThe new view Things with responsibilitiesThings with responsibilities
From conceptual perspectiveFrom conceptual perspectiveFocus on what the objects are supposed to doFocus on what the objects are supposed to do
Make a preliminary design without worrying about all of the Make a preliminary design without worrying about all of the details involveddetails involved
Implement the designImplement the design
66Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
ObjectsObjects
Things with responsibilitiesThings with responsibilities Easier to think in terms of responsibilitiesEasier to think in terms of responsibilities Help to define the object’s public interfaceHelp to define the object’s public interface
For exampleFor example A Shape object’s responsibilitiesA Shape object’s responsibilities
To know where it is locatedTo know where it is located getLocation( … )getLocation( … ) drawShape( … )drawShape( … ) unDrawShape( … )unDrawShape( … )
To be able to draw itself on a displayTo be able to draw itself on a displayTo be able to remove itself from a displayTo be able to remove itself from a display
77Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
ObjectsObjects
Focus on motivation rather than on Focus on motivation rather than on implementation is a recurring theme in design implementation is a recurring theme in design patterns.patterns. Basic viewpoint for objectsBasic viewpoint for objects Superior designsSuperior designs
88Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
99Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
EncapsulationEncapsulation
The traditional viewThe traditional view Data hidingData hiding
Too limitedToo limited Umbrella and carUmbrella and car
The new viewThe new view Any kind of hidingAny kind of hiding
1010Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
EncapsulationEncapsulation
Multiple levels of encapsulationMultiple levels of encapsulation Encapsulation of Encapsulation of datadata Encapsulation of Encapsulation of methodsmethods Encapsulation of Encapsulation of subclassessubclasses Encapsulation of Encapsulation of other objectsother objects
1111Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
InheritanceInheritance
In object-oriented paradigmIn object-oriented paradigm Reuse of classes was one of its big benefitsReuse of classes was one of its big benefits
Generalized classGeneralized classSpecialized classSpecialized class
InheritanceInheritance as a concept versus for reuseas a concept versus for reuse
1212Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
ProblemsProblems
Can cause weak cohesionCan cause weak cohesion Different types of borders or other thingsDifferent types of borders or other things Pentagon aren’t just concerned about pentagons Pentagon aren’t just concerned about pentagons
anymoreanymore
Reduces possibility of reuseReduces possibility of reuse Code for different bordersCode for different borders
Does not scale well with variationDoes not scale well with variation Need to specialize the pentagon class repeatedlyNeed to specialize the pentagon class repeatedly
1313Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
1414Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Find What Is Varying and Find What Is Varying and Encapsulate ItEncapsulate It
GoF suggests the followingGoF suggests the following Consider what should be variable in your design.Consider what should be variable in your design. This This
approach is the opposite of focusing on the cause of approach is the opposite of focusing on the cause of redesign. Instead of considering what might force a redesign. Instead of considering what might force a change to a design, consider what you want to be change to a design, consider what you want to be able to change without redesign. The focus here is on able to change without redesign. The focus here is on encapsulating the concept that variesencapsulating the concept that varies, a theme of , a theme of many design patterns.many design patterns.
1515Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Find What Is Varying and Find What Is Varying and Encapsulate ItEncapsulate It
Use encapsulation to create layers between Use encapsulation to create layers between objectsobjects Change things on different sides of the layers without Change things on different sides of the layers without
affecting the other sideaffecting the other side Loose-coupling between the sidesLoose-coupling between the sides
1616Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Variation in Data VersusVariation in Data Versusin Behaviorin Behavior
RequirementsRequirements Each type of animal can have Each type of animal can have a different number of a different number of
legslegs Animal objects must be able to remember and Animal objects must be able to remember and
retrieve this informationretrieve this information Each type of animal can have Each type of animal can have a different type of a different type of
movementmovementWalking and flyingWalking and flying
Animal objects must be able to return how long it will Animal objects must be able to return how long it will take to move from one place to anothertake to move from one place to another
Given a specified type of terrainGiven a specified type of terrain
1717Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Variation in Data VersusVariation in Data Versusin Behaviorin Behavior
SolutionsSolutions A data memberA data member A choice of approachA choice of approach
Having a data memberHaving a data memberHaving two different types of Animals (both derived Having two different types of Animals (both derived from the base Animal class)from the base Animal class)
One for walkingOne for walking One for flyingOne for flying
1818Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Variation in Data VersusVariation in Data Versusin Behaviorin Behavior
ProblemsProblems Too many detailsToo many details
A plethora of classesA plethora of classes Eagles : carnivores that flyEagles : carnivores that fly Lions : carnivores that walkLions : carnivores that walk Sparrows : vegetarians that flySparrows : vegetarians that fly Cows: vegetarians that walkCows: vegetarians that walk
Cannot handle Animals that can both walk and flyCannot handle Animals that can both walk and fly Weakly cohesiveWeakly cohesive
1919Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Variation in Data VersusVariation in Data Versusin Behaviorin Behavior
Another possibility existsAnother possibility exists Have the Animal class contain an object that has the appropriate Have the Animal class contain an object that has the appropriate
movement behaviormovement behavior
2020Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Comparing the twoComparing the two
Is one object containing another object Is one object containing another object inherently different from an object having a mere inherently different from an object having a mere data member ?data member ? Are data members objects?Are data members objects?
In object-oriented programming, In object-oriented programming, everythingeverything is an is an object.object. Using objects to contain variation in attributesUsing objects to contain variation in attributes Using objects to contain variation in behaviorUsing objects to contain variation in behavior
2121Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
2222Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Commonality and Variability Commonality and Variability AnalysisAnalysis
Commonality analysisCommonality analysis Identify Identify wherewhere things vary things vary
Search for common elementsSearch for common elements
Variability analysisVariability analysis Identify Identify howhow they vary they vary
2323Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Commonality/Variability and Commonality/Variability and Abstract ClassesAbstract Classes
2424Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
Two-Step ProcedureTwo-Step Procedure
When DefiningWhen Defining You Must Ask yourselfYou Must Ask yourself
An abstract classAn abstract class(commonality)(commonality)
What interface is needed to handle all What interface is needed to handle all of the responsibilities of this class?of the responsibilities of this class?
Derived classesDerived classesGiven this particular implementation Given this particular implementation (this variation), how can I implement it (this variation), how can I implement it with the given specification?with the given specification?
2525Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
RelationshipsRelationships
The relationship between the specification The relationship between the specification perspective and the conceptual perspectiveperspective and the conceptual perspective It identifies the interface I need to use to handle all of It identifies the interface I need to use to handle all of
the cases of the concept (that is, the commonality)the cases of the concept (that is, the commonality)
The relationship between the specification The relationship between the specification perspective and the implementation perspectiveperspective and the implementation perspective Given this specification, how can I implement this Given this specification, how can I implement this
particular case (this variation)?particular case (this variation)?
2626Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
OutlineOutlineOverviewOverviewObjects: the Traditional View and the New ViewObjects: the Traditional View and the New ViewEncapsulation: the Traditional View and the New Encapsulation: the Traditional View and the New ViewViewFind What is Varying and Encapsulate ItFind What is Varying and Encapsulate ItCommonality/Variability and Abstract ClassesCommonality/Variability and Abstract ClassesSummarySummary
2727Ku-Yaw ChangKu-Yaw Chang Expanding Our HorizonsExpanding Our Horizons
SummarySummary
EncapsulationEncapsulation Any kind of hidingAny kind of hiding
More than simply hiding dataMore than simply hiding data
InheritanceInheritance A method of consistently dealing with different A method of consistently dealing with different
concrete classes that are conceptually the sameconcrete classes that are conceptually the sameNot a means of specializationNot a means of specialization
Using objects to hold variations in behaviorUsing objects to hold variations in behavior Data members to hold variations in dataData members to hold variations in dataCommonality/variability analysisCommonality/variability analysis More effectively than looking for nouns and actionsMore effectively than looking for nouns and actions
The EndThe End