Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Wednesday, July 8, 2015

Javascript Mixing Pattern

In traditional object oriented languages we could write inherited classes and reuse functionality. However, in javascript, inheritance comes from chaining prototypes.  But for performance reason, you should not have long prototype chain. Therefore in javascript, for code reuse we can use mixing pattern. Multiple inheritance makes it easy by using mixing pattern.
The concept of mixing is to take an object and mixing the functionality with another object.
First we will define two objects.
Then we will define mixings.
Then we will define mixing function.
Now add mixing to the above objects and check the output.
Most important thing to remember is, you are injecting functionality to your code. Therefore you must document it properly in a large code base.

Javascript Inheritance Pattern

In javascript we do not have classes. We have objects. There's is only one way to make inheritance in javascript. That is by chaining prototype.
in the following example I have defined Person and Student objects where Student object inherit form the Person object.

Tuesday, May 19, 2015

Factory Pattern

Factory Pattern is one of the most useful design pattern which we have to use in every day life. There are some variations in this pattern like abstract factory pattern and etc. According to the name, there is a client which order some factory to create objects rather than create products by himself.
I will demonstrate this using a Ice cream Factory. Assume we have several Ice cream factories which specializes on various topping types.
First we will create an Ice Cream Factory and Ice Cream.
Then we will create Chocolate Ice Cream and it's factory.
Then we will create Strawberry Ice Cream and it's company.
Then create an Ice Cream Seller which buy Ice Cream from various factories.
Below code will show, how the seller get various topping types, depending on the factory that seller buy Ice Cream.

Tuesday, July 31, 2012

Decorator Pattern

Decorator Pattern adds additional attributes to an object dynamically.

Picture frame would be a example. The picture is an object which has own attributes. But for the display purposes we add a frame, in order to decorate it.

In programming world, Decorator pattern is about the wrapper objects. The definition is, "Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviors"

We'll create an ice cream application, Vanilla/chocolate ice cream will be decorated with almonds/honey.

Create an interface which contains the MakeIceCream functionality.
Using above interface create vanilla and chocolate ice cream create implementation.
We need to decorate that vanilla/chocolate ice cream. This should happen without effecting the existing class structure.
For that, create a decorator class by implementing the IceCream interface.
Using the above decorator class we can create Honey/Almond decorated ice cream like below.
 Now we can create a program to demonstrate how the decorators will be used.

Friday, June 15, 2012

Unity Container

Unity Application Block is a dependency injection container which supports for,
  • Property Injection
  • Constructor Injection
  • Method Call Injection
Dependency injection is a design pattern which allows to injects the components at run time, rather than the compile time.

Property Injection

Below sample project is, the way to bind different database implementation(Oracle & SQL implementation) at run time, without compile the application again and again.

Create a solution and add projects as follow.

In the UnitLib project, declare the interfaces which we are going to bind objects at run time. It's better to define the interfaces in a separate project. Because dependency injection is pattern is about loose coupling and separation of behavior.
Implement the interfaces in the UnityLibImplOracle and UnityLibImplSQL projects. We should write the different implementation in separate projects. According to the sample, it's better to separate the Oracle implementation and the SQL implementation.
In the facade we can define the StudentFacade like below.
The real implementation of IUnityStudent's Register method will not be known till the run time.
In the run-time the concrete implementation of the IUnityStudent will be injected  by Unity, depending on it's configuration. In the type we should define the class of the property with the name space and then by separating comma, we should define the Dll file name. In mapTo xml tag define the concrete class that should be use in the run time.

The entry point for the application is like,
In the above configuration file, in the run-time the program will access the Oracle based implementation. By changing only the configuration file, in the run time we can load the implementation of the SQL.

In the above code we are calling the resolve no argument method. But we can pass the registration name and through that we can bind the related objects at the run time.

Constructor Injection

Create a class called Student
In the constructor after the object initialization, using setters, the FirstName will be set.

Method Injection

In the UserFacade class define the method that we need to inject at the run time.
We can configure the App.config file like below.
According to the Microsoft, If you are not sure which type of injection to use, the recommendation is that you use constructor injection. This is likely to satisfy almost all general requirements.