Discussion on the DaO factory design mode in Java

Source: Internet
Author: User

Source: http://www.114java.com/javajichu/201007/2389.html


 

In Java Development
With the popularity and wide application of software hierarchical designDaoSet
Calculation Mode
No stranger,DaoLayer
It has become an essential layer in the development of software systems. It separates the background data layer from the foreground vo. Some time agoDaoDesign ModeIntroduction
Introduced a hibernate-based"Design of generic Dao
".

Pass DaoOf
The design does allow our software system to simply separate the data layer from the presentation layer, so that the functions at all levels of our system are clearer. So we began to feel proud, DaoOf
The introduction makes the system coupling more loose, and the presentation layer no longer needs to care about the changes in background data operations. As a result, we began to rest assured that we were unscrupulous in the performance Layer by calling DaoCome
Implement our system. Is that true? Let's take an example to see if our system has the ability to solve the problem.
Now we have an employee management system. In this system, person. Java indicates the employee object, which corresponds to the person table in the database. There are also DaoConnect
Interface-persondao. Java, there is also a persondao implementation class persondaoimpl. java. In the persondaoimpl. Java
We have implemented all the methods to operate the person object. We are proud to say that we have encapsulated operations in persondao. Now we can use
Use persondao = new persondaoimpl () to call DaoOperate on our data objects
. When we were intoxicated with the beautiful code we wrote, the project manager was informed that the efficiency of using hibernate was low, and the customer began to be dissatisfied. We still had to rewrite the Code with JDBC again.
Once. So all DaoStart to convert all hibernate implementations to JDBC implementations. However, the project manager recommends that you do not destroy the existing
Hibernate implementation. Later, we can use technical research to improve the access efficiency of hibernate. So we started to write and write a set of JDBC implementation-
Persondaojdbcimpl. Java, now the problem arises. The performance layer uses persondao = new
It's easy to change all the persondaoimpl () to new persondaojdbcimpl (). Are we beginning to cry.
How can we solve similar problems? So we started to introduce Dao FactoryMode. We create a class daofactory. Java pair DaoInto
Unified management.



Daofactory. Java:
Public class daofactory {public static persondao
Getpersondaoinstance () {return New persondaoimpl ();} // obtain otherDaoInstance ......}


The presentation layer is instantiated through persondao = daofactory. Get getpersondaoinstance ();DaoReal
Example. This solves the problem,DaoChanged. We only needDao FactoryClass
Code: return New persondaojdbcimpl ()
Then our application is quickly switched to JDBC. We can see that using the factory model, our system is further decoupled, and the presentation layer does not need to be related.DaoLayer
Everything has changed.Dao Factory. Developers who have used frameworks such as spring know that spring uses IOC to decouple classes. In factDao FactoryMedium
We can also use the IOC idea to further remove the coupling between classes. Spring performs IOC by configuring the XML file, so we can also use the XML file
. For example, the following XML:



<? XML version = "1.0"?> <Config> <daos> <! -- Group
Organization Service Interface implementation class --> <Dao id = "organizationservice" type = "com. yyyyy. OA. Services. Organization. organizationserviceimpl"> </Dao> <! -- Work
Stream participant interface --> <Dao id = "maid" type = "com. shareyyy. workflow. Services. impl. Particle serviceimpl"> </Dao> <! -- Work
Stream definition interface --> <Dao id = "processdefinitionservice" type = "com. baiyyy. workflow. Services. impl. processdefinitionserviceimpl"> </Dao> <! -- Work
Stream instance interface --> <Dao id = "processinstanceservice" type = "com. interval yyy. workflow. services. impl. processinstanceserviceimpl "> </Dao> </daos> </config>


This is the XML configured for the interface provided by the workflow system to the business system. All interfaces are configured in XML. How can we use it? I just putDao FactoryInto
For further improvement
Processinstanceservice = daofactory. getdao (processinstanceservice. Class ,"

Processinstanceservice "); from the input parameters, we can see that we know the interface to be called.
Processinstanceservice, both through the string"
Processinstanceservice ". We can parse the configuration file above to get the path of the specific implementation class and get the instance of the implementation class through reflection. SpecificDao
Factory
If you are interested in implementing the class, I will not introduce it in detail.
The above is the correctDao FactorySome simple understandings. Now I am studying the design model. I will introduce my understanding of each design model based on my own thoughts. I hope you can study and discuss it together.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.