Reputation: 28652
Before going to describe my problem first,I would like to define definitions of Decorator and Extension method Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality
Extension method
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type
I have following code snippet in c#
public interface IMyInterface
{
void Print();
}
public static class Extension
{
public static void PrintInt(this IMyInterface myInterface, int i)
{
Console.WriteLine
("Extension.PrintInt(this IMyInterface myInterface, int i)");
}
public static void PrintString(this IMyInterface myInterface, string s)
{
Console.WriteLine
("Extension.PrintString(this IMyInterface myInterface, string s)");
}
}
public class Imp : IMyInterface
{
#region IMyInterface Members
public void Print()
{
Console.WriteLine("Imp");
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Imp obj = new Imp();
obj.Print();
obj.PrintInt(10);
}
}
In the above code I am extending interface without modifying the existing code,and these two methods are available to derived class. So my question is this: Is the extension method a replacement of the decorator pattern?
Upvotes: 23
Views: 12881
Reputation: 838
This explanation by Erich Gamma (GoF) seems to be the best... http://www.mif.vu.lt/~plukas/resources/Extension%20Objects/ExtensionObjectsPattern%20Gamma96.pdf
Essentially stating that
a) Combining all the operations and state that the different clients (present and future) need into a single interface results in a bloated interface
b) Desired operations (known and unknown) can be categorized into components. From which one (or more) Component Interfaces) (extended interfaces) can be defined. These may or may not be implemented by objects (current and future).
c) Clients that wish to use this extended interface can query whether a component supports it
d) Finally, this extended interface has a common base class (ComponentExtension) with minimal interface to manage the extension itself (checking if an extension exists, informing the extension that it is about to be deleted)
To be used when:
1 When your existing classes may need additional and unforeseen interfaces (i.e. new, currently unknown patterns of behavior).
2 When a class representing a key abstraction plays different (unforeseen & open-ended) roles for different clients.
3 You wish to extend without sub-classing
It is similar to the following patterns
Visitor which needs stable class hierarchy & introduces dependency cycle
Decorator where the use is more transparent, the interface is narrow and existing operations should be augmented
Adapter which supports EXISTING interfaces
Upvotes: 0
Reputation: 4644
Extension method a Decorator pattern or a Visitor pattern? After reading I would say its more akin to the Visitor.
Quoting the greatness that is wikipedia, the pedia that never has errors :P
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure it operates on. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to easily follow the open/closed principle. In essence, the visitor allows one to add new virtual functions to a family of classes without modifying the classes themselves; instead, one creates a visitor class that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.
Upvotes: 6
Reputation: 1813
Extension methods are not a replacement for the decorator pattern. Extension methods work to provide functionality to an existing type without having to create a derived type.
This is different from the traditional implementation to the decorator pattern. The decorator pattern allows you to dynamically provide multiple behaviors to an object at runtime without having to create a new subclass for each combination of those behaviors.
Upvotes: 7
Reputation: 101052
A extension method is really just syntactic sugar for calling a static method.
While with a decorator you could actually change the behaviour of your decorated class, a extension method could only alter properties or call methods on your class, just like an "ordinary" static method.
Decorator pattern is actually definied as using a wrapper to alter behaviour, which a extension method clearly doesn't do.
Upvotes: 26
Reputation: 19928
Ýou miss the dynamic part of the decorator pattern. Extension methods are static beasts defined at compile time and can be used or not... but not modified / exchanged at runtime.
Upvotes: 17