Courseiva
Question 141 of 169
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

A team is developing a data processing pipeline where each step is a class that implements a common interface. They have defined an abstract base class DataProcessor with an abstract method process(data). Several concrete subclasses implement process. Now they need to add a new step that logs the data before processing. They want to reuse the existing processing logic without modifying the original classes. Which design pattern should they apply?

⚠ Common exam trap

Python Institute often tests the Decorator pattern in scenarios where the requirement is to add responsibilities to objects dynamically without altering their structure, and the trap is that candidates confuse it with the Factory pattern because both involve creating objects, but the Decorator focuses on extending behavior, not on instantiation logic.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Decorator pattern by creating a LoggingProcessor subclass that wraps another processor and calls its process method after logging.

The Decorator pattern allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. By creating a LoggingProcessor that wraps an existing DataProcessor and delegates to its process method after logging, the team reuses the original processing logic without modifying the existing classes, adhering to the Open/Closed Principle.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Factory pattern to instantiate processors dynamically.

    Why it's wrong here

    The Factory pattern is a creational pattern whose purpose is to encapsulate object instantiation, typically selecting a concrete class based on runtime conditions. While it could produce decorated processors if explicitly coded, the pattern itself provides no mechanism for adding logging behavior to an existing processor's process method. Using a factory here would still require altering processor classes or introducing a separate wrapper, so it does not solve the stated problem of adding logging dynamically.

  • Decorator pattern by creating a LoggingProcessor subclass that wraps another processor and calls its process method after logging.

    Why this is correct

    The Decorator pattern is correct because it lets you attach new responsibilities to an object without modifying its class. A LoggingProcessor subclass that holds a reference to another processor and invokes its process method after emitting log output is the canonical decorator implementation, preserving the processor interface while transparently adding logging. This wrapper can be applied to any processor instance at runtime and can even be stacked with other decorators.

  • Singleton pattern to ensure only one logger exists.

    Why it's wrong here

    The Singleton pattern concentrates on ensuring a logger class has at most one instance, which only addresses resource sharing and global access. It does not extend the behavior of the processor—nothing about being a singleton injects logging calls into process(). To get logging via a singleton, you would still need to modify every processor's method or wrap it separately, so the pattern is orthogonal to the requirement of enhancing processing behavior.

  • Observer pattern to notify loggers of data changes.

    Why it's wrong here

    The Observer pattern establishes a one-to-many dependency in which a subject notifies observers when its state changes, but it assumes processors are designed as observable subjects with registration and notification infrastructure. Adding a logger as an observer would require invasive changes to processor classes to maintain observer lists and broadcast events, which is fundamentally different from wrapping a single method. Here the goal is to add logging around process(), not to propagate data-change events to external listeners.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.