Concepts / Design Patterns in Object-Oriented Programming

Design Patterns in Object-Oriented Programming

Procedure-oriented programming organizes code around reusable functions; object-oriented programming organizes code around objects that combine data and functionality.

  • Programming

Two Ways to Organize a Program

A program can be organized around the actions it performs or around the things that exist in the problem it models. Procedure-oriented programming emphasizes reusable functions and the flow of data between them. Object-oriented programming emphasizes objects that combine the data they care about with the functions that operate on that data.

processcontainscontainsReusable functionsActionsDataPassed between functionsObjectsThings and interactionsAttributesObject dataMethodsObject functions
How does the same program organize its data and functionality when structured around reusable functions versus objects?

The central question is where the program's organization begins: with what actions need to happen, or with what entities and interactions need to be represented.

Tracing Data Through Procedures

A student's information in a procedure-oriented design

How might a program work with a student's name, ID, and grades when its organization is based on reusable functions?

Collect the data: The student's name, ID, and grades exist as data that will be supplied to functions.

Calculate: A function such as calculate_average(grades) takes the grades as input and returns a result.

Display: A separate function such as print_student_info(name, id, grades) receives the relevant data and displays information.

Update: Another function such as update_grade(grades, new_grade) receives the grades and a new grade, then transforms the grade data.

The functions are independent and reusable, while the student's data remains separate and is passed between function calls.

In this organization, the focus is on what actions the program performs. Each function takes data as input, transforms it, and returns a result. This can make individual functions reusable, but the student's information is spread across the arguments used in different function calls.

Bundling Data with Behavior

Object-oriented programming changes the unit of organization. Instead of treating a student's information as separate data passed among independent functions, the program can represent a student as an object. That object bundles the student's data, called attributes, with the functions that operate on that data, called methods.

containscontainscontainsprovidesprovidesprovidesStudent objectOne cohesive unitNameAttributeCalculate averageMethodIDAttributePrint informationMethodGradesAttributeUpdate gradeMethod
What data does an object contain, which functions operate on that data, and how are those elements connected?

The important change is not merely giving data a new name. The object becomes the unit that holds related information and the operations associated with it. This makes objects useful for modeling real-world entities and for creating multiple similar things that can interact with one another.

Choosing an Object-Based Design

Object-oriented design is especially useful when the program needs to represent entities, keep the data associated with each entity together, and describe how those entities interact. For example, a program that works with multiple similar students can model each student as an object, with each object carrying its own information and related methods.

A procedure-oriented design may be a natural fit when the main idea is a sequence or collection of reusable actions that receive data, transform it, and return results. The source of the design decision is the problem itself: Python allows either approach, as well as a mixture of both.

QuestionProcedure-oriented organizationObject-oriented organization
What is the main unit?A reusable procedure or functionAn object
Where is the data?Separate from the functions and passed between themBundled inside the object as attributes
Where is functionality?In functions that process supplied dataIn methods that operate on the object's data
What is the focus?What actions the program performsWhat things exist and how they interact
What does Python allow?Programs built with pure functionsPrograms built with objects, or a mixture of both

Python Supports Both Paradigms

Python fully supports procedure-oriented programming and object-oriented programming. You can write pure functions and organize an entire program around them. You can also organize a program around objects whose attributes and methods form a cohesive unit. Python also allows these approaches to be mixed according to the needs of the problem.

Learning object-oriented programming does not require abandoning functions. In Python, the design choice is whether a responsibility is clearer as an independent function, as a method associated with an object, or as part of a combination of both.

Mistakes in Paradigm Selection

  • Treating object-oriented programming as simply a collection of functions with different names.

    The defining organization has not changed: the data is still separate from the functions that process it.

    Fix: Look for the connection between an object, its attributes, and the methods that operate on those attributes.

  • Assuming procedure-oriented programming cannot be reusable.

    Procedure-oriented programming is explicitly organized around reusable procedures or functions.

    Fix: Recognize that reusable functions are central to procedure-oriented programming.

  • Assuming every Python program should use only one paradigm.

    Python supports both paradigms and allows them to be mixed based on the problem's needs.

    Fix: Choose or combine functions and objects according to how clearly they organize the problem.

  • Confusing attributes with methods.

    Attributes are data, while methods are functions that operate on the object's data.

    Fix: Classify information such as a name, ID, or grades as attributes and actions such as updating or calculating as methods.

Practice the Design Choice

MEDIUM

A program receives a collection of grades, calculates an average, transforms the grades, and returns results. A second program must represent several students, retain each student's name, ID, and grades, and let the students' information be processed as separate entities. For each program, decide whether a procedure-oriented design, an object-oriented design, or a mixture would be a reasonable starting point. Explain what the main unit of organization would be.

Hints
  • Ask whether the main focus is on reusable actions and data flowing between them or on entities and their interactions.
  • For an object-oriented design, identify possible attributes and methods.
  • Remember that Python permits either approach or a mixture.

Reasoning through the choice

Compare the two program descriptions in the practice prompt.

First program: The description emphasizes functions that receive grades, transform them, and return results. A procedure-oriented organization is a reasonable starting point.

Second program: The description emphasizes several student entities, each with its own name, ID, and grades. An object-oriented organization can keep each student's attributes and related methods together.

Python decision: Because Python supports both paradigms, a mixed design could also be considered if some responsibilities are clearer as independent functions.

The appropriate organization follows the program's focus: actions and data flow suggest procedures, while entities with associated data and behavior suggest objects.

Key Takeaways

  1. Procedure-oriented programming organizes a program around reusable functions, with data passed between those functions.
  2. Object-oriented programming organizes a program around objects that combine attributes and methods.
  3. Attributes represent the data an object cares about, while methods are functions that operate on that data.
  4. Objects are useful for modeling real-world entities and creating multiple similar things that interact.
  5. Python supports procedure-oriented programming, object-oriented programming, and mixtures of both.

Key Takeaways

  • Procedure-oriented programming focuses on actions performed by reusable functions.
  • Object-oriented programming focuses on entities represented by objects.
  • Objects bundle attributes and methods into cohesive units.
  • Object-oriented design is useful when a program models entities, multiple similar things, and their interactions.
  • Python supports both paradigms and allows them to be combined.