开发者

Examples of functional or dynamic techniques that can substitute for object oriented Design Patterns

This is somewhat related to Does functional programming replace GoF design patterns?

Since the introduction of lambdas and dynamics in C#, are there any of the standard design patterns that could be considered obsolete or solved in some other way using lambdas or other language features?

For example, the dynamic features of C# can now be used to do multi methods. http://achoiusa.wordpress.com/2009/08/27/exploring-c-4-0-multimethods/ (I think Marc Gravell had some post about this to ?)

Personally I tend to do factories using Func of T nowdays.

e.g.

public static class SomeFactory
{
     public static Func<IUnitOfWork> GetUoW = 
       () => new EF4UoW(new SomeModelContainer());
}

// usage

var uow = SomeFactory.GetUoW();

// testabillity

var testUoW = new InMemUoW();
testUoW.Add(new Customer()...);

SomeFactory.GetUoW = () => testUoW;

// the service can get an UoW using the factory
var result = SomeDomainS开发者_开发百科ervice.DoStuff(...); 

Anyone got other examples?

[Edit] Ofcourse the patterns don't go obsolete per se but some patterns are paradigm specific and thus since C# is now multi paradigm, some of the functional properties of C# may make some of the OOP patterns less attractive.


Design patterns do not become obsolete just because a single language evolves. Patterns are usually language-agnostic.

In a sense you could say that .NET made the Observer pattern 'obsolete' already in .NET 1.0. However, that's not really correct because the pattern didn't become obsolete - the framework just provides a default implementation of the pattern, which means that you rarely have to implement it yourself.

In the same sense you can say that delegates are just anonymous interfaces, so a Func<T> is an Abstract Factory.

Patterns don't go away just because a language supplies idiomatic support for them.


Examples of some of the patterns which are provided as part of .net or C# that are not implemented explicitly by the programmer. I am providing answer with respect to .net in general and not specific to C#

  1. Iterator patterns: By using a List<> or Collection<> developer gets the implementation of IEnumerable by default. So, explicit implementation is not needed. But having said that, there is someone (in this case, .net framework) who is implementing the pattern for the developer.

OOAD Design patterns do not become obsolete but developer need not know the implementation to use such patterns.

  1. Object.Clone() is another example - prototyping. Developer need to just implement shallow or deep copy based on the requirement. But .net framework guarantees to support prototype pattern.

  2. Decorator - with XAML, you can decorate a UI control with additional responsibilities. A text box can be decorated with border color, width & so on.

  3. Tunneling and bubbling of events is an example of Chain or responsibility pattern

  4. Events are first class citizens in .net and need not be implemented from scratch. This is an example of Observer pattern which programmer can use without implementing from scratch.


You have the Visitor pattern as it's described in this answer :

Inheritance and service class


Of course, the obvious one would be the Strategy Pattern, which can also be done with Higher Order Function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