Click Here to Install Silverlight*
IndiaChange|All Microsoft Sites
Microsoft
Communities 
 
Chat Transcript
 
Chat Topic : Creational Design Patterns
Chat Expert : Namratha Shah (MVP)
May 11, 2005
 
 
abhishek[MSFTE] (Moderator):
Welcome to today’s chat. Our topic today is Creational Design Patterns. Before we begin, I would like to
begin by posting the chat rules and instructions for customers:
1. This chat will last for one hour. During this hour, our Experts will respond to as many questions as they can. Please understand that there are some questions we cannot respond to due to lack of information or because the information is not yet public.
2. We encourage you to submit questions for our Experts. To do so, type your questions in the send box, select the Submit a Question radio button and click SEND. Questions sent directly to the Guest Chat room will not be answered by the Experts, but we encourage other community members to assist.
3. We ask that you stay on topic for the duration of the chat. This helps the Guests and Experts follow the conversation more easily. We invite you to ask off topic questions after this chat is over, but not during.
4. Please only submit your question once. Submitting your question more than on
abhishek[MSFTE] (Moderator):
4. Please only submit your question once. Submitting your question more than once is against the Chat Code of Conduct.
5. Please abide by the Chat Code of Conduct.
abhishek[MSFTE] (Moderator):
We are pleased to welcome our Expert for today’s chat. I will have her introduce herself now.
Nasha [MVP] (Expert):
Good Evening Everybody. This is Namratha Shah here .. u can call me Nasha.
Nasha [MVP] (Expert):
I am working for NESS Technologies Mumbai ....
Nasha [MVP] (Expert):
Today we are going to discuss about GOF - Creational Design Patterns.
Nasha [MVP] (Expert):
So is everyone ready ?
Nasha [MVP] (Expert):
Before we go to Creational Design Patterns let me ask you one basic question : What are design patterns?
Nasha [MVP] (Expert):
please put forward wat u know or ur ideas
Nasha [MVP] (Expert):
any one
Nasha [MVP] (Expert):
ok
Nasha [MVP] (Expert):
A design pattern is a solution to a recurring problem in an object oriented environment.
Nasha [MVP] (Expert):
yes
Nasha [MVP] (Expert):
Design patterns need not necessarily be to a particular to an application domain infact it usually addresses problems across application domains and programming languages.
Nasha [MVP] (Expert):
We will discuss today GOF Creational Patterns
Nasha [MVP] (Expert):
Giving you a brief introduction about GOF.
Nasha [MVP] (Expert):
Gang Of Four includes Erich Gamma, Richard Helm, Ralph Joshson and John Vlissides.
Nasha [MVP] (Expert):
These guys worked togather and analysed various object-oriented systems and came up with 23 design patterns and gave these patterns some nomencalture.
Nasha [MVP] (Expert):
These 23 patterns are sub-divided in to 3 categories Creational, Structural and Behavioural patterns with in these categories and they are classified as Class based or Object Based patterns.
Nasha [MVP] (Expert):
Ok the obvious question now is wat are class based and object based patterns ?
Nasha [MVP] (Expert):
any one to answer that ?
Nasha [MVP] (Expert):
Let me put it in simple words .. Class based are those which are based on inheritance and object based are those which use delegation.
Nasha [MVP] (Expert):
Any queries on concepts inheritance or delegation .. if yes .. pls raise them now .. else the later part of discussion would be very confusing.
Nasha [MVP] (Expert):
everybody familiar with inheritance and delegation ?
Nasha [MVP] (Expert):
inheritance is static relationship between two classes
Nasha [MVP] (Expert):
on the other hand delegation is a dynamic relationship where one class uses another class (or an object of another class)
Nasha [MVP] (Expert):
agree ?
Nasha [MVP] (Expert):
kool
Nasha [MVP] (Expert):
Let us start our discussion on Creational design patterns
Nasha [MVP] (Expert):
We will start with the question :- What are creational design patterns ?
Nasha [MVP] (Expert):
hi
Nasha [MVP] (Expert):
Creational Design patterns as the name says it all .. is concerned with the process of object creation.
Nasha [MVP] (Expert):
There are 5 creational design patterns :
Factory Method.
Abstract Factory.
Prototype.
Singleton.
Builder.
Nasha [MVP] (Expert):
Every pattern that I will start with I will give its scope and its intent .... after that I will take an example and discuss the pattern .. and finally discuss its applicablity and proc-cons.
Nasha [MVP] (Expert):
The First patterns to start with is Factory Method.
Nasha [MVP] (Expert):
Scope : Class
Nasha [MVP] (Expert):
Intent : Defines an interface for creating an object, but let subclasses decied which class to instantiate.Factory Method lets a class defers instantiation to subclasses.
Nasha [MVP] (Expert):
When I say the scope of this pattern is Class .. I mean to say that it is an inheritance based pattern
Nasha [MVP] (Expert):
Let us take an example
Nasha [MVP] (Expert):
Consider we are designing Human Resource Information System framework which can be customzied to address the specific requirements of various industries or organizations. In this framework we have classes such as
Nasha [MVP] (Expert):
Employee : To record basic details of an employee
EmployeeCollection : A collection of various Employee objects
EmployeeController : Controller class for Employee responsible for maintaining Employee details.
Qualification : To record employees qualification
Experience : To record employees experience
Nasha [MVP] (Expert):
Employee class contains an object of Experience and qualification. i.e.
class Employee
{
string name;
string age;
Qualification quaf;
Experience exp;
......
}
Nasha [MVP] (Expert):
Since employee controller is reponsible for creating employee objects we have a function called as newEmployee which will return us the employee object.
Nasha [MVP] (Expert):
// structure of EmployeeController
class EmployeeController
{
private EmployeeCollection employees;
public void newEmployee ()
{
Employee employee = new Employee();
employees.add(employee);
}
}
Nasha [MVP] (Expert):
any queries till now ?
Nasha [MVP] (Expert):
please raise ur queries rite away
Nasha [MVP] (Expert):
When I say that scope of this pattern is Class .. I mean that this pattern uses Inheritance
Nasha [MVP] (Expert):
All the relationships between classes are inhertance based
Nasha [MVP] (Expert):
and thatz y they are static
Nasha [MVP] (Expert):
ok saurabh
Nasha [MVP] (Expert):
Creational patterns are related to object creation as i mentioned before
Nasha [MVP] (Expert):
behavioural patterns are concerned with how the objects interact with each other
Nasha [MVP] (Expert):
and structural patterns are concerned with composition of classes or objects
Nasha [MVP] (Expert):
I hope I have solved ur doubt
Nasha [MVP] (Expert):
any more questions ?
Nasha [MVP] (Expert):
i think I would like to limit this to creational patterns only
Nasha [MVP] (Expert):
Is the example for Factory Method Pattern related to Employee and Employee Controller clear
Nasha [MVP] (Expert):
well no
Nasha [MVP] (Expert):
You program will communicate with Employee Controller class
Nasha [MVP] (Expert):
which will instantiate the object for you
Nasha [MVP] (Expert):
as per the current implementation
Nasha [MVP] (Expert):
coz if you c the implementation of newEmployee method
Nasha [MVP] (Expert):
public void newEmployee ()
{
Employee employee = new Employee();
employees.add(employee);
}
Nasha [MVP] (Expert):
we have treated Employee as a Concrete class
Nasha [MVP] (Expert):
Now since we are designing a framework
Nasha [MVP] (Expert):
We plan to sell our framework to an IT Firm so we need to subclass Employee and EmployeeController and create to new classes like ITEmployee and ITEmployeeController which are more specific
Nasha [MVP] (Expert):
to their domain
Nasha [MVP] (Expert):
If check out the code EmployeeController.. you will notice that newEmployee instantiates an object of Employee and add's it to the employee collection. But this is only possible if the Employee class is a concrete class. Since we are creating a framework we would like our Employee class to be an abstract class so that it can be extended ahead as per the domain requirement.
Nasha [MVP] (Expert):
Q: What're the advantages for adapting this?
A: we will c in a while
Nasha [MVP] (Expert):
So now the issue is how to design the framework in such a way that the newEmployee function creates the object and adds it to the collection without making Employee class a concrete class
Nasha [MVP] (Expert):
any body who can come up with a solution to that problem
Nasha [MVP] (Expert):
Q: The collection will hold all the employees records that is fine with this pattern but how these objects are accessible to my program - will you use any other design pattern for that?
A: ur cleint will talk to the controller class
Nasha [MVP] (Expert):
any one ?
Nasha [MVP] (Expert):
is the problem clear ?
Nasha [MVP] (Expert):
anything u want
Nasha [MVP] (Expert):
as long as it solves the issue
Nasha [MVP] (Expert):
and i dont need to make employee Class a concrete class
Nasha [MVP] (Expert):
a hint
Nasha [MVP] (Expert):
ur answer lies in the scope and the intent of the pattern
Nasha [MVP] (Expert):
we are discussing'
Nasha [MVP] (Expert):
so wat will be the implementation of newEmployee function
Nasha [MVP] (Expert):
in the ur ITEmployeeController Class
Nasha [MVP] (Expert):
and ur base EmployeeController class
Nasha [MVP] (Expert):
u will need to use inheritance
Nasha [MVP] (Expert):
yes Ravi
Nasha [MVP] (Expert):
and wat else ?
Nasha [MVP] (Expert):
wat about the EmployeeController class ?
Nasha [MVP] (Expert):
U will even need to create ur ITEmployeeController class from ur EmployeeController class
Nasha [MVP] (Expert):
ok it goes like this
Nasha [MVP] (Expert):
As the intent says that factory pattern defers the object instantiation to the subclasses.
Nasha [MVP] (Expert):
To achieve this we change the implementation of EmployeeController
Nasha [MVP] (Expert):
let c how
Nasha [MVP] (Expert):
We add an abstract method called as createEmployee to the EmployeeController
Nasha [MVP] (Expert):
which will return a employee object
Nasha [MVP] (Expert):
And we change the implementation of newEmployee function ..... so Instead of instantiating an object of employee we call the createEmployee function
Nasha [MVP] (Expert):
Let us view the Structure of EmployeeController Class
Nasha [MVP] (Expert):
/// Structure of EmployeeController
abstract class EmployeeController
{
private EmployeeCollection employees;
abstract public Employee createEmployee();
abstract public Employee createQualification();
abstract public Employee createExperience();
public void newEmployee ()
{
Employee employee = createEmployee();
employees.add(employee);
}
}
Nasha [MVP] (Expert):
Now all the classes which extend the abstract Employeecontroller class needs to implement the CreateEmployee method (and other factory methods).
Nasha [MVP] (Expert):
something like this
Nasha [MVP] (Expert):
/// Structure for ITEmployeeController
class ITEmployeeController:EmployeeController
{
public Employee createEmployee()
{
ITEmployee itemp = new ITEmployee();
itemp.Qualification = createQualification();
itemp.Experience = createExperience()
return new ITEmployee();
}
public Experience createExperience()
{
return new ITExperience();
}
public Qualification createQualification()
{
return new ITQualification();
}
}
Nasha [MVP] (Expert):
any confusions ?
Nasha [MVP] (Expert):
here the createEmployee fuction
Nasha [MVP] (Expert):
will return the employee object ..... and newEmployee function instead of creating an Employee object ... will return the object created by createEmployee method
Nasha [MVP] (Expert):
by doing this I dont need to create my Employee class as a concrete class
Nasha [MVP] (Expert):
Q: But why you choose Employee as abstract?
A: so that I can extend the class to create other classes for different domains like IT,Bank etc
Nasha [MVP] (Expert):
You will notice that the ITEmployeeController provides implementation for createEmployee() , createQualification() and createExperience(). They are the factory methods which are responsible for creating Employee, Qualification and Experience objects or in other words ... the concrete products.
Nasha [MVP] (Expert):
Q: but controllers will also extended for all types
A: yes
Nasha [MVP] (Expert):
let us look at the applicability of this pattern
Nasha [MVP] (Expert):
and the pros and cons
Nasha [MVP] (Expert):
Q: Ok, my argument was all these are possible without making Employee abstract
A: how
Nasha [MVP] (Expert):
When you want your subclasses to decide which class to instantiate. ... ITEmployeeController decides which class to instantiate.
Nasha [MVP] (Expert):
When you want to create 'static' heirarchy of objects ... static because Factory Method is an inheritance based pattern so all the class relationships are predefined.
Nasha [MVP] (Expert):
Pros and Cons of Factory Method :-
Nasha [MVP] (Expert):
On the positive side :
Nasha [MVP] (Expert):
Eliminates the need for creating concrete classes in the base creator.
Nasha [MVP] (Expert):
Gives the subclasses the flexibilty of creating there own objects by by allowing them to implement 'factory method'.
Nasha [MVP] (Expert):
But factory method also has a lot of demerits
Nasha [MVP] (Expert):
It results in the creation of a lot of classes ... coz you will land up creating one concrete creator per concrete product due to subclassing for each creator.
Nasha [MVP] (Expert):
Saurabh that answers ur question
Nasha [MVP] (Expert):
yes this is one of the biggest demerit of FM
Nasha [MVP] (Expert):
Q: but controllers will also extended for all types
A: yes .. thatz the demerit which I just mentioned ... that u will have to extend the controllers
Nasha [MVP] (Expert):
Q: I want to know what is the core problem where i wll need to implement factory approach
A: When you want your subclasses to decide which class to instantiate
Nasha [MVP] (Expert):
Q: I hope this is a valid question. Isn't this Similar to Polymorphism ? The Subclasses can control which Brother Class to Instantiate. Right ?
A: yes ... but the relationship is predefined or static
Nasha [MVP] (Expert):
the other demerits are
Nasha [MVP] (Expert):
consider if you hav multiple creators than
Nasha [MVP] (Expert):
all the creators will have to implement factory methods
Nasha [MVP] (Expert):
and lastly
Nasha [MVP] (Expert):
All relationships between the creators and products are static relationships ... thus cannot be changed at runtime.
Nasha [MVP] (Expert):
In .NET there are quite a few
Nasha [MVP] (Expert):
objects which implement factory method design pattern
Nasha [MVP] (Expert):
one of them is WebRequest
Nasha [MVP] (Expert):
This class has a static factory method called Create
Nasha [MVP] (Expert):
WebRequest.Create
Nasha [MVP] (Expert):
this method creates protocol-specific descendants of WebRequest using the value of the URI passed in as argument.
Nasha [MVP] (Expert):
For example, when a URI beginning with "http://" is passed, an HttpWebRequest object is returned; when a URI beginning with "file://" is passed, a FileWebRequest object is returned.
Nasha [MVP] (Expert):
Q: so controller here is webrequest?
A: WebRequest is the creator
Nasha [MVP] (Expert):
Q: benefit?
A: are the merits and demerits clear
Nasha [MVP] (Expert):
EmployeeController
Nasha [MVP] (Expert):
creating the EmployeeObject
Nasha [MVP] (Expert):
EmployeeController or ITEmployeeController is the creator
Nasha [MVP] (Expert):
Employee or ITEmployee is the product
Nasha [MVP] (Expert):
and createEmployee is the FactoryMethod that we implemented
Nasha [MVP] (Expert):
I hope that makes things clear
Nasha [MVP] (Expert):
good
Nasha [MVP] (Expert):
:)
Nasha [MVP] (Expert):
any other queries
Nasha [MVP] (Expert):
pls ask ?
Nasha [MVP] (Expert):
Q: Nasha can you give some example of Factory Method used in .NET Framework class library? some times we used some patterns but don't know we are using that.
A: Factory method primarily is a used in creation of frameworks .
Nasha [MVP] (Expert):
Q: Nasha can you give some example of Factory Method used in .NET Framework class library? some times we used some patterns but don't know we are using that.
A: There are a lot of classes in .NET which use this pattern like WebRequest, IEnumerable, Cryptography HttpApplicationFactory to name a few
Nasha [MVP] (Expert):
Q: Nasha, Is it possible to implement polymorphism without inheritance?
A: u can use interfaces
Nasha [MVP] (Expert):
Q: and who was creator in our example
A: EmployeeController and ITEmployeeController
Nasha [MVP] (Expert):
Q: or the controller for protocol
A: ya ... but better refered as creator
Nasha [MVP] (Expert):
There are a lot of classes in .NET which use this pattern like WebRequest, IEnumerable, Cryptography HttpApplicationFactory to name a few
Nasha [MVP] (Expert):
Q: then how?
A: instead of creating an abstract class create an interface and implement that interface
Nasha [MVP] (Expert):
Q: then how?
A: answered
Nasha [MVP] (Expert):
Q: I have a little query. Are the application blocks in sync with any of the GOF patterns? I mean, are MS application blocks implement any of GOF patterns
A: yes ... they are ... although I have not gone in to the depths of each one of them so I cant reply u rite away ... but all of the app blocks come with there architecture doc .. u can refer that
Nasha [MVP] (Expert):
Q: Anyway anyother way other than interfaces?
A: nope
Nasha [MVP] (Expert):
Q: I'm in strong confusion that implementation is also a type of inheritance, right?
A: when we say inheritance we mean that the we are carrying the baggage of the base class along with us .... but when we say implementing as in implementing an interface we provide the implementation
Nasha [MVP] (Expert):
Q: Do we say controler do anything more than passing products.
A: in our current example or in general ?
Nasha [MVP] (Expert):
Q: Other than interface, there is also abstract class, no?
A: yes ... interface is a sp case of abstract class ... which does not have any implemented methods
Nasha [MVP] (Expert):
Q: Ok, thank you, you have been wonderful through the session
A: thanx for coming and participating :)
abhishek[MSFTE] (Moderator):
Thank you Namratha here for joining us today on a Microsoft Community Chat to talk about Creational Design Patterns here.
Nasha [MVP] (Expert):
Q: When do we use Interface and When do we use a Class to Inherit ?
A: you use classes when you have some behaviour in your base class object to be inherited ... you use interfaces when you need to provide implementation
Nasha [MVP] (Expert):
You use classes when you have some behaviour in your base class object to be inherited ... you use interfaces when you need to implement behaviour and primarily for polymorphism
Nasha [MVP] (Expert):
interface is a sp case of abstract class which does not have any implementation
Nasha [MVP] (Expert):
any other questions ?
Nasha [MVP] (Expert):
not here dear ... this is a chat for creational design patterns ... let us stick to it
Nasha [MVP] (Expert):
Q: what all patterns do u believe do come in use.. in normal life projects... in ur professional project dev. experiences.... n this one is for everybody....
A: Commonly used patterns factory method, singleton, facade, strategy,template,builder,abstractfactry
Nasha [MVP] (Expert):
Some Commonly used patterns factory method, singleton, facade, strategy,template,builder,abstractfactory
Nasha [MVP] (Expert):
yes
Nasha [MVP] (Expert):
the next i was to discuss was abstract factory
Nasha [MVP] (Expert):
Q: Is Abstractfactory different from Factory Method ?
A: yes
Nasha [MVP] (Expert):
Q: what I am trying to get here is the jist of everybody's work experience. :D There are many creational patterns.. but we have talked only about factory pattern... wht is so ??
A: will continue with others next time
Nasha [MVP] (Expert):
Q: what I am trying to get here is the jist of everybody's work experience. :D There are many creational patterns.. but we have talked only about factory pattern... wht is so ??
A: only 1 hr which is less :(
Nasha [MVP] (Expert):
ok today coz of less time
Nasha [MVP] (Expert):
I could only discuss factory method
Nasha [MVP] (Expert):
4 more are remaining
Nasha [MVP] (Expert):
Q: Technically Who are supposed to be Aware of Patterns ? I mean Project Manager , Architect ?
A: Developers and Architects
Nasha [MVP] (Expert):
Q: can u explain about factory method
A: thatz wat we discussed today :)
Nasha [MVP] (Expert):
Q: Why does C# classes does not allow multiple inhertence?
A: irrelevant to the current topic
Nasha [MVP] (Expert):
ok guys
Nasha [MVP] (Expert):
thatz all for today
Nasha [MVP] (Expert):
will meet u guys soon again and continue ahead
Nasha [MVP] (Expert):
I hope u guys enjoyed and gained something from this session
Nasha [MVP] (Expert):
Thanx a lot for coming and investing ur time here
Nasha [MVP] (Expert):
Thanks a lot
Nasha [MVP] (Expert):
Thanx everyone
abhishek[MSFTE] (Moderator):
Thank you all for joining the webchat today
Nasha [MVP] (Expert):
if u guys have any questions post them on UG
abhishek[MSFTE] (Moderator):
We are now ending the chat due to time restrictions
abhishek[MSFTE] (Moderator):
you can find more information on the Patterns at the Microsoft site: www.microsoft.com/practices
Nasha [MVP] (Expert):
bye guys
abhishek[MSFTE] (Moderator):
We would also encourage you to participate in the local User Groups a list of which is available at: http://www.microsoft.com/india/communities/usergroups/default.aspx
abhishek[MSFTE] (Moderator):
We hope you join us for our next chat on May 25 2005
abhishek[MSFTE] (Moderator):
We would also store a transcript of this chat on http://www.microsoft.com/india/communities/chat/default.aspx
abhishek[MSFTE] (Moderator):
We do community chats every Wednesday from 5:00 to 6:00 PM
abhishek[MSFTE] (Moderator):
for topic being discussed in the upcoming webchat, please visit: www.microsoft.com/india/communities/chat
abhishek[MSFTE] (Moderator):
Please send your feedback about chat and other community activities at: commind@microsoft.com. We eagerly look forward to hearing from you on how we can help you better achieve your potential
abhishek[MSFTE] (Moderator):
Thanks all!

 

 
     

©2009 Microsoft Corporation. All rights reserved. Contact Us |Terms of Use |Trademarks |Privacy Statement