1 / 22

Object-Oriented Design: Fundamentals and Benefits

Object-Oriented Design (OOD) is a reliable and cost-effective approach that complements top-down design by organizing code into logical groupings known as objects. This chapter explores the essential components of OOD and provides a step-by-step guide to identifying objects, defining classes, and working with instance variables. Embracing encapsulation, polymorphism, and inheritance, OOD offers numerous benefits, such as code reusability, simplicity, flexibility, and easier code revisions.

kbarry
Download Presentation

Object-Oriented Design: Fundamentals and Benefits

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12 Object Oriented Design

  2. What is Object Oriented Design? • Complements top-down design • Data-centered view of design • Reliable • Cost-effective

  3. Components of OOD Clients/ Users Black Boxes (provide services) Interfaces

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

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

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

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

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

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

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

  11. ‘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.

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

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

  14. 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. • 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. 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! 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. 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.

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

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

  19. Advantages of inheritance • Classes can be structured to avoid duplicating methods. • This reduces the introduction of new error. • Inheritance promotes code re-use.

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

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

More Related