# Wrapper
A wrapper in software engineering is a piece of code that encapsulates or provides a modified interface to another component, system, or data type, often to abstract complexity, provide compatibility, or add functionality.
**Pronunciation:** rap-er
**Difficulty:** Intermediate
**Synonyms:** Adapter, Facade, Proxy, Decorator, Shim, Layer
**Categories:** Software Design, Programming Concepts, Design Patterns
**Tags:** Design Pattern, Abstraction, Encapsulation, Software Architecture, Programming, Object-Oriented Programming
Canonical: https://scaleengineer.com/glossaries/wrapper
---
## Definitions

- **General Programming Concept / Abstraction Layer:** In its most general sense, a **wrapper** (or **wrapper class/function**) is a piece of code that encapsulates or provides a simplified, standardized, or modified interface to another piece of code, component, or data structure. The primary purpose is to abstract away complexity, provide compatibility, or add functionality without altering the original component. It acts as an intermediary, delegating calls to the underlying object or system.

Key concepts include:
*   **Encapsulation**: Hiding the internal details of the wrapped component.
*   **Abstraction**: Providing a simpler or more convenient view of the underlying functionality.
*   **Delegation**: The wrapper typically calls methods or functions on the wrapped object.

For example, a C++ class might wrap a C-style library to provide an object-oriented interface, making it easier to use within a C++ application. Similarly, a Python function might wrap a complex command-line tool, allowing it to be called with simpler arguments.
- **Design Pattern Context:** The concept of a **wrapper** is fundamental to several well-known software design patterns, where it describes a specific structural relationship between objects:

*   **Adapter Pattern**: An **adapter** (often called a **wrapper**) converts the interface of one class into another interface clients expect. It allows classes with incompatible interfaces to work together. Imagine an old power plug (the adaptee) and a new socket (the target interface); the adapter allows them to connect.
*   **Decorator Pattern**: A **decorator** (a type of **wrapper**) attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. For instance, a `LoggingStream` might wrap an `OutputStream` to add logging capabilities without changing the original stream.
*   **Facade Pattern**: A **facade** provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use. While not strictly a wrapper of a single object, it wraps a *subsystem* of objects to simplify its interaction.
*   **Proxy Pattern**: A **proxy** (another form of **wrapper**) provides a surrogate or placeholder for another object to control access to it. This can be used for lazy initialization, access control, logging, or remote object access. For example, a `RemoteImageProxy` might wrap a `RealImage` object, loading the image only when it's actually needed.

In these contexts, the term **wrapper** highlights the act of surrounding or enclosing another entity to modify or control its behavior or interface.
- **Data Structure / Type Wrapper:** In many programming languages, especially object-oriented ones, **wrapper classes** are used to convert primitive data types (like integers, booleans, or characters) into objects. This is often necessary when working with collections or frameworks that require objects rather than primitives.

For instance, in Java, `int` is a primitive type, but `Integer` is its corresponding **wrapper class**. This allows an `int` value to be treated as an object, enabling it to be stored in `ArrayLists` or used with generics. Similarly, Python's built-in types like `int`, `str`, `list` are already objects, effectively acting as wrappers around their underlying C implementations.
- **Scripting / Command-line Wrapper:** A **wrapper script** or **command-line wrapper** is a script (e.g., in Bash, Python, Perl) that executes another program or command-line tool. These scripts are often used to:

*   **Simplify complex commands**: Providing default arguments or a simpler syntax.
*   **Automate common tasks**: Chaining multiple commands together.
*   **Add logging or error handling**: Intercepting output or errors from the wrapped program.
*   **Set up environment variables**: Ensuring the wrapped program runs in the correct environment.

For example, a `run_tests.sh` script might wrap a complex `pytest` command, adding specific flags or setting up a virtual environment before execution.

## Etymology

The term 'wrapper' in software engineering derives from its literal meaning: something that wraps or encloses another thing. It implies an outer layer that surrounds an inner component, providing a protective, modifying, or simplifying interface. Its use became prevalent as software systems grew in complexity, necessitating layers of abstraction.

## First used

1970s-1980s (conceptually with modular programming and early OOP)

## Historical context

The concept of a **wrapper** emerged naturally with the evolution of modular programming and object-oriented programming (OOP). As software systems became more complex, the need to manage dependencies, hide implementation details, and reuse code became paramount. Early forms of modularity in the 1960s and 70s, such as those seen in languages like Simula and Smalltalk, laid the groundwork for encapsulating data and behavior.

In the 1980s and 1990s, with the rise of mainstream OOP languages like C++ and Java, the idea of a **wrapper class** or **wrapper function** became a common idiom. Programmers frequently needed to integrate legacy C libraries into new object-oriented codebases, leading to the creation of C++ classes that 'wrapped' C functions, providing a more idiomatic C++ interface. This practice was a direct application of the **adapter** concept, allowing incompatible interfaces to work together.

The formalization of design patterns in the mid-1990s, particularly with the publication of the 'Gang of Four' book 'Design Patterns: Elements of Reusable Object-Oriented Software' (1999), solidified the term's usage. Patterns like **Adapter**, **Decorator**, **Facade**, and **Proxy** are all specific manifestations of the **wrapper** concept, each addressing a particular problem of object composition and interface management. These patterns provided a common vocabulary for discussing how to structure code to be more flexible, reusable, and maintainable by adding layers of indirection or abstraction around existing components. The term **wrapper** thus became a general descriptor for any construct that encloses another to modify its behavior or interface.

## Q&A

- **What is a wrapper in software engineering?:** A **wrapper** in software engineering is a piece of code that encapsulates or provides a modified interface to another component, system, or data type. Its main purpose is to abstract complexity, provide compatibility, or add functionality without altering the original.
- **Name some design patterns that are considered types of wrappers.:** Common design patterns that are types of **wrappers** include the **Adapter** (to convert interfaces), **Decorator** (to add responsibilities dynamically), **Facade** (to simplify a subsystem interface), and **Proxy** (to control access to an object).
- **Why are wrapper classes used in languages like Java?:** In Java, **wrapper classes** like `Integer` or `Boolean` are used to convert primitive data types (`int`, `boolean`) into objects. This is necessary when working with collections or generics that require objects.

## Usage examples

- The team decided to create a **wrapper** class to provide a more user-friendly interface to the complex legacy database API.
- We used a Python **wrapper** script to automate the execution of the command-line tool, adding logging and error handling.
- The `Integer` class in Java is a classic example of a **wrapper** class for the primitive `int` type, allowing it to be used in collections.
- The **adapter** pattern, often referred to as a **wrapper**, allowed us to integrate the old payment gateway with our new e-commerce system by converting its interface.
- To add caching functionality without modifying the core service, we implemented a **proxy** as a **wrapper** around the original service object.

## Related terms

- Adapter Pattern
- Decorator Pattern
- Facade Pattern
- Proxy Pattern
- Encapsulation
- Abstraction
- Delegation
- Interface
- Middleware

## Popular related terms

- API
- Library
- Framework
- Module
- Component
