250 likes | 261 Views
Explore the traditional and new views of objects and encapsulation in object-oriented design. Learn to find and encapsulate varying aspects, utilize abstract classes, and understand commonality/variability. Discover the importance of responsibilities in object design and how to focus on motivation over implementation in design patterns.
E N D
Chapter EightExpanding Our Horizons Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
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 Expanding Our Horizons
Overview • Fundamental concepts • Objects • Encapsulation • Abstract • A new way of seeing object-oriented design • From the perspective that design patterns create Expanding 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 Expanding Our Horizons
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 achieved. Expanding Our Horizons
Objects • Things with responsibilities • Easier to think in terms of responsibilities • Help to define the object’s public interface • For example • A Shape object’s 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 Expanding Our Horizons
Objects • Focus on motivation rather than on implementation is a recurring theme in design patterns. • Basic viewpoint for objects • Superior designs Expanding 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 Expanding Our Horizons
The traditional view Data hiding Too limited Umbrella and car The new view Any kind of hiding Encapsulation Expanding Our Horizons
Encapsulation • Multiple levels of encapsulation • Encapsulation of data • Encapsulation of methods • Encapsulation of subclasses • Encapsulation of other objects Expanding Our Horizons
Inheritance • In object-oriented paradigm • Reuse of classes was one of its big benefits • Generalized class • Specialized class • As a concept versus for reuse Expanding 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 Expanding 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. Expanding 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 • Loose-coupling between the sides Expanding Our Horizons
Variation in Data Versusin Behavior • Requirements • Each type of animal have a different number of legs • Each type of animal have a different type of movement • Walking and flying • Solutions • A data member • A choice of approach • Having a data member • Having two different types of Animals Expanding Our Horizons
Variation in Data Versusin Behavior • Problems • Tightly coupling • Too many details • Manage the subtype of Animal • Cannot handle Animals that can both walk and fly • A third possibility exists • Have the Animal class contain an object that has the appropriate movement behavior Expanding Our Horizons
Variation in Data Versusin Behavior Expanding 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? • In object-oriented programming, everything is an object. • Using objects to contain variation in attributes • Using objects to contain variation in behavior Expanding 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 Expanding Our Horizons
Commonality/Variability and Abstract Classes Expanding Our Horizons
Two-Step Procedure Expanding 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) • The relationship between the specification perspective and the implementation perspective • Given this specification, how can I implement this particular case (this variation)? Expanding 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 Expanding Our Horizons
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 Expanding Our Horizons