Preface:I'm going to write a series of articles about swing program development. This is due to the recent development of a swing product I'm doing. For a long time to do the Java EE program, let me some numb. Swing is a model of design, is an elegant art, is a product beyond the times! Having the opportunity to develop swing software makes me feel great! Oh, I hope to have the opportunity to write software with JAVA3D, that feeling must be more great! Both Java and swing are masterpieces. I am a person who has always been very picky about other people, can get my heartfelt praise, imagine how good they are. Oddly enough, they have been unable to occupy the desktop market. Some people say this is the cause of technology. I think this should be a commercial, historical reason. Well, I should also do my best for the success of Java and swing on the desktop, share my thoughts! Thanks to the wonderful memories that Java has brought to me for so many years. Lamp, computer, Java, and coffee, how many sleepless nights I spent! I Love U!
Swing
one of the best architecture design of the program
Business Model Center schema pattern
A business model-centric architecture pattern. This is the architectural pattern that swing and all other programs should adopt. Swing is emphasized because it is more meaningful and more useful to use this "business model centric" architectural pattern in swing programs. We know that a software consists of several parts: 1, the presentation layer (user interface layer) at this time the software and user interaction parts, can be divided into character interface (DOS and other command lines), GUI graphical interface (desktop GUI, browser GUI, three-dimensional interface) and so on. This is only the appearance of software, the appearance of gorgeous, also can not explain the merits of the software! Google's interface is bland, but it's the most powerful software today!  2, business layer This is where software functions are implemented. There are a lot of objects that map the business domain, there are complex business computing. This is where the software really works. The core of the software and the difficulty is here! The enormous amount of development effort and thinking is also concentrated here. Business models, and their interactions with each other, this is the core of software!  3, the database access layer (may have) some software needs to save the operation into the database, permanently preserved. Typically, enterprise-class software that requires business data requires this module. It implements the mapping of business objects and relational databases. Called a "o-r" mapping. 4, Persistence layer (possibly) some software does not save the data into the database, but saves it as a file. such as, binary files, text files, XML files, etc. It implements a mapping between business objects and files. The business object may be serialized. If it is saved as an XML file, we call it the O-xml map, which is the mapping between the business object and the XML file. Now there are some open source software that implements the mapping between Java objects and XML files as if they were o-r mappings. In fact, Java-based XML access components, the development of their own o-xml mapping is very simple, I also wrote a library like this. can see, "business model as the center", is not a special case, but the most basic software development law! Is the basic feature common to all software. Just today, in their different fields, this basic knowledge is blurred by architectural patterns in some of the most common areas. For example, in Java Enterprise Software development, we all know that the EE software is divided into 3 layers: 1, the performance Layer 2, the business Layer-----business object and Service helper Class 3, the Database Access Layer-----DAO helper class Such architectural pattern, blurred the "business object" Is the core of the software as a whole. The Service helper class and the DAO helper class, which are actually part of the business object, can be placed entirely in the business class. The separation of helper classes from business objects is nothing more than a design pattern.
applications in the swing programWell, this article does not want to discuss these "metaphysical" problems. The above discussion is just to make you understand that the "business model-centric" architectural pattern discussed in this article is not unique to swing applications, but rather the features of all software, but is even more useful in a desktop development environment where swing is an excellent condition. Swing program as a desktop program, with a better environment than the Java EE program, more powerful features. Swing programs are often single-machine, even networked, can be distributed in the business layer, and unlike Java EE programs, user interface and response to user action code are distributed on different machines! As a result, the swing program is more capable of exploiting the wisdom and ability of our software developers!
Swing
the second best architecture design of the program
MVC
Mode
the composition of the Swing programIn general, the Swing program consists of the following parts: 1, the presentation layer----swing components of the GUI2, the business layer------business objects, and their relationship with each other 3, the persistence layer-----The business object is saved as a file, or a database record.
The application of MVC pattern in swing programWe know that swing is an example of the MVC paradigm. The various visual components of swing are designed using the MVC pattern.
Swing components typically consist of three parts:The model of 1,swing, which is the M-model part of MVC. It holds the data required by the swing component. The UI of a swing component needs to be presented according to it. The 2,swing UI class. This is the view-view part of the MVC pattern. It performs the task of drawing and presenting swing components based on the data in the model of the component. The 3,swing component class. This is "façade", which encapsulates swing's UI objects and model objects. We generally use it to manipulate swing components without directly using the UI objects and model objects inside the swing component. There is also a complex relationship between swing components. Swing's UI class monitors the data changes of the model object and instantly redraws the interface. You can also register a series of event listeners on swing components. These are controller controllers in the MVC pattern.
using the MVC pattern in swing applicationsThe GUI of a swing application consists of a number of swing components. The various swing components themselves are designed by the MVC pattern, and the presentation layer of our entire swing application should be designed by the MVC pattern. By using the MVC pattern, you can better separate code and data. The whole application of the performance layer, lower coupling, cohesion poly, more flexible. In the process of writing the swing application, we need to use the 2 parts of the Swing component: Model of the 1,swing component. The design of the MVC pattern for swing components makes the entire swing component A model-centric one. By changing the model, we can instantly change the UI appearance of swing components. Registers the event listener on the 2,swing component. This is the controller of the swing component, or it can act as one of the controllers for the entire swing program. In the swing component listener, we can respond to the user's actions as a portal to the entire program's functionality. Here, we can call the business layer's code for business computing. After the calculation is complete, you can modify the appearance of the swing program. It is often the model that modifies a swing component. The listener for swing components is typically implemented using the internal classes of the swing program. Therefore, we can fully use all the resources of the entire swing program in the controller! Therefore, the controller of swing components is strong enough!
This model is not a modelReaders should be more familiar with Java EE. After all, the main battlefield of Java today is in enterprise application, will Java, and do not understand Java EE, this is a bit justified. Here, I use the familiar Java EE Performance layer technology to illustrate the design of the Swing program MVC.
in struts, its MVC consists of the following parts:The 1,view view is a JSP page that uses the struts tag. The 2,controller controller is the action that responds to user action. The 3,model model is the data submitted in the user's HTML page form. This is referred to as the "form" form in struts. The data of the HTML form is string type, and the properties of our business object are not necessarily string. Therefore, in a struts application, we need to construct the business object based on the received Form object. In Springmvc, a mechanism is provided to help us automatically convert form data to business objects. For some special type conversions, we still need to provide the conversion manually. The automatic conversion between form and business objects is, of course, an improvement in development efficiency. However, we must understand that the data form and the business object in the GUI component are two different things. 2 of them happen to be the same, or can be automatically converted, just a special case. They are essentially two different things. A form is a data container for a presentation layer, whereas a business object is a business-specific object. The data type of the form, the change is not much, but the business object data type, that is the ever-changing. Therefore, the form object type is consistent with the business object type, which is a rare exception.
form data (form) and Business Objects (Model) in swing programsform data in a Web application form, which is the data from the individual items of the HTML form, is of type string. form data in the swing program. is made up of the model of each swing component of the swing program. The data types of each part are very different. For example, the simplest text box JTextField model, isan object of type Javax.swing.text.Document. This object, we can also directly get or set the value of string as model. In fact, this string type of data is the document property. Also, the model of the JTree component is the TreeModel object. Therefore, the form data form in the swing program is very different. Obviously, our business objects are unlikely to be the same type as swing's form data. Therefore, the transformation between the data form of the performance layer of swing program and the model of business object must be realized by ourselves. It is unlikely that the form will be used directly as a business object in the business layer. It is also impossible to display the data of business objects directly in Swing's GUI!
The use of the MVC pattern for swing programsThe entire swing program should use the MVC pattern: in 1,jframe or other top-level containers, each swing component forms the view layer. A graphical interface for displaying data and providing user action. 2, one or a group of business objects is model. They store the data that the swing component will display. They are business objects and therefore can be used directly in business layer code to perform complex business calculations. They cannot be displayed directly in swing components. Instead, you need to construct the form object, which is the model of the swing component, to present the data, based on the business object. In order for the View-swing component to present the data of the business object in real time, we need to have the swing component listen to the business object and, once the business object changes, re-build the model of the new swing component based on the new data to present the latest business object data on the swing component. Here, we useChangeListener Change the listener. In order for a business object to be able to get the latest data entered by the user, we also need to register the business object on the swing component. Once the swing component's data has changed, the business object is notified. The business object modifies the value of the business object based on the model of the swing component, which is the data of the Form object. In order to achieve such a function, I have also used the event mechanism to achieve this function. This is discussed in the following article. Now, between the business object and the swing component, two-way reference is realized through the bidirectional event listening mechanism. 3, registers the Listener (Controller) that responds to the event on various components of swing in response to the user's actions. These controllers are implemented using anonymous inner classes. The controller on each swing component is not just the controller of the swing component, but the controller of the entire swing program. Because the inner class can manipulate all the resources of the entire swing program, we can use the form of all swing components in the controller, or we can use all the business objects, invoke all business methods, and implement whatever functionality is needed! The controller, the entry point for business functions, connects the performance and business layers of the swing program, connecting the Swing form (model of the swing component) and the business object. Now, our swing program structure is clear, the function is reasonable, low coupling, cohesion, it is a model of MVC model!
the interaction between swing components and business ObjectsIn the above, we propose an event listener mechanism between swing components and business objects. The swing component listens to the business object and uses an implementation mechanism of the "watcher pattern" to changelistener change the listener. Of course, you can also use a mechanism such as observe/observable, or other event mechanisms. However, after the trade-offs, I think the ChangeListener is the best fit for my needs. It's powerful and simple. When we need a swing component to listen to our business objects, we create a subclass of that swing component that implements the ChangeListener interface, implementing public void StateChanged (ChangeEvent e) {...} method to change the display of this swing component by building its own new model based on the data of the listener's business object. How do business objects listen to swing components? I love ChangeListener, or register a ChangeListener listener on the swing component, the listener is our business object. However, given the wide use of changelistener, in order to avoid firing the event by other operations of the swing component, I have created a new type of interface with reference to the ChangeListener interface: Controllerchangelistener interface. Now, the data on the swing component can be kept in sync with the data of our business objects! Forms and business objects, although of different types, seem to be the same object, with the same data!
The execution flow of the swing program using MVC mode1, the user can see a GUI interface composed of a swing component. 2, the user in the interface to operate. The controller of the 3,swing component is triggered. 1) The Controllerchangelistener event may be fired, causing the business object to automatically update with the model data of the swing component. 2) The business object updates the data and fires the ChangeListener event on the business object. This causes all listener business objects whose swing components update their model data to change the display of swing components. 3) We may execute business-layer code and perform complex business calculations for business objects. Gets the value of the new business object. This also triggers the ChangeListener event for the business object, allowing the model of the swing component to be updated to change the interface displayed by the swing component. 4) Finally, of course, we can also directly modify the model or appearance of some swing components. Because a controller as an inner class can use all the properties and methods of the swing program. In my "Swing multithreaded programming" article, I discussed the swing program where Swingui processing code and general business code should be assigned to different threads. So, in the controller here, we should use the SwingWorker helper class to put the business code in
PublicObject construct () {...} method is executed in the and the method that directly modifies the Swing component UI is put to public
void finished () {...} method to execute the. This way, theSwing program's interactivity, responsiveness and performance will be greatly improved! As you can see, here, by associating the business object with the form object (model of the swing component), we only need to "business object centric" in the swing application. Manipulate business objects and perform business operations. The user interface of the swing program is updated automatically. This is the great advantage of using the "business object-centric MVC model"! Http://www.cnblogs.com/armlinux/archive/2007/02/05/2391035.html
Swing Program Best Architecture Design-business object-centric MVC pattern (GO)