askvity

What is a Wrapper Object Structural Pattern?

Published in Structural Design Pattern 3 mins read

A wrapper object in a structural design pattern is fundamentally about encapsulating an existing object within a new object to provide a different interface or enhanced behavior without altering the original object's class. This is a core concept used in several structural patterns.

Understanding the Wrapper Concept

At its heart, a wrapper class "wraps" an instance of another class. Think of it like putting a gift inside wrapping paper and adding a ribbon – the gift (the original object) remains unchanged, but its presentation (interface) or perhaps its purpose in the context of giving (behavior) is modified or extended by the wrapping (the wrapper object).

The key point, as highlighted in the reference, is that the wrapper implements a design pattern that holds an instance of an object and presents its own interface or behavior to that object, crucially without changing the original class.

This concept often demonstrates delegation, where the wrapper object intercepts requests made to it and forwards them to the wrapped object, possibly adding actions before or after the forwarding.

How Wrapper Objects Function in Structural Patterns

Structural design patterns deal with composing classes and objects to form larger structures. Wrapper objects are a common mechanism within these patterns to manage relationships and responsibilities between objects in flexible ways. They allow you to add new capabilities or change how an object is accessed or interacted with without modifying its original source code.

Here are a couple of prominent structural patterns that heavily utilize the wrapper object concept:

H3: The Decorator Pattern

The Decorator pattern is a prime example of using wrapper objects. It allows adding new functionalities or responsibilities to an object dynamically.

  • Wrapper's Role: The decorator class acts as a wrapper. It implements the same interface as the object it wraps. It contains an instance of the object and delegates most calls to it, but can add extra behavior before or after the delegated call.
  • Purpose: To extend an object's behavior without subclassing (which can be inflexible) and without modifying the original class.

H3: The Adapter Pattern

The Adapter pattern uses a wrapper to make incompatible interfaces compatible.

  • Wrapper's Role: The adapter class wraps an object with an incompatible interface. It provides a different interface that clients expect, translating calls from the new interface into calls on the wrapped object's original interface.
  • Purpose: To allow classes with incompatible interfaces to work together.

Comparing Wrapper Usage in Patterns

While both Decorator and Adapter use wrappers, their goals differ:

Pattern Primary Goal Wrapper Action Original Class Modified? Delegation Used?
Decorator Add behavior/responsibilities Adds functionality around delegated calls. No Yes
Adapter Translate interface Converts interface calls to wrapped object calls. No Yes

In essence, a "wrapper object structural pattern" isn't a single, named pattern itself, but rather describes a category of structural patterns (like Decorator and Adapter) that leverage wrapper objects to achieve their goals of flexible object composition and interaction without modifying the original classes.

Related Articles