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.

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

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

Worked example

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?

  1. 1
    Collect the data

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

  2. 2
    Calculate

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

  3. 3
    Display

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

  4. 4
    Update

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

Result

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.

Student objectOne cohesive unitNameAttributeCalculate averageMethodIDAttributePrint informationMethodGradesAttributeUpdate gradeMethodcontainscontainscontainsprovidesprovidesprovides
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

A practical comparison of the two organizational approaches

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.

Key point

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

  • Common mistake

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

    What it looks like

    Several independent functions still receive a student's name, ID, and grades as separate arguments, but the design is described as object-oriented.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming procedure-oriented programming cannot be reusable.

    What it looks like

    A learner thinks only objects can provide reusable behavior.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming every Python program should use only one paradigm.

    What it looks like

    A learner refuses to use a function because the rest of the program contains objects.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Confusing attributes with methods.

    What it looks like

    Calling a student's grades an operation or calling calculate average stored data.

    Why it fails

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

    Do this instead

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

Reasoning through the choice

Compare the two program descriptions in the practice prompt.

  1. 1
    First program

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

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

  3. 3
    Python decision

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

Result

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

At a glance

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

Related concepts

More concepts starting with D