Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Factory Pattern [duplicate]

Possible Duplicate:
Interface vs Abstract Class (general OO)

Hi Experts,

I am reading on Factory patterns

  1. Based on article- http://aspalliance.com/809_Working_with_Factory_Design_Pattern_using_C

We define interface, interface is implemented by classes. Based on certain attribute we decide which class is instantiated

  1. Looked at another article- http://csharpdesignpatterns.codeplex.com/wikipage?title=Factory%20Method%20Pattern&referringTitle=Home

We define abstract class and implement it in the subclasses.

Both the examples listed are quite different. Based on fundamentals that I remember.

  • When do we use interface - When we need to implement polymorphic behavior
  • When do we use abstract class - to provide default base functionality

Help me understand how both using interaces vs using abstract classes is called as factory pattern ?

Are these two valid approaches ?

Apologies for formatting issues.

Thanks in advance

like image 519
Siva Avatar asked May 16 '11 16:05

Siva


1 Answers

Since patterns are abstract concepts, they are not strictly dependent on implementations; so using interfaces or abstract classes are both valid approaches imo.

The main idea of Factory pattern is to "simplify" object creation by making an abstraction.

Whether you use interfaces or abstract classes depends on your implementation. In my experience, factory pattern usually came to play when the class structure is already defined. For example, I've already made the decision to use interfaces or abstract classes in my model and now I want to refactor so that object creation is simpler => I go for the factory pattern. Hope this helps.

like image 64
henginy Avatar answered Oct 02 '22 09:10

henginy