Chapter Eight Expanding Our Horizons Ku-Yaw Chang canseco_at_mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering – PowerPoint PPT presentation
Assistant Professor, Department of Computer Science and Information Engineering
Da-Yeh University
2 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
3 Overview
Fundamental concepts
Objects
Encapsulation
Abstract
A new way of seeing object-oriented design
From the perspective that design patterns create
4 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
5 Objects
The traditional view
Data with methods
Smart data
From implementation perspective
The new view
Things with responsibilities
From conceptual perspective
Focus on what the objects are supposed to do
Make a preliminary design without worrying about all of the details involved
Implement the design
6 Objects
Things with responsibilities
Easier to think in terms of responsibilities
Help to define the objects public interface
For example
A Shape objects responsibilities
To know where it is located
getLocation( )
drawShape( )
unDrawShape( )
To be able to draw itself on a display
To be able to remove itself from a display
7 Objects
Focus on motivation rather than on implementation is a recurring theme in design patterns.
Basic viewpoint for objects
Superior designs
8 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
9 Encapsulation
The traditional view
Data hiding
Too limited
Umbrella and car
The new view
Any kind of hiding
10 Encapsulation
Multiple levels of encapsulation
Encapsulation of data
Encapsulation of methods
Encapsulation of subclasses
Encapsulation of other objects
11 Inheritance
In object-oriented paradigm
Reuse of classes was one of its big benefits
Generalized class
Specialized class
Inheritance
as a concept versus for reuse
12 Problems
Can cause weak cohesion
Different types of borders or other things
Pentagon arent just concerned about pentagons anymore
Reduces possibility of reuse
Code for different borders
Does not scale well with variation
Need to specialize the pentagon class repeatedly
13 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
14 Find What Is Varying and Encapsulate It
GoF suggests the following
Consider what should be variable in your design. This approach is the opposite of focusing on the cause of redesign. Instead of considering what might force a change to a design, consider what you want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns.
15 Find What Is Varying and Encapsulate It
Use encapsulation to create layers between objects
Change things on different sides of the layers without affecting the other side
Loose-coupling between the sides
16 Variation in Data Versusin Behavior
Requirements
Each type of animal can have a different number of legs
Animal objects must be able to remember and retrieve this information
Each type of animal can have a different type of movement
Walking and flying
Animal objects must be able to return how long it will take to move from one place to another
Given a specified type of terrain
17 Variation in Data Versusin Behavior
Solutions
A data member
A choice of approach
Having a data member
Having two different types of Animals (both derived from the base Animal class)
One for walking
One for flying
18 Variation in Data Versusin Behavior
Problems
Too many details
A plethora of classes
Eagles carnivores that fly
Lions carnivores that walk
Sparrows vegetarians that fly
Cows vegetarians that walk
Cannot handle Animals that can both walk and fly
Weakly cohesive
19 Variation in Data Versusin Behavior
Another possibility exists
Have the Animal class contain an object that has the appropriate movement behavior
20 Comparing the two
Is one object containing another object inherently different from an object having a mere data member ?
Are data members objects?
In object-oriented programming, everything is an object.
Using objects to contain variation in attributes
Using objects to contain variation in behavior
21 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
22 Commonality and Variability Analysis
Commonality analysis
Identify where things vary
Search for common elements
Variability analysis
Identify how they vary
23 Commonality/Variability and Abstract Classes 24 Two-Step Procedure When Defining You Must Ask yourself An abstract class(commonality) What interface is needed to handle all of the responsibilities of this class? Derived classes Given this particular implementation (this variation), how can I implement it with the given specification? 25 Relationships
The relationship between the specification perspective and the conceptual perspective
It identifies the interface I need to use to handle all of the cases of the concept (that is, the commonality)
The relationship between the specification perspective and the implementation perspective
Given this specification, how can I implement this particular case (this variation)?
26 Outline
Overview
Objects the Traditional View and the New View
Encapsulation the Traditional View and the New View
Find What is Varying and Encapsulate It
Commonality/Variability and Abstract Classes
Summary
27 Summary
Encapsulation
Any kind of hiding
More than simply hiding data
Inheritance
A method of consistently dealing with different concrete classes that are conceptually the same
Not a means of specialization
Using objects to hold variations in behavior
Data members to hold variations in data
Commonality/variability analysis
More effectively than looking for nouns and actions
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.