© Kenneth M. Anderson, 2011EXPANDING OUR HORIZONSCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 8 — 02/03/20111Sunday, February 6, 2011© Kenneth M. Anderson, 2011Goals of the LectureCover the material in Chapter 8 of our textbookNew perspective on objects and encapsulationHow to handle variation in behaviorNew perspective on inheritanceCommonality and Variability AnalysisRelationship between Design Patterns and Agile2Sunday, February 6, 2011© Kenneth M. Anderson, 2011Tr aditional View of Objects“Data with Methods” or “Smart Data”Based on the mechanics of OO languagesIn C, you have structs (data) and then you have functions that operate on the structs (methods)In C++, you could combine the two into a single unit… hence “data with methods”But this view limits your ability to design with objectsThe focus is mainly on the data not the behavior!3Sunday, February 6, 2011© Kenneth M. Anderson, 2011Example4public class Pixel {12 private double red;3 private double green;4 private double blue;5 private double alpha;67 public Pixel(double red, double green, double blue, double alpha) {8 this.red = red;9 this.green = green;10 this.blue = blue;11 this.alpha = alpha;12 }1314 public double getRed() {15 return red;16 }1718 public void setRed(double red) {19 this.red = red;20 }2122 ...23}2425“Dumb Data Holder”This is a class that exists solely to help some other class. It is the worst form of “data with methods”Part of the problem is this “concept” is too low level to be useful.Sunday, February 6, 2011© Kenneth M. Anderson, 2011New Perspective on Objects (I)Objects are “Things with Responsibilities”Don’t focus on the data; it is subject to change as the implementation evolves to meet non-functional constraintsthis is why we set attributes as private by default focus on behaviorAnd how those behaviors allow you to fulfill responsibilities that the system must meet5Sunday, February 6, 2011© Kenneth M. Anderson, 2011New Perspective on Objects (II)The responsibilities come from the requirementsIf you have a requirement to create profiles for your users then somewhere in your design, you havean object with the responsibility of creating profiles and managing the workflow related to that taskan object with the responsibility of storing and manipulating the data of a profilean object with the responsibility of storing and manipulating multiple profiles 6Sunday, February 6, 2011© Kenneth M. Anderson, 2011New Perspective on Objects (III)Responsibilities help you designRequirements lead to responsibilitiesAnd responsibilities need to “go somewhere”The process of analysis becomesfinding all of the responsibilities of the systemThe process of design becomesfinding a home for each responsibility (object/subsystem)7Sunday, February 6, 2011© Kenneth M. Anderson, 2011New Perspective on Objects (IV)A focus on responsibilities also promotes a focus on defining the public interface of an objectWhat methods will I need to meet my responsibilities?How will I be used?This focus early in design matches the external perspective we need to maintainsee the system from the user’s point of viewA rush to implementation obscures that perspective8Sunday, February 6, 2011© Kenneth M. Anderson, 2011Example, continuedPixel was too low level to be usefulbut a collection of pixels… an imageNow you’re talkingWith an image class you can specify useful servicesstretch, flip, distort, change to black and white, add a shadow, produce a mirror image effect, move, display yourself on this canvas, …9Sunday, February 6, 2011© Kenneth M. Anderson, 201110Example, continued.Here’s the public interface of the UIImage class in Apple’s Cocoa touch libraryNote that they refer to the public interface as “Tasks”A “+” in front of a method name indicates a static methodA “-” indicates an instance methodThis class is designed to be used with UIImageView to be displayed and CoreAnimation to be manipulated/animatedSunday, February 6, 2011© Kenneth M. Anderson, 2011Tr aditional View on EncapsulationEncapsulation means “hiding data”This view is too limited and again focuses on the data when we want to focus on behavior and responsibilitiesThe Umbrella ExampleIn the analogy, the car plays the role of “encapsulation”Thinking of a car as an “umbrella” is too limiting; it can do so much more! The same is true of encapsulation11Sunday, February 6, 2011© Kenneth M. Anderson, 2011New Perspective on Encapsulation (I)Encapsulation should be thought of as “any kind of hiding” especially the hiding of “things that can change”We certainly can hide data but alsobehavior, implementations, design details, etc.and the mechanisms can involve more than just attribute and method visibility annotationsdesign patterns, subsystem boundaries, interfacesfor example, Objective-C’s class clusters12Sunday, February 6, 2011© Kenneth M. Anderson, 2011Multiple Types of Encapsulation (I)13setLocation(Point)ShapePoint LInesetLocation(Point)CirclesetX(int)setY(int)OtherCircleSunday, February 6, 2011© Kenneth M. Anderson, 2011Multiple Types of Encapsulation (II)14setLocation(Point)ShapePoint LInesetLocation(Point)CirclesetX(int)setY(int)OtherCircleEncapsulation of Data within each classSunday, February 6, 2011© Kenneth M. Anderson, 2011Multiple Types of Encapsulation (III)15setLocation(Point)ShapePoint LInesetLocation(Point)CirclesetX(int)setY(int)OtherCircleEncapsulation of methods; e.g. setLocation() in CircleSunday, February 6, 2011© Kenneth M. Anderson, 2011Multiple Types of Encapsulation (IV)16setLocation(Point)ShapePoint LInesetLocation(Point)CirclesetX(int)setY(int)OtherCircleEncapsulation of objects; only Circle knows about OtherCircleSunday, February 6, 2011© Kenneth M. Anderson, 2011Multiple Types of Encapsulation (V)17setLocation(Point)ShapePoint LInesetLocation(Point)CirclesetX(int)setY(int)OtherCircleEncapsulation of type; clients of Shape do not have to know about points, lines and circlesSunday, February 6, 2011© Kenneth M. Anderson, 2011Encapsulation of Type18Encapsulation of Type occurswhen there is an abstract class with derivations (subclasses) or an interface with implementationsANDthe abstract class or interface is used polymorphicallyWhen you encounter the term “encapsulation” in design patterns, this is typically what they are referring toThese abstract types provide the means for decomposing designs around the major services the system
View Full Document