A day with .Net

My day to day experince in .net

Archive for March, 2013

Applying a uniform style to all child elements in a Grid – WPF Tips

Posted by vivekcek on March 25, 2013

So you have Grid with some TextBlock, TextBox and Button controls.How can we apply a Margin to all such controls coming inside Grid.

The solution is Given below


        <Style  x:Key="Margin">
            <Setter Property="Control.Margin" Value="5"></Setter>
        <Style TargetType="Grid" x:Key="GridStyle">
                <Style TargetType="TextBlock" BasedOn="{StaticResource Margin}"></Style>
                <Style TargetType="Button" BasedOn="{StaticResource Margin}"></Style>
                <Style TargetType="TextBox" BasedOn="{StaticResource Margin}"></Style>

<Grid Background="Beige" Style="{StaticResource GridStyle}" x:Name="grdProduct">


Posted in WPF | 1 Comment »

Simple Factory Vs Factory Method Vs Abstract Factory by Example

Posted by vivekcek on March 17, 2013

I came across lots of situations in which, I have to explain the difference between a Simple Factory, Factory Method and Abstract Factory patterns.

The main objective of this post is how simply you can explain the difference among these patterns using some example. I hope the readers are aware of these patterns, because I am not going to explain each pattern in depth.

Simple Factory Pattern

Creates objects without exposing the instantiation logic to the client.
Refers to the newly created object through a common interface




The heart of above Simple Factory pattern is the ‘MobileFactory’ class. From the Client when we create an instance of this ‘MobileFactory’, this class will load and cache all the classes that implement the ‘IMobile’ interface by using reflection or some other logic. After that we can call the ‘GetMobile(string Name)’ method in the ‘MobileFactory’ class which will return the specified object through the parameter.

The Client will expect an object that implements the ‘IMobile’ interface.

Factory Method


Defines an interface for creating objects, but let subclasses to decide which class to instantiate
Refers the newly created object through a common interface.




In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.

In my example the client want a Nokia object. So the steps are given below.

1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.

2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.

3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.

Abstract Factory


Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes




The main point regarding Abstract Factory pattern is that, this pattern creates a family of related objects that have different parent class or interface.

In Abstract Factory pattern the object creation happens in the same way as Factory Method. The only difference is the creation of related objects.

I now updated our old ‘IMobileFactory’ interface with two new methods.
CreateNokiaMobile() – Returns a Nokia objects that implements an ‘INokia’ interface.
CreateAppleMobile()- Returns Iphone objects that implements ‘IApple’ interface.

There are two concrete implementation of ‘IMobileFactory’ named 3GMobileFactory and 4GMobileFactory.

3GMobileFactory – Can return 3G supported mobiles of Nokia and Apple. So as per definition confirms, which returns a family of related objects that have different parents (INokia, IApple).

4GMobileFactory – Can return 4G supported mobiles of Nokia and Apple


By this post my aim was to help you to explain the differences of these three patterns with a simple diagrammatic example.

Posted in Design Patterns | 4 Comments »