Showing posts with label design-patterns. Show all posts
Showing posts with label design-patterns. Show all posts

Wednesday, January 1, 2014

Strategy Pattern

Allows behavior of an algorithm to be selected at run time. The strategy pattern, 
  • defines family of algorithms,
  • encapsulate each algorithm and
  • makes algorithms interchangeable




Following is an example of a implementing strategy pattern.




Therefore the host class (The class that uses strategy, below the Context) class doesn't have to implement the details of the strategy. It will only use the needed strategy.

Difference with State Pattern

The strategy pattern deals with how an object performs a certain task. Encapsulating the implementation. Where as The state pattern deal with what state the object is in, wrapping state behavior. Other than that strategy pattern and state pattern are very similar.

Difference with Factory Pattern (DZone)

Strategy pattern may look very similar to factory but they are different. When using strategy if you take above example, Context just have a strategy property which gets injected. But if you use Factory, specifically a ContextFactory, The type is not generic anymore. Your implementation would a Either BusContext or CaraContext. 


Resources

Factory Pattern

Factory pattern is a creational design pattern. It allows creation of objects without exposing internal logic to client. It allows subclasses to decide which class to instantiate. 

Let's look at how we can implement factory pattern.


This is one of the simplest ways to implement factory pattern (noob implementation). Problem is once we add a new employee type we should modify the factory method which violates open/closed principle. We can subclass factory class but factory classes are often used as singleton.

Here's a practical example on database connection.

Using Reflection

We can use reflection to avoid modification to factory class

But reflection comes with the cost of performance. You can use class registration mechanism without using reflection even.

Factory pattern In JavaScript

Factory in JavaScript : method1 (addyosmani) and method2 (carldanley)

When to use Factory

Before implementing factory method in your code think if it is really necessary to implement one because it might add unnecessary complexity to your code.

Command Pattern

In command pattern an object is used to represent and encapsulate (hide information) all the information needed to call a method at a later time. 



We are not directly interact with the event receiver. Instead we use Command Objects to interact



Receiver : Has operations to be executed
Invoker : Calls command to carryout operations. Command object is passed to an invoker object which invokes the command. 
Command : Declares an interface for executing operations. It has Receiver object.
ConcreteCommand : Invoke operations defined on Receiver (links Receiver and action)
Client : Creates ConcreteCommand and sets its Receiver. Holds both invoker objects and command objects. It passes command objects to the invoker  

Example for C#

Usage
  • GUI buttons
  • Macro recording
  • Multi level undo
  • Progressbars
  • Wizards
wikipedia
http://stackoverflow.com/questions/tagged/command-pattern

How MVC, MVP and MVVM patterns are related


MVC

MVC pattern stands for Model, View and Controller. This pattern is used to separate concerns of the application in to different layers

MVP

MVP pattern stands for Model, View and Presenter. It's a derivative of MVC. It is commonly used for user interfaces. 

MVVM

MVVM pattern stands for Model, View and ViewModel. MVVM is also based on MVC and is mainly targets UI development frameworks which supports event-driven programming.

Resources



Proxy Pattern

A proxy functions as an interface to something else. Proxy can interface to a network connection, complex object, file etc. The operations performed on proxy is forwarded to the original object it can also provide additional functionality if required.

Types of proxies,

  • Virtual proxy : placeholder for expensive to create objects. The real object created only when client accesses it
  • Remote proxy: local representative to an object resides somewhere else
  • Protective proxy: Controls access to master object
Use proxy if you want the access to the object to be controlled or additional intermediate functionality should be provided.

Difference with Adapter Pattern

Adapter pattern allows unrelated interfaces to work together by adapting to a common interface which again implemented by Adapter classes. This adapter class will contain a property for each unrelated interface.

Whereas proxy is creating a interface on top of another interface or a class to get access. Both may look same but serves two different purposes. Adapter is like having multiple proxy classes for each interface.

Both uses wrapper classes but serves different purposes.



Wikipedia
https://sourcemaking.com/design_patterns/proxy

Decorator Pattern

Allows behavior to be added to an individual object either statically or dynamically, without affecting the behavior of other objects from the same class. FileReader in C# is a good example for this. Also check this article from codeproject for C#. Explore the Decorator pattern in JavaScript.

Thursday, November 28, 2013

Dependency Injection

