What is the Extension Object Design Pattern?

Göksu Deniz
5 min readApr 21, 2023

--

Image is created by DALL-E 2 AI

The Extension Object design pattern is a software design pattern used in object-oriented programming. It allows adding new functionality to an existing object without modifying its code.

The pattern works by defining a separate extension object that is responsible for providing additional functionality to an existing object. The extension object typically implements an interface that the existing object can use to access the new functionality.

When the existing object needs to use the new functionality, it delegates the responsibility to the extension object. This way, the existing object’s code remains unchanged, and the new functionality is added by adding new code in the extension object.

The Extension Object pattern is useful when you have a large codebase and you want to add new features without introducing bugs or breaking existing code. It also makes it easier to maintain the code by separating concerns and reducing the amount of code changes needed to add new features.

Overall, the Extension Object pattern is a useful design pattern for software development that can help make code more modular and easier to maintain.

Here’s an example of how the Extension Object design pattern could be implemented in C#:

// Define an interface for the extension object
public interface IExtension
{
void NewMethod();
}

// Define the existing object
public class ExistingObject
{
public void ExistingMethod()
{
Console.WriteLine("Existing method called.");
}
}

// Define the extension object that implements the IExtension interface
public class ExtensionObject : IExtension
{
public void NewMethod()
{
Console.WriteLine("New method called.");
}
}

// Extend the existing object using the extension object
public static class ExistingObjectExtension
{
public static void UseNewMethod(this ExistingObject existingObject, IExtension extension)
{
extension.NewMethod();
}
}

// Example usage
var existingObject = new ExistingObject();
var extensionObject = new ExtensionObject();

existingObject.ExistingMethod(); // Output: "Existing method called."

existingObject.UseNewMethod(extensionObject); // Output: "New method called."

In this example, we define an interface IExtension for the extension object, and an implementation of this interface ExtensionObject. We also define the existing object ExistingObject that has a method ExistingMethod.

We then define an extension method UseNewMethod for ExistingObject that takes an instance of IExtension. This method delegates the call to NewMethod to the extension object passed as a parameter.

Finally, we show an example usage where we call ExistingMethod on existingObject, which outputs "Existing method called." We then call the UseNewMethod extension method on existingObject, passing in an instance of ExtensionObject, which outputs "New method called."

You can use just the ExistingObjectExtension without defining an interface for the extension object. However, using an interface provides some benefits, such as decoupling the extension object from the existing object, making it easier to test, maintain, and replace the extension object.

If you use only the ExistingObjectExtension without an interface, it means that you have to tightly couple the extension object to the existing object. This tight coupling makes it difficult to replace or modify the extension object without affecting the existing object. It also makes it more challenging to test the existing object's behavior without having to create a concrete implementation of the extension object.

Therefore, defining an interface for the extension object provides more flexibility, decoupling, and maintainability to your codebase, making it easier to extend and modify your existing objects’ behavior.

Here’s the modified example without the ExtensionObject class:

// Define the existing object
public class ExistingObject
{
public void ExistingMethod()
{
Console.WriteLine("Existing method called.");
}
}

// Extend the existing object using the extension method
public static class ExistingObjectExtension
{
public static void NewMethod(this ExistingObject existingObject)
{
Console.WriteLine("New method called.");
}
}

// Example usage
var existingObject = new ExistingObject();

existingObject.ExistingMethod(); // Output: "Existing method called."

existingObject.NewMethod(); // Output: "New method called."

In this modified example, we define the ExistingObject class and the extension method NewMethod for ExistingObject. The NewMethod implementation is directly included in the ExistingObjectExtension class.

Finally, we show an example usage where we call ExistingMethod on existingObject, which outputs "Existing method called." We then call the NewMethod extension method on existingObject, which outputs "New method called."

This modified implementation works and achieves the goal of the Extension Object pattern, which is to add new functionality to an existing object without modifying its code.

This pattern remind us of Fluent Interface Design Pattern at some points. Given example has a similar structure to the Fluent Interface Design Pattern.

In the Fluent Interface pattern, methods are chained together in a way that allows the resulting code to read like a series of sentences. The methods typically return the object instance itself, allowing for further method chaining. This can make code more concise and easier to read, write, and maintain.

In the modified example, we define an extension method NewMethod for the ExistingObject class, which can be called on an instance of ExistingObject and returns void. However, we can modify this method to return the ExistingObject instance itself, allowing for method chaining. Here's an example:

// Define the existing object
public class ExistingObject
{
public void ExistingMethod()
{
Console.WriteLine("Existing method called.");
}
}

// Extend the existing object using the extension method
public static class ExistingObjectExtension
{
public static ExistingObject NewMethod(this ExistingObject existingObject)
{
Console.WriteLine("New method called.");
return existingObject;
}
}

// Example usage
var existingObject = new ExistingObject();

existingObject.ExistingMethod(); // Output: "Existing method called."

existingObject.NewMethod().ExistingMethod(); // Output: "New method called.", "Existing method called."

In this modified example, we modify the NewMethod extension method to return the ExistingObject instance itself. This allows us to chain the NewMethod call with the ExistingMethod call, resulting in code that reads like a series of sentences.

While this example has a similar structure to the Fluent Interface pattern, it’s important to note that the Extension Object pattern and the Fluent Interface pattern are distinct patterns with different goals. The Extension Object pattern is used to add new functionality to an existing object without modifying its code, while the Fluent Interface pattern is used to make code more concise and easier to read by allowing methods to be chained together.

Conclusion

The Extension Object pattern is a useful software design pattern that allows adding new functionality to an existing object without modifying its code. It promotes code modularity, separation of concerns, and maintainability by decoupling the extension object from the existing object and defining a clear contract for the extension object to follow.

The pattern works by defining an extension object that provides additional functionality to an existing object through an interface or extension method. The existing object delegates the responsibility of using the new functionality to the extension object, allowing for code reuse and easy addition of new features.

Overall, the Extension Object pattern is a powerful tool for software developers that can help make code more flexible, modular, and easier to maintain. However, like any design pattern, it should be used judiciously and in appropriate situations to achieve the desired benefits.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# Reference

It was created with the help of ChatGPT AI.

--

--

Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.