Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Eight Expanding Our Horizons Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.

Similar presentations


Presentation on theme: "Chapter Eight Expanding Our Horizons Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering."— Presentation transcript:

1 Chapter Eight Expanding Our Horizons Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangExpanding Our Horizons 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 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

4 4Ku-Yaw ChangExpanding Our Horizons 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 5Ku-Yaw ChangExpanding Our Horizons Objects The traditional view Data with methods Data with methods Smart data From implementation perspective The new view Things with responsibilities 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 Make a preliminary design without worrying about all of the details involved Implement the design Implement the design

6 6Ku-Yaw ChangExpanding Our Horizons Objects Things with responsibilities Easier to think in terms of responsibilities Easier to think in terms of responsibilities Help to define the object’s public interface Help to define the object’s public interface For example A Shape object’s responsibilities A Shape object’s responsibilities To know where it is located getLocation( … ) getLocation( … ) drawShape( … ) drawShape( … ) unDrawShape( … ) unDrawShape( … ) To be able to draw itself on a display To be able to remove itself from a display

7 7Ku-Yaw ChangExpanding Our Horizons Objects Focus on motivation rather than on implementation is a recurring theme in design patterns. Basic viewpoint for objects Basic viewpoint for objects Superior designs Superior designs

8 8Ku-Yaw ChangExpanding Our Horizons 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 9Ku-Yaw ChangExpanding Our Horizons Encapsulation The traditional view Data hiding Data hiding Too limited Umbrella and car Umbrella and car The new view Any kind of hiding

10 10Ku-Yaw ChangExpanding Our Horizons Encapsulation Multiple levels of encapsulation Encapsulation of data Encapsulation of data Encapsulation of methods Encapsulation of methods Encapsulation of subclasses Encapsulation of subclasses Encapsulation of other objects Encapsulation of other objects

11 11Ku-Yaw ChangExpanding Our Horizons Inheritance In object-oriented paradigm Reuse of classes was one of its big benefits Reuse of classes was one of its big benefits Generalized class Specialized class Inheritance as a concept versus for reuse as a concept versus for reuse

12 12Ku-Yaw ChangExpanding Our Horizons Problems Can cause weak cohesion Different types of borders or other things Different types of borders or other things Pentagon aren’t just concerned about pentagons anymore Pentagon aren’t just concerned about pentagons anymore Reduces possibility of reuse Code for different borders Code for different borders Does not scale well with variation Need to specialize the pentagon class repeatedly Need to specialize the pentagon class repeatedly

13 13Ku-Yaw ChangExpanding Our Horizons 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 14Ku-Yaw ChangExpanding Our Horizons 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. 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 15Ku-Yaw ChangExpanding Our Horizons 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 Change things on different sides of the layers without affecting the other side Loose-coupling between the sides Loose-coupling between the sides

16 16Ku-Yaw ChangExpanding Our Horizons Variation in Data Versus in Behavior Requirements Each type of animal can have a different number of legs Each type of animal can have a different number of legs Animal objects must be able to remember and retrieve this information Animal objects must be able to remember and retrieve this information Each type of animal can have a different type of movement 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 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 17Ku-Yaw ChangExpanding Our Horizons Variation in Data Versus in Behavior Solutions A data member A data member A choice of approach 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 walking One for flying One for flying

18 18Ku-Yaw ChangExpanding Our Horizons Variation in Data Versus in Behavior Problems Too many details Too many details A plethora of classes Eagles : carnivores that fly Eagles : carnivores that fly Lions : carnivores that walk Lions : carnivores that walk Sparrows : vegetarians that fly Sparrows : vegetarians that fly Cows: vegetarians that walk Cows: vegetarians that walk Cannot handle Animals that can both walk and fly Weakly cohesive Weakly cohesive

19 19Ku-Yaw ChangExpanding Our Horizons Variation in Data Versus in Behavior Another possibility exists Have the Animal class contain an object that has the appropriate movement behavior Have the Animal class contain an object that has the appropriate movement behavior

20 20Ku-Yaw ChangExpanding Our Horizons Comparing the two Is one object containing another object inherently different from an object having a mere data member ? Are data members objects? 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 attributes Using objects to contain variation in behavior Using objects to contain variation in behavior

21 21Ku-Yaw ChangExpanding Our Horizons 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 22Ku-Yaw ChangExpanding Our Horizons Commonality and Variability Analysis Commonality analysis Identify where things vary Identify where things vary Search for common elements Variability analysis Identify how they vary Identify how they vary

23 23Ku-Yaw ChangExpanding Our Horizons Commonality/Variability and Abstract Classes

24 24Ku-Yaw ChangExpanding Our Horizons 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 25Ku-Yaw ChangExpanding Our Horizons 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) 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)? Given this specification, how can I implement this particular case (this variation)?

26 26Ku-Yaw ChangExpanding Our Horizons 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 27Ku-Yaw ChangExpanding Our Horizons Summary Encapsulation Any kind of hiding Any kind of hiding More than simply hiding data Inheritance A method of consistently dealing with different concrete classes that are conceptually the same 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 Data members to hold variations in data Commonality/variability analysis More effectively than looking for nouns and actions More effectively than looking for nouns and actions

28 The End


Download ppt "Chapter Eight Expanding Our Horizons Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering."

Similar presentations


Ads by Google