Chapter Eight Chapter Eight Expanding Our Expanding Our Horizons Horizons Ku-Yaw Chang Ku-Yaw Chang [email protected][email protected]Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University
28
Embed
Chapter Eight Expanding Our Horizons Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
3Ku-Yaw ChangExpanding Our Horizons Overview Fundamental concepts Objects Objects Encapsulation Encapsulation Abstract Abstract A new way of seeing object-oriented design From the perspective that design patterns create From the perspective that design patterns create
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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?
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)?
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
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