4

Comparing these two patterns - Factory and DAO patterns, I see that they are quite similar:

Both of them have interface (Product/Dao), Factory (ProductFactory/DaoFactory) and concrete implementation (ConcreteProduct,ConcreteDao).

Factory creates some concrete implementation object and this object is used by client via interface.

So are they similar or I don't understand something? Or maybe is Dao pattern a specific implementation of factory pattern?

Thanks in advance.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • 1
    Possible duplicate of [What is dao factory pattern?](http://stackoverflow.com/questions/6401543/what-is-dao-factory-pattern) – Armfoot Sep 30 '15 at 18:26
  • 1
    A DAO generally is an abstraction to some type of database/backend. The idea is that you can work with the data and not care about the particular implementation on the backend (SQL Server, NoSQL, etc.). A factory is even more abstract. It provides some implementation for a type of phenomena, and you defer to the factory what that should be. – lintmouse Sep 30 '15 at 18:27

4 Answers4

2

The Data Access Object (DAO) pattern abstract away the details of persistence in an application. Instead of having the domain logic communicate directly with the database, file system, web service, or any other persistence mechanism : The domain logic speaks to a DAO layer instead. This DAO layer then communicates with the underlying persistence system or service.

The advantage of the DAO layer is that if you need to change the underlying persistence mechanism you only have to change the DAO layer, and not all the places in the domain logic where the DAO layer is used from.

Look at this article for better understanding.

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.

The factory instantiates a new concrete product and then returns to the client the newly created product(casted to abstract product class).

The client uses the products as abstract products without being aware about their concrete implementation.

Read this article for better understanding.

Factory pattern talks about creation of object & DAO patterns talks about management of data

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
1

IMO They are pretty different

DAOs are used to transform between serialized (normally sql) versions of the data and Java objects

On the other hand, a Factory can be used to facilitate generic solutions for problems where a significant proportion of the work is similar. An example of this that I created a while back was a SocketHandlerFactory, to be used by a generic ServerSocket, so that however the generated Socket needs to be handled, you can configure this in the Factory.

Does that shed some light?

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Thank you for your time. You mean they are different in fields of usage (DataSources and SocketHandler) but I am saying they are similar in their structure and way they are used (interface,factory, implementation). – Pavel_K Sep 30 '15 at 18:36
  • Sure, I'd agree with that. The idea for both is to encapsulate as much plumbing as possible and make the rest modular – ControlAltDel Sep 30 '15 at 19:25
1

I'm assuming that when you are referring to the DAO pattern then you have something like this in mind:

enter image description here

source

However DAO's don't necessarily require factories to create them. For example in the Spring context there is no difference between Repositories and DAOs and there are no Repository factories as far as the client is is involved.

Quoting the Oracle article, that the diagram was taken from, they are in fact making use of a the GoF factory patterns (there are 2 in the Gang of Four book; Abstract Factory and Factory Methods):

The DAO pattern can be made highly flexible by adopting the Abstract Factory [GoF] 
and the Factory Method [GoF] patterns (see "Related Patterns" in this chapter).

In summary, there is a clear difference in use-case if not structure of both patterns and that the factory within the DAO-pattern likely refers to a 'real' factory patterns that was added as an additional benefit, not as integral part of said pattern.

real_paul
  • 574
  • 2
  • 22
0

The major difference lies in the semantic context in which these design patterns are used.

DAO is used to abstract away the domain logic of Database Handling.

Fatory is used to abstract away the object creation logic.

Dont bother about the implementation/interface details.

Separate your application into layers and if you require a database, then, Having DAO is one option, which could be used across layers.

basav
  • 1,475
  • 12
  • 20