Source: http://www.114java.com/javajichu/201007/2389.html
In Java Development
With the popularity and wide application of software hierarchical designDaoSet
Calculation ModeNo 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
FactoryIf 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.