Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.

Similar presentations


Presentation on theme: "Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective."— Presentation transcript:

1 Chapter 12 Object Oriented Design

2  Complements top-down design  Data-centered view of design  Reliable  Cost-effective

3 Black Boxes (provide services) Interfaces Clients/ Users

4  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.

5  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).

6  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.

7  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.

8  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.

9  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.

10  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 ( jehp7ver04.py )

11  Keep asking yourself, is this instance variable needed? Does this method accomplish what it’s supposed to do?  Eschew complexity. Embrace simplicity.

12  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.

13  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.

14  “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.  myArtBox = [myCirc, myOval, myLine] for obj in myArtBox: obj.draw(win)  The last line is polymorphic: it invokes many different forms—circle, oval, line.

15  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! 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! Woof!

16  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.

17  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.

18  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.

19

20  Classes can be structured to avoid duplicating methods.  This reduces the introduction of new error.  Inheritance promotes code re-use.

21  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.

22  Use the OOD principles you used in the previous workshop to block out objects and actions for our final project.


Download ppt "Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective."

Similar presentations


Ads by Google