C# Factory Pattern
C# Factory Pattern
C# Factory Pattern
C# Factory Pattern
A factory creates objects. We implement the factory design pattern in a C# program. With this pattern, we develop an abstraction that isolates the logic for determining which type of class to create. Object
Go
C#: Class
Example
The factory design pattern relies on a type hierarchy. The classes must all implement an interface or derive from a base class. We use an abstract class as the base. The Manager, Clerk and Programmer classes derive from Position. Interface Abstract
F a c t o r yp a t t e r ne x a m p l e :C # u s i n gS y s t e m ; c l a s sP r o g r a m { a b s t r a c tc l a s sP o s i t i o n { p u b l i ca b s t r a c ts t r i n gT i t l e{g e t ;} } c l a s sM a n a g e r:P o s i t i o n { p u b l i co v e r r i d es t r i n gT i t l e { g e t { r e t u r n" M a n a g e r " ; } } } c l a s sC l e r k:P o s i t i o n { p u b l i co v e r r i d es t r i n gT i t l e { g e t { r e t u r n" C l e r k " ; } } } c l a s sP r o g r a m m e r:P o s i t i o n { p u b l i co v e r r i d es t r i n gT i t l e {
http://www.dotnetperls.com/factory 1/3
2/24/2014
C# Factory Pattern
g e t { } }
r e t u r n" P r o g r a m m e r " ;
s t a t i cc l a s sF a c t o r y { / / /< s u m m a r y > / / /D e c i d e sw h i c hc l a s st oi n s t a n t i a t e . / / /< / s u m m a r y > p u b l i cs t a t i cP o s i t i o nG e t ( i n ti d ) { s w i t c h( i d ) { c a s e0 : r e t u r nn e wM a n a g e r ( ) ; c a s e1 : c a s e2 : r e t u r nn e wC l e r k ( ) ; c a s e3 : d e f a u l t : r e t u r nn e wP r o g r a m m e r ( ) ; } } } s t a t i cv o i dM a i n ( ) { f o r( i n ti=0 ;i< =3 ;i + + ) { v a rp o s i t i o n=F a c t o r y . G e t ( i ) ; C o n s o l e . W r i t e L i n e ( " W h e r ei d={ 0 } ,p o s i t i o n={ 1 }" ,i ,p o s i t i o n . T i t l e ) ; } } O u t p u t W h e r ei d=0 ,p o s i t i o n=M a n a g e r W h e r ei d=1 ,p o s i t i o n=C l e r k W h e r ei d=2 ,p o s i t i o n=C l e r k W h e r ei d=3 ,p o s i t i o n=P r o g r a m m e r
The factory design pattern is found in the Factory class. The point of the Get method is to take a value and instantiate a class based on that value. It translates integers to objects with a switch statement. Switch Also: Because Manager, Clerk, and Programmer all derive from the same abstract class, the return type Position can be used. And: An implicit cast automatically casts the Manager, Clerk and Programmer to Position references.
http://www.dotnetperls.com/factory 2/3
2/24/2014
C# Factory Pattern
The Main method serves to demonstrate the Factory class in action. We use as part of the demonstration the integers 0, 1, 2, and 3. We use the Get method with each of these values. Then: We show that the appropriate type of class was instantiated for each integer.
Discussion
Imagine you have a system that needs to create objects in many different places. Suppose the system has integers and you want objects for those integers. The factory pattern is ideal for this usage. Tip: You can use the Factory type to handle object creation in a uniform and concise way.
Summary
We looked the factory design pattern, which is used to instantiate objects based on another data type such as integers. Factories can be used to reduce code bloat and also make it easier to modify which objects need to be created.
http://www.dotnetperls.com/factory
3/3