With the popularity of the model concept, more and more programmers understand the model and usage model, many people will have such a doubt when learning the model: "Is it necessary to get so complicated?". Indeed, because the example of the tutorial is too simplified (so convenient reader learning), or when the author's option is not very good, it does not reflect the advantages of the scheduled model. In many cases, if you only have the questions of its example, mode is Too complicated. Therefore, this misunderstanding: "Is the mode complicated simple questions?" Of course, as you developed the practice, you will finally find the powerful power, and the model is not an archer programming, it is some method skills that have been extracted.
Through the learning model, the programmer began to bid farewell to the quasi-linear code method, and the mode is expanded to open our vision, strengthen our way of thinking of object-oriented programming. However, another universal problem now appears, blind application mode. The mode is a problem solution, and there is a problem before you have a problem, and the mode is born with the problem you want to solve. It must be understood that in many cases, it is necessary to enhance flexibility and reuse at the cost of increasing the complexity of the code. If you use a mode in your own code, only the complexity of the code is only increased, and other aspects have little effect, or some partial code does not exist flexibility and highly reused demand, then we don't have to Abandoning a more intuitive and simple code writing in use.
First-class master 90% energy attention to the solution solution, because finding a good solution, then write code will be easy and smooth, seeing such code is a kind of enjoyment and improvement; second-class familiar 90% energy attention The code is implemented, because the problem is not the best, the implementation code will be more complicated; the three-way breeding bird records, 90% energy is in the keyboard, often doing a lot of time, it is not good, go back and then use 90% Time to knock the keyboard, you will not use any modes at all, and the code written is only to understand. The software made is also crushed. It has to be a variety of changes, and you still don't know what problems will be generated after the change, and there is a feeling in dangerous houses.
Here is an example of an abuse pattern. I have participated in the second development of a large group company OA system, developed along with the original code architecture and add new functional modules. When the document is rare, when I read the original code, I was turned to the code in its program. I was going to get the head. I finally read: the original code architecture is generally used in factory model, and is the most complex abstract factory model. It generates all module classes through the factory, and each module class has an interface, and each interface has only one module real class because the permissions control also uses the agent (Proxy) mode. After reading the code, I started embedding code. I found that every additional class is going to increase the corresponding code in six java files, and each additional method is added in the class, it is necessary to go to four java files such as its interface. Increase the corresponding code. God! ! ! I remember that my Xiaom couldn't listen to the call, that is, because frequent use of Ctrl C, Ctrl V, Xiaom finger is tired by the CTRL button. The whole project is hard to be confused, it is really annoying. After the project, I reviewed that the agent mode is also used to use (now the horizontal control of the permissions) has a new solution to this new solution), the factory model is here, but also does not solve any problem, but increase the code. Complexity and coupling, reducing development efficiency, continuous maintenance is improved. And the way each class is simple to add an interface. It is even more reasonable, which makes me very much, said that Zhou Xingchi said: "Ball ~~~ is not so kick ~~~~, interface ~~~ Not like this ~~~ ". The words retired, let's first see such a common problem: a system needs to support a variety of types of databases. People who have used Oracle, MSSQL and other databases know that their SQL write methods are different. For example, Oracle's unique identifier automatic 1 field is sequence, MSSQL changes the field attribute, and there are also individual specific SQL usage. In order to support multi-database, do we need to develop multiple systems? Of course, NO. Please see the solution below. Often data inventory is in a variety, we can put all the operations of the database in the system abstracted, write into a class into a class, there are several databases we write a few such classes. The specific design class diagram is as follows:
brief introduction:
OracleDataOperate, SqlServerDataOperate, MySQLDataOperate, represents Oracle, SQLServer, Mysql's three databases. Inherited from AbstractDataOperate AbstractDataOperate is an abstract class that contains those different types of databases are the same code. Inheriting from DataOperate DataOperate is a unified interface of the data operation class above, only two ways: obtain a record, insert a record. DataOperateFactory is a factory method that uniformly uses its method to get an instance of the database operation class. SampleClass is a class of a functional module in our system. People is an entity class representing a record. Three fields OID unique identifier, name name, date birthday.
Detailed description:
1. All system function module classes only recognize that this interface does not have to manage the specific implementation class is OracleDataOperate and SQLServerDataOperate. The DataOperate source code is as follows:
public
Interface DataOperate {
/ / Take a record according to the unique identifier of the record
People getPeople (String OID);
// Insert a record
Boolean INSERTPEOPLE (People People);
}
2, AbstractDataOperate, OracleDataOperate, SQLSERVERDATAOPERATE, MYSQLDATAOPERATE, is inherited the DataOperate interface, there is nothing to say, omitted. 3, DataOperateFactory. Let's take a look at how the factory method is written.
public
Class DataOperateFactory {
public
Static
Final
Int Oracle = 0; // Defines three constants representing database types
public
Static
Final
INT mysql = 1;
public
Static
Final
INT SQLSERVER = 2;
Private
Static DataOperate DB;
Private
Static
INT DATATYPE = mysql;
/ **
* A database operation class is obtained according to the database type (DATATYPE),
* Here the DataOperate uses a single example mode because OracLDataOperate is both stateless tool classes.
* So only one instance is only reserved throughout the system.
*
*
@return returns the interface, the client does not have to care about the implementation class
* /
public
Static DataOperate GetInstance () {
IF (db ==
NULL) {
If (DataType == Oracle) / / Returns the corresponding implementation class according to DateType
Return
New oraclerdataoperate ();
IF (DataType == mysql)
Return
New mysqldataoperate ();
IF (DataType == SQLServer)
Return
New SQLServerDataOperate ();
}
Return DB;
}
}
4, then look at how the use is how to call the factory method and use the data operation class.
/ **
* System a function class
* /
public
Class SampleClass {
Private DataOperate DB; // Declare a database operation class, note that the interface is used
/ ** Method * /
public
Void sampleMethod () {
DB = DataOperateFactory.getInstance (); // Get a single instance
People P = db.getpeople ("123"); // Get a record
db.insertpeople (p); // plug in
}
}
We found that there is no shadow of Oraceldataoperate, MySQLDataOperate, etc. in SampleClass, which is the power of the interface. The client does not have to write different code for ORACELDATAOPERATE, it only cares about DataOperate, and the logic to take that class is responsible by DataOperateFactory.
to sum up:
From the example we can see what is a programming way for interfaces. SampleClass uses data operation classes that you can do not have to care about it, as long as it is in line with the interface.
To instance? Just call DataOperateFactory.GetInstance (), other payment in the DataOperateFactory factory, use the side without care.
We must support new database types, just like an ORACELDATAOPERATE, then write a class that inherited AbstractDataOperate, such as SysbaseDataOperate. Then add the corresponding code to DataOperateFactory. If we want to be configurable, you can use
Private
Static
INT DATATYPE = mysql; the value in the text is set to a text file.
For the development of systems that support multiple databases, it is strongly recommended to use Hibernate. I am doing the system now uses mysql. When I develop, I want to change the database when I want to give the customer, I don't have to do any changes. transplant. However, this is nothing to use by the methods mentioned in this article.