Simply Dependency Injection means giving an object its dependencies, instead of them itself having initializing them. Dependency Injection is useful for testing as well. Because it allows dependencies to be mocked or stubbed out. 

Types of Dependency Injection ##

Constructor injection
Setting dependencies through constructor

Setter injection
Setting dependencies using Setter method

Interface injection 


http://stackoverflow.com/questions/12246438/constructor-injection-and-initialization-of-dependencies-being-injected
http://programmers.stackexchange.com/questions/190120/should-i-use-dependency-injection-or-static-factories


What is a Dependency Resolver
Allows you to abstract away Dependency Injection implementation 
http://stackoverflow.com/questions/20086440/asp-net-mvc-4-dependency-resolver

What is interception


You can use Unity for Dependency Injection in .NET. Read this comprehensive article set on unity.

Dependency Inversion in JavaScript

http://stackoverflow.com/questions/5349003/dependency-inversion-principle-in-javascript

Resources

https://www.youtube.com/watch?v=FuAhnqSDe-o

http://stackoverflow.com/questions/13442312/when-do-i-use-abstract-class-or-interface-in-dependency-injection

http://stackoverflow.com/questions/13442312/when-do-i-use-abstract-class-or-interface-in-dependency-injection

https://richnewman.wordpress.com/about/code-listings-and-diagrams/dependency-injection-examples/dependency-injection-example-interface-injection/


Saturday, October 5, 2013

Design patterns for cloud


Strangler pattern

Incrementally developing a newer system replacing the old system. Parts of the old system is replaced with features in new system eventually replacing the complete system. This pattern is mainly helpful for backend systems (API).


Federated Identity Pattern

Delegate authentication to an external identity provider. Simplify the development, minimizes requirements for user administration. 

Cache-Aside pattern
Load data on demand to a cache. Can improve performance.



Monday, October 8, 2012

Tuesday, May 11, 2010

Creational design patterns

Factory

Provides an interface to create objects. Rather than creating new objects using new keyword, we ask the factory to create and return them for us. 

Factory in JavaScript : method1 (addyosmani) and method2 (carldanley)





Builder

http://www.oodesign.com/builder-pattern.html

Lazy initialization

Creation is deferred until it is first used. Useful for complex objects. 

C# : http://msdn.microsoft.com/en-us/library/dd997286%28v=vs.110%29.aspx


Object pool

Useful when cost of initializing a class instance is very high.  Therefore when requester requires an instance, he can get one of already created instance.

Prototype




Singleton 

http://stackoverflow.com/questions/327955/does-functional-programming-replace-gof-design-patterns?rq=1

Sunday, April 4, 2010

Behavioral Design Patterns

Observer

Object (subject) maintains list of its dependencies (observers). Subject will then notify observers whenever any state change occurs in it. 

Related patterns are Publish-Subscribe pattern, mediator and singleton 
http://weblogs.asp.net/fmarguerie/events-references-garbage-collecting-memory-leaks-and-weak-delegates


Iterator



Mediator

Defines an object which encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects referring from each other explicitly.

http://www.dofactory.com/net/mediator-design-pattern  
Template method
http://www.oodesign.com/template-method-pattern.html
Null object

Strategy

Defines set of algorithms that can be used interchangeably. Basically you create an interface and derive implementations from that. Clients can couple themselves with the interface. 

http://robdodson.me/javascript-design-patterns-strategy/
http://sourcemaking.com/design_patterns/strategy

Command


Tuesday, March 16, 2010

Structural Design Patterns

Decorator

Allows behavior to be added to an individual object either statically or dynamically, without affecting the behavior of other objects from the same class. FileReader in C# is a good example for this. Also check this article from codeproject for C#. Explore the Decorator pattern in JavaScript

Source
Example

Facade

Provides simplified interface to larger body of code such as a class library. Checkout this article by dofactory for C# and addyosmani section for JavaScript.


Source

Composite

Describes that group of objects to be treated same way. The objective is to "compose" objects into tree structures to represent part-whole hierarchies.


Proxy

It's a class acting as an interface to something else. This can be a network connecting, web service or some other resource which is expensive or impossible to duplicate


Module pattern

See (http://en.wikipedia.org/wiki/Module_pattern)


Resources

http://en.wikipedia.org/wiki/Software_design_pattern
Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook