content:
What is the framework case example framework for software development How to make a valid framework design Another instance in-depth understanding of the author's evaluation
related information:
Software Quality Road (1): Software Quality Framework Software Quality Road (2): Software Quality Road (3): Test Drive Development Software Quality Road (5): Component's large-scale software architecture
In the Linux area:
Tutorial Tools & Product Codes and Component Project Articles
Lin Xing (IAMLINX@21cn.com) March 2004
What is a framework that appears in software development, the rise of open source software, making a variety of frameworks, such as a lot of framework products, including domestic familiar Struts . So what is the framework? In design mode, Gamma et al. Gives a definition: "The frame is a class of collaborative work, and they build a reusable design for specific types of software." [Gamma 94, P.26] framework is For specific problems areas, for example, Struts is a framework for Web development. The frame includes a set of abstract concepts. These abstract concepts are derived from the problem area. For example, Struts is designed based on MVC mode, so it must build abstract concepts for Model, View, Control. The framework makes these abstraction concepts and provides an extension form to achieve reuse. This is the specific work of the framework. Framework works on abstract concepts, defines collaboration between abstract concepts. The difference between the frame and ordinary software or class library is that the user reuses the framework by extending the framework. The design of these extensions is called the core of the frame design. For example, in Struts1.0, an extension method (expand Action) is used. Framework instance Spring (http://www.springframework.org) is an open source framework, which is positioned in a holistic J2EE-based application framework. His positioning philosophy is not re-inventing the wheel. For example, he provides a database access mechanism, but his data access mechanism is based on JDBC, Hibernate, and JDO. The idea of reuse and re-package is full of the entire Spring framework. Most of the domestic software industry is located in system integration, which is ideal for domestic software organizations. The purpose of using this example is also this. The frame should use existing technologies as much as possible. Not to repeat investment. JDBC is a very basic data access API. It encapsulates access to the relational database, but it should be acknowledge that JDBC is still a low level API, so when we use, we have to write a lot of code to complete a simple job:
PreparedStatement St = DB.PRepareStatement ("Select User.Name from User);
ResultSet RS = st.executeQuery ();
While (rs.next ()) {
System.out.println (Rs.getstring (1));
}
Rs.close ();
St.close ();
Seeing how much trouble, but please note that there is a serious flaw in the above code, because there is no abnormal code, adding an exception code means we need to add some code, this duplicate labor makes programmers work Like a fool. Remember the principle of automation in code automation? We should automate them for duplicate labor. How to do? Note that we found that most of the query code is not too big in addition to the different SQL statements, most of the query code is not too big, so our goal is to extract the common part, and leave a specific part to the developer himself. . So, what should I do with a frame? The definition of the recall framework is to define a group of abstract bodies, and collaboration between its abstract bodies and provide extensions. In JDBC, abstracts have DataSource, Connection, PreparedStatement, ResultSet, Statement, SQLException. Therefore, the first step in establishing a framework is to analyze the behavior of these abstract bodies, which is common and which is proprietary. Using the Spring Framework, the final JDBC client code is like this: jdbcTemplate Template = New JDBCTemplate (Datasource);
Final List names = new linkedlist ();
Template.query ("Select User.name from User",
New rowcallbackhandler () {
Public Void ProcessRow (ResultSet RS) throws sqlexception {
Names.Add (rs.getstring (1));
}
});
First, the code creates a JDBCTemplate instance, which is the core JDBC package, we can see its partial implementation later. Then, by incorporating a SQL statement and a callback anonymous class by sending the Query method of JDBCTemplate and a callback. It can be seen that things that the client need to do include a DataSource that provides a SQL Statement, and a specific processing method. These actions are different for each client, but for the process of Connection, PreparedStatement, Statement, SQLException, basically similar. Next, let's take a look at the practice of JDBCTemplate:
Public void query (String SQL, ROWCALLBACKHANDAL CALLBACKHANDBACKHANDLER CALLBACKHANDBACKHANDLER CALLBACCESSEXCEPTION {
Connection con = NULL;
PreparedStatement PS = NULL;
ResultSet RS = NULL;
Try {
Con = DataSourceCeutils.getConnection (this.DataSource);
PS = con.preparestatement (SQL);
RS = ps.executeQuery ();
While (rs.next ()) {
CallbackHandler.ProcessRow (RS);
}
Rs.close ();
ps.close ();
}
Catch (Sqlexception EX) {
Throw this.ExceptionTranslater.Translate ("JDBCTemplate.Query (SQL)", SQL, EX);
}
Finally {
DataSourceUtils.closeConnectionIfnecessary (this.datasource, con);
}
First (1 place), the code uses DataSourceUTILS to get an available connection from DataSource, and then processes the query statement using PrepareStatement. 2 is the essence of the entire program. The code uses a callback interface to handle the records taken from the database to the callback interface. Let's combine the code here and the previous client code, you can understand its ideas. 3 Plant to handle exceptions, the SQLException is more meaningful, that is, the subclass of DataAccessException, as a single SQLEXCEPTION indicates that complex database operation is too simple. Finally (4), the connection is closed regardless of the result of the result. It should be said that this code is not difficult to understand. But it fully demonstrates the framework of work and the meaning of the framework we will talk about. Framework's significance of software development The core value of the accumulation framework is the accumulation of knowledge. Software development is a knowledgeive activity. But knowledge of knowledge is in the human brain, is the most difficult to accumulate. In software development, the code is the most determined knowledge, people and machines can understand the purpose of code by browsing code, and will not have different understandings. Therefore, the accumulation of knowledge from the code is the best way. The frame is the output of this idea. The frame contains a lot of code that is a description of the relationship between abstraction concepts and these abstraction concepts. Therefore, the framework is competent for the integration of knowledge. Although the code is the core of the frame, the framework of the light code is difficult to understand. The level of the code is too low, which is difficult to understand the framework of the code from the perspective of the code. Therefore, there must be a workpiece higher than the code. These workpieces can be design documents, domain models, UML diagrams, javadoc. Their purpose is to help the framework developers and the user's smooth understanding of the framework. Just like the example above, through a JDBCTemplate object, you have accumulated a JDBC's best practice. Of course, you can also use documentation to require developers to use JDBC in some way, but there will be a lot of problems in practice, for example, the exception handling of JDBC may be too cumbersome and ignored. Although there will be problems most of the time, it will often be very troublesome when the problem occurs. There is no similar problem in the way in the framework. The accumulation of assets is essential to the protection of assets. Another important protective job is that software organizations (especially enterprises) need to ensure that learning and improvement in knowledge is legally authorized. For example, illegal outflows of knowledge are not desired to see any organization. The form of knowledge accumulates to the framework helps to relieve this situation. The frame can be published in the source code, or it is available in the library form, and the different framework users can choose a different release form, which can act as authority control. The reason why the reuse framework is encouraged is because it can be reused. In the development of the software organization, the framework is the development method, and the framework is used in the development and the framework is improved after development. During this process, the reuse work has been carried out. Reuse is actually not so difficult. As in the example above, in fact, the code is not difficult, thinking is also very clear, in fact, it is to extract universal behavior. Optimizing the architecture framework represents an excellent software architecture. The framework defines the extension method to standardize the use of the framework. This allows the software to maintain the stability and consistency of the overall architecture. In the above example, after using the frame, the client can save a lot of code, and the code structure will be clearer. The key to large-scale software design large-scale software design is to provide reasonable division of applications, and provide a consistent manner to establish an architecture. Large-scale software design requires core designers working on an abstract level. Although they belong to designers, they also need to write code, and these codes are frame code.
In the agile method, the designer's profession will never mean that you just need to write a design document. If you often read some specification, you will find that many of the specifications are written in code. However, these code does not provide implementation, only the abstract interface is provided. How to effectively design a good frame design is a certain guideline to follow. Some of the concepts given below have laid the theoretical basis for framework development. Design abstract hierarchy. In the definition of the frame, the abstract body is critical. The definition of abstract bodies depends on the target of the frame. The framework without the target is not a framework, or a class library is either a programming language. In the above example, first there is a target that simplifies the JDBC operation and then defines the abstraction from this target. So we got an abstract body such as Connection, ResultSet, Statement. Regulate behavior in the abstract level. There is still no way to work with abstract bodies. It also needs to define the behavior of abstract bodies. In the above example, we define the behavior of obtaining the data set. However, in JDBC, in addition to obtaining the data set, you may need to fill the data into the value object, and you need to support all the operations of the Crud. These are abstract behaviors. With these behaviors, we need to standardize and exhaust these behaviors. Analyze the general part of the abstract behavior and the non-liquid part. In the abstract body's behavior, some movements are common, some are special. The former is part of the framework to be implemented. The latter is left to the user as an extension. The abstract level is extracted as a frame and is designed to expand. With abstract bodies, after the universal behavior of abstract bodies, you can design extensions. The simplest extension is the way to use method calls, complicated may pass design modes or configuration files. The evaluation criteria for the expansion point design is convenient to use, where the use includes applications, commissioning, testing, etc. Appropriate use of design patterns. The design pattern represents advanced software design ideas. The appropriate design pattern in the framework helps improve the structure of the frame. For example, in the above example, the design of the extended point uses the callback design mode. Excessive design patterns are not adopted in the frame design, which will make the framework difficult. Of course there is also an example. Anti-JUNIT FRMEWORK. As the automation unit test framework, JUnit uses a large number of design patterns in a simple design, including Command mode, Template Method mode, Collecting Parameter mode, PLUGGABLE Selector mode, Adapter mode, Composite mode, etc. Simplify the use of the frame. In the above example, after the expansion point of the frame is designed, the code using the frame is still more complicated, the callback interface and the anonymous class will still make some inexplicable. So, in order to make the framework is convenient, a certain degree of simplification is still required. In the JUnit framework, we only need to let the test method can automatically test with Test, and this function is that the Command mode cannot be provided. According to the requirements of Command mode, a test class can only include a test method. The reason is that the Pluggable Selector mode, Adapter mode, and reflection technology have been used in JUNIT to make new packages for Command mode: protected void runtest () throws throwable {
Method Runmethod = NULL;
Try {
Runmethod = getClass (). getMethod (FName, New Class [0]);
} catch (nosuchmethodexception e) {
Assert ("Method /" FNAME "/" Not Found ", False;
}
Try {
Runmethod.Invoke (this, new class [0]);
}
// catch invocationtargetexception and illegaaccessException}
Isolation Third Party Technology. Current software development has developed towards the direction of collaboration. In this case, a large number of third-party software appeared. The division of software in the software will bring prosperity to the software industry, but for software organizations, we need to consider the cost, vitality of third-party software, and the level of dependence on it. This part of the work should be given to the frame. Let the framework are responsible for separating core applications and third-party technologies. For example, as a developer of an enterprise application, I found that the change in the database level is too big, the new XQuery query language, ORM technology, which makes the application system require constant changes. This undoubtedly adds risks to the application system. Therefore, I decided to design an abstract level and isolate these technologies and core applications. Abstract levels are only responsible for inquiring the database that meets certain conditions. As for this query, it doesn't matter if you use SQL or XQuery. Therefore, technical details are isolated. Another example above discussed example is a simple example, mainly from the code level to tell the concept of the framework. But in order to fully understand the power of the frame, we need to look at the concept of the frame from a higher level. The example of our choice is Eclipse. Since IBM donates Eclipse to open source, Eclipse has quickly become a very good integrated development tool (it is definitely simple Java IDE), what is the Eclipse? The key is the design concept of Eclipse and the underlying support framework developed according to the design concept. In software development, a wide variety of skills require a wide range of development tools, but if they cannot interact with each other, the development process is difficult to coherent. Eclipse's goal is to solve this problem. The value of the Eclipse platform is its promotion: Rapid development of integrated functionality based on the plug-in model. So how do you do it? Let's see the description of Eclipse to this issue: Eclipse's core is the architecture of dynamic discovery of plug-ins. The platform is responsible for handling the background work of the basic environment and provides standard user navigation models. So each plugin can focus on performing a small amount of tasks. What types of tasks are there? Definition, test, production animation, release, compilation, debugging, diagram, etc., as long as you think there should be. Eclipse's platform running environment is the support section of the framework, while Workbench, Help, Team, Workspace involved in the platform are abstracts, while plug represents the system's extension point (called hook). Here you need to discuss the Positioning philosophy of Eclipse. Eclipse as an integrated development tool, he hopes to focus on a variety of development tools into a platform, but all things are impossible by one organization, the best way is to build an organization. Can integrate different technical providers technology. So his positioning philosophy is to provide a way to do things, not to do specific things. This idea is very common in excellent software design, and another excellent example is Ant. Due to the limit, we will not discuss in depth. Therefore, Eclipse only provides an abstract body for the IDE environment, the Java object model provides a group of abstract bodies, the developer of the plugin can design your plug-in according to your own needs, and access your plug-in by extension points to Eclipse. Eclipse is a successful software and a successful framework. In-depth understanding of the Eclipse platform entry on developerWorks provides information about Eclipse. The framework process mode is a book that discusses the frame design process, which you can learn from it to the development process of the framework and the difference between the development of ordinary software.