- Slides: 22
Object Oriented Design Chapter 12
What is Object Oriented Design? Complements top-down design Data-centered view of design Reliable Cost-effective
Components of OOD Black Boxes (provide services) Clients/ Users Interfaces
Black Boxes In top-down design, black boxes are functions. In OOD, black boxes are objects. This is just a simplification of SEPARATION OF CONCERNS. Services or tasks to be performed are ENCAPSULATED in a function or object. Of course, the guts of any object is the class definition behind it. OOD centers on breaking the problem down into logical groupings and defining classes for those groupings.
Find the objects Look at nouns in problem statement Find things that can be grouped together Find things/data that might be represented with a primitive data type (numbers, strings).
Identify instance variables Once you’ve located logical groupings of data or tasks, identify the kinds of data needed to make this grouping of data/tasks work within the whole program.
Consider interfaces—methods needed Go back to your original list of objects needed (now complicated with the instance variables needed) and identify the actions which must be taken on that dtata. Circle verbs in the problem statement. All manipulation of an object’s data must be performed through methods defined in the class statement which creates the object.
Work on the complicated methods Find the most complicated methods, and work at coding them. Use top-down design and step-wise refinement to flesh out the methods. Step-wise refinement: start with high-level, abstract description and add details. Look for other classes the method may have to interact with. Look for new methods that need to be added to other classes, or new classes that are needed.
Work recursively As you work on modules, functions and classes, you will need to go back to other classes, functions, etc. , to make changes. Working on just one module means it may not work at all when you run it; it needs interaction with other modules. Add just a line or two of code, and test to see if it a) meets syntax rules and b) performs what it’s supposed to perform.
Trash it if need be Sometimes a whole module just doesn’t seem to work out. You can tell this when you’re repeating code in another class or in main() that should have been handled in one module. You can keep that code by either commenting it out or saving multiple versions of your program as you go (jehp 7 ver 04. py)
‘Tis a gift to be simple Keep asking yourself, is this instance variable needed? Does this method accomplish what it’s supposed to do? Eschew complexity. Embrace simplicity.
Encapsulation Packaging data and methods together on one object is encapsulation. We can also define encapsulation as hiding the details of an operation in an object. Encapsulation is the primary concept which distinguishes OOP because it groups data and actions intuitively in objects, mimicking real life. The implementation of an object (its definition) is independent of its use because of encapsulation.
Benefits of encapsulation Encapsulation allows us to isolate major tasks or decisions in a program by grouping data and methods in one package. Revisions to the code will be easier—change the definition (implementation) of an object but not its use. Because revisions are easier, fewer errors will be introduced Supports code re-use By itself it makes a language object-based.
Polymorphism “many forms”: allows a single line of code to implement different actions depending on the type or class of the object. The same method invocation/call could be used with different objects with different results. my. Art. Box = [my. Circ, my. Oval, my. Line] for obj in my. Art. Box: obj. draw(win) The last line is polymorphic: it invokes many different forms—circle, oval, line.
Example of polymorphism from Python class Animal: def __init__(self, name): # Constructor of the class self. name = name class Cat(Animal): def talk(self): return 'Meow!' class Dog(Animal): def talk(self): return 'Woof!' animals = [Cat('Missy'), Cat('Mr. Bojangles'), Dog('Lassie')] for animal in animals: print animal. name + ': ' + animal. talk() # prints the following: # Missy: Meow! # Mr. Bojangles: Meow! # Lassie: Woof!
Advantages of polymorphism Flexibility. An action similar for different objects can be written once but executed correctly and differently depending on the object(s) on which it is executed. Simplicity of code. Reduces the necessity of writing multiple functions or methods to achieve processes.
Inheritance Inheritance is the idea that a subclass, created from a superclass, inherits behavior from its superclass. Or, in other words, if a new class is created as a smaller specialization of a larger class, the new class will borrow attributes from the existing class. In the example preceding, Cat and Dog are subclasses of the superclass Animal. We can also think of subclasses as children classes and the superclass as a parent class.
Multiple Inheritance In Python, a subclass can actually inherit from more than one superclass. In the animals— dogs—cats example, Dog and Cat classes might also inherit attributes from another superclass called Pets. If Pets included a Boolean instance variable for ‘groomed’ (True or False) then subclasses Dog and Cat would inherit that data field as well as everything in the Animal superclass.
Advantages of inheritance Classes can be structured to avoid duplicating methods. This reduces the introduction of new error. Inheritance promotes code re-use.
Workshop In groups of two or three, locate data groupings needed for Programming Exercise # 3 or 4, depending on which you have been assigned. List actions/methods specified. Using pseudocode, plan modules that will perform the needed tasks and make use of the objects you identified.
Last workshop Use the OOD principles you used in the previous workshop to block out objects and actions for our final project.