Chapter Eight Expanding Our Horizons - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Chapter Eight Expanding Our Horizons

Description:

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

Number of Views:374
Avg rating:3.0/5.0
Slides: 29
Provided by: KuY2
Category:

less

Transcript and Presenter's Notes

Title: Chapter Eight Expanding Our Horizons


1
Chapter EightExpanding Our Horizons
  • Ku-Yaw Chang
  • canseco_at_mail.dyu.edu.tw
  • 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

28
The End
Write a Comment
User Comments (0)
About PowerShow.com