Going down the design pattern of the altar - entry

zhaozj2021-02-16  127

Below is just a simple understanding of design patterns, I only hope that beginners have a quick understanding of the basic content of design patterns. If you need to understand, please see the classics of Masters.

First, why do you have a design pattern, the main purpose is that the software is reused. The hard work, I don't have it, I am not a pity. But the quality of code is too bad, and it will not be wrong with it. So we must try to write the code that can be reused.

Our goal is that the code reuse and maintenance is convenient. It is white, it is 1. Less write duplicate code 2. Try not to modify the completed code 3. If you want to add a function, it is best to add a new class, not modify the existing existing Code. 4. Be sure to modify it, it is easy to modify and will not affect other. 5 ....

Design mode is to teach us how to write code that can meet the above conditions in his experience. It's all objects, so mainly, it is based on object reuse. Use an object to be divided into 3 steps: 1. Create / acquire an object 2. Operate the method of this object to change the object state 3. Destroy this object. So how to create an object is the first question to consider, and there is a creation mode. Operation object is behavioral mode. It is relatively easy to destroy an object, but it will be forgotten, be careful, and its model is about much. Java basically doesn't have to consider this problem too much, so it is also buried. There are still some complicated relationships between objects to describe some complex transactions, of course, structural mode.

Since the creation of an object is the first step, then you will start from the creation mode. If you create an object, the simplest is the most standard, of course, is new myclass obj = new myclass ();

is there any problem? If you are so simple, there is no problem, but if you create an object is not so simple? For example, you have to build a database connection, which is not a New. As follows: string drv = "com.microsoft.jdbc.sqlserver.sqlserverdriver"; string url = "JDBC: Microsoft: SQLSERVER: //127.0.0.1: 1433"; string uid = "sa"; string PWD = "123456";

Connection cn = null; try {class.Forname (DRV) .newinstance (); cn = drivermanager.getConnection (URL, UID, PWD);} catch (exception e) {E.PrintStackTrace ();}

So trouble, you will write so many codes every time you generate a database connection? What to do, paste a copy is a way, but it is generally written a way. The method does not exist independently, you need a Class

public DBManager {public static Connection getCn () {String drv = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String url = "jdbc: microsoft: sqlserver: //127.0.0.1: 1433"; String uid = "sa" String PWD = "123456";

Connection cn = null; try {class.forname (DRV) .newinstance (); cn = drivermanager.getConnection (URL, UID, PWD);} catch (Exception E) {E.PrintStackTrace ();} return cn;}} This time does not have Ctrl C, Ctrl V. Your code is a way to call, it looks simple. This is a factory method, and it is a static factory. Why is it in a static factory, because there is nothing to save, so use the static method is better, and you don't have to create a factory instance. For the calling factory, this is a metaphor, the factory produces products, here is specializing in the production of objects. The metaphor is more appropriate, but also remember. It is best to change the example. public DBFactory {public static Connection getCn () {String drv = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String url = "jdbc: microsoft: sqlserver: //127.0.0.1: 1433"; String uid = "sa" String PWD = "123456";

Connection cn = null; try {class.forname (DRV) .newinstance (); cn = drivermanager.getConnection (URL, UID, PWD);} catch (Exception E) {E.PrintStackTrace ();} return cn;}}

It is also possible to generate an object to be generated by the number of applicants, and use other objects, and use other objects. Any object that cannot be simply generated with a New can consider the factory method. The advantage is that hey, write a few lines of code. In addition, when the method of creating an object is changed, only the factory, the caller is not changed. Don't forget that our goal is to reuse the code.

If a factory only produces a product, it is not a workshop, so letting this factory do something, what is the above factory to generate different databases. Yes, the code modifies: public dbfactory {public static connection getcn (DRV, URL, UID, PWD) {

Connection cn = null; try {class.forname (DRV) .newinstance (); cn = drivermanager.getConnection (URL, UID, PWD);} catch (Exception E) {E.PrintStackTrace ();} return cn;}} So this factory can produce various databases. It is, JDBC can use Java reflex mechanism to implement a code, generate different JDBCs, if not JDBC, it is likely to write a lot of code through if Else. But always paste the code every place. You can also generate JDBC parameters through the configuration file, which is a finer implementation, don't have to be tailored.

This is the origin of simple factory methods (static factories).

转载请注明原文地址:https://www.9cbs.com/read-8396.html

New Post(0)