Optimal Architecture Design of swing programs-MVC model centered on Business Objects

Source: Internet
Author: User

Swing Optimal Program Architecture Design - Service-oriented MVC Mode Preface:I plan to write a series of articles about swing program development. This is because I recently developed a swing product. I am a little numb when I have been working on javaee for a long time. Swing is a model of the design model. It is an elegant work of art and a product beyond the times! I have the opportunity to develop swing software! Well, I hope I have the opportunity to write software with Java3D, which makes it even better! Java and swing are both masterpiece. I have always been very picky about others and can get my sincere praise. I can imagine how good they are. The strange thing is that they have never been able to occupy the desktop market. Some people say this is a technical reason. I think this should be the reason for business and history. Well, I should also do my best to share my thoughts on the success of Java and swing on the desktop! Thanks to the wonderful memories that Java has brought to me over the years. Desk Lamp, computer, Java, and coffee, how many sleepless nights can I spend with me! I love u! Swing One of the best program architecture designs Business Model center architecture model  Business Model-centricA business model-centric architecture. This is the architectural model that swing and all other programs should adopt. Swing is emphasized because it is more meaningful and useful to use this "business model-centric" architecture model 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 can be divided into character interfaces (DOS and other command lines ), gui gui (desktop GUI, browser GUI, and 3D interface. This is only the appearance of the software, and its appearance is gorgeous, and it cannot describe the advantages and disadvantages of the software! Google's interface is not surprising, but it is indeed the most powerful software today! 2. The business layer is where software functions are implemented. There are a large number of objects mapped to the business field and complex business computing. This is where the software really works. The core and difficulty of the software is here! The huge development workload and thinking are also concentrated here. The business model and interaction between them are the core of the software! 3. database access layer (possibly) some software needs to save the operation to the database and keep it permanently. Generally, enterprise-level software that requires business data needs this module. It maps business objects to relational databases. Called the O-R ing. 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, and XML files. It maps business objects and files. It may be a serialized service object. If it is saved as an XML file, we call it a O-XML ing, A ing between a business object and an XML file. Now some open source software implements the ing between Java objects and XML files, just like O-R ing. In fact, Java-based XML access components, their own development of O-XML ing is also very simple, I also wrote such a library. As you can see, "taking the business model as the center" is not a special case, but the most basic software development rule! Is a common feature of all software. However, today, in different fields, this basic knowledge is blurred by architecture models in some common fields. For example, in Java enterprise-level software development, everyone knows that J2EE software is divided into three layers: 1, performance Layer 2, business layer ----- Business Object and service assistant Class 3, database access layer-the architecture model such as Dao Assistant class blurts the fact that "Business Objects" are the core of the entire software. Both the Service Assistant class and the DaO Assistant class are actually part of the business object and can be fully placed in the business class. The Assistant class is separated from the business object, but it is only a design model. Applications in swing programsWell, this article does not want to discuss these "metaphysical" issues. The above discussion is just to make you understand that the "business model-centric" architecture model discussed in this article is not unique to swing applications, but the characteristics of all software, however, it is even more useful in a desktop development environment with superior swing conditions! As a desktop program, swing programs have a better environment and better functions than javaee programs. Swing programs are often stand-alone. Even if they are networked, they can be distributed at the business layer, unlike javaee programs, user interfaces and codes that respond to user operations are distributed on different machines! Therefore, swing programs can give full play to the wisdom and capabilities of our software developers! Swing Architecture Design 2 MVC Mode Composition of swing programsGenerally, a swing program consists of the following parts: 1. gui2, business layer ------ business objects, and their relationships. 3, persistence Layer-stores Business Objects as files or database records. Application of MVC pattern in swing ProgramWe know that swing is a model of MVC. Various visual components of swing are designed in the MVC mode. The swing component consists of three parts:1. Swing model, which is the M-model part in MVC. It stores the data required by the swing component. The UI of the swing component needs to be presented based on it. 2. Swing UI class. This is the view part of the MVC mode. It executes the tasks of drawing and displaying the swing component based on the data in the component model. 3. Swing component class. This is the "Facade", which encapsulates the swing UI objects and model objects. We usually use it to manipulate the swing component and do not directly use the UI and model objects inside the swing component. There is a complicated relationship between swing components. Swing UI class, monitors data changes of Model objects, and instantly redraws the interface. You can also register a series of event listeners on the swing component. They are controller controllers in MVC mode. Use the MVC pattern in swing applicationsThe GUI of a swing application is composed of many swing components. Each swing component is designed in MVC mode, and the presentation layer of our entire swing application should also be designed in MVC mode. Using the MVC pattern, code and data can be better separated. Lower coupling, higher cohesion, and higher flexibility. In the process of compiling swing applications, we need to use two parts of the swing component: 1. The model of the swing component. The MVC Pattern Design of swing components enables the entire swing component to be model-centric. By changing the model, we can instantly change the UI appearance of the swing component. 2. register the event listener on the swing component. This is the controller of the swing component and can also be one of the controllers of the entire swing program. In the listener of the swing component, we can respond to user operations as the entry to the function of the entire program. Here, we can call the code at the business layer for business computing. After calculation, you can modify the appearance of the swing program. It is often used to modify the model of a swing component. The listener of the swing component is generally implemented using the internal class of the swing program. Therefore, we can use all the resources of the entire swing program in the controller! Therefore, the Controller of the swing component is powerful enough! This model is not another modelReaders should be familiar with javaee. After all, the main battlefield of Java today is for enterprise-level applications. Java is not familiar with javaee, which is a bit hard to say. Here, I describe the MVC design of the swing program with the presentation layer technology of javaee that everyone is familiar. In struts, its MVC consists of the following parts:1. view is the JSP page using the struts tag. 2. The controller is the action that responds to user operations. 3. The model is the data submitted in the HTML page form. This is called a form in struts. The data in HTML forms is string type, but the attributes of our business objects are not necessarily string type. Therefore, in the struts application, we need to construct the business object according to the received form object. Springmvc provides a mechanism to automatically convert form data into business objects. For some special type conversions, we still need to manually provide conversions. The automatic conversion between form and Business Objects improves the development efficiency. However, we must understand that the data form and business object in the GUI component are two completely different things. The two happen to be the same, or they can be automatically converted, but they are only special cases. They are essentially two very different things. Form is the data container of the presentation layer, and the business object is the object from the specific business. The data type of the form does not change much, but the data type of the Business Object is ever-changing. Therefore, the form object type is the same as the business object type, which is a rare exception. Form and Business Object (model) in the swing Program)Form data in Web applications is the data of various items in HTML forms, all of which are of the string type. Form data in the swing program. It is composed of the models of various swing components of the swing program. The Data Types of each part are very different. For example, the model of the simplest text box jtextfield is an object of the javax. Swing. Text. Document Type. We can also directly obtain or set the string value as the model. In fact, this string type data is the document attribute. Also, for example, the model of the jtree component is a treemodel object. Therefore, there are many types of form data in swing programs. Obviously, our business objects are unlikely to be consistent with the form data type of swing. Therefore, the conversion between the data form at the presentation layer of the swing program and the business object model must be implemented by ourselves. It is unlikely that the form will be directly used as a business object in the business layer. It is also impossible to directly display the data of business objects in the swing Gui! Use of the MVC mode of swing programsThe entire swing program should adopt the MVC mode as follows: 1. In jframe or other top-level containers, each swing component forms the view layer. Displays data and provides a graphical interface for user operations. 2. One or more business objects are model. They store the data to be displayed by the swing component. They are business objects. Therefore, they can be used directly in the business-layer code to execute complex business computing. They cannot be directly displayed in the swing component. Instead, you need to construct a form object based on the business object, that is, the model of the swing component, to display data. To enable the view-swing component to display the data of business objects in real time, we need to enable the swing component to listen to Business Objects. Once the Business Objects change, the swing component will reload the new data, create a model for the new swing component to display the latest business object data on the swing component. Here, we use changelistener to change the listener. To enable business objects to obtain the latest user input data, we also need to register the business objects on the swing component. Once the data of the swing component changes, the business object is notified. The Business Object modifies the value of the business object according to the model of the swing component, that is, the data of the form object. In order to implement this function, I also used the event mechanism to implement this function. This will be discussed below. Now, the business object and the swing component use a bidirectional event listening mechanism to implement bidirectional reference! 3. register the Event Response listener (Controller) on each swing component to respond to user operations. These controllers are implemented using anonymous internal classes. The Controller on each swing component is not only the controller of the swing component, but the controller of the entire swing program. Because internal classes can manipulate all resources of the entire swing program, we can use the form of all swing components in the Controller, or all business objects to call all business methods, implement any required functions! The Controller is the entry point of business functions. It connects the presentation layer and business layer of swing programs, and the form (Model of swing component) and Business Objects of swing. Now, our swing program has a clear structure, reasonable functional differentiation, low coupling, and high cohesion, which is a model of the MVC model! Interaction between swing components and Business ObjectsIn the preceding section, we propose an event listening mechanism between swing components and Business Objects. The swing component listens to Business Objects and uses the observer mode to change the listener. Of course, you can also use mechanisms such as observe/observable, or other event mechanisms. However, after balancing, I think changelistener is the most suitable for my needs. It is powerful and simple. When we need a swing component to listen to our business objects, we create a subclass of the swing component, which implements the changelistener interface and implements public void statechanged (changeevent e ){...} Method: Build a new model based on the data of the monitored business object to change the display of the swing component. How can Business Objects listen to swing components? I love changelistener, or register a changelistener listener on the swing component. This listener is our business object. However, considering that changelistener is widely used, to avoid other operations on the swing component from initiating this event, I have created a new type of interface called controllerchangelistener by referring to the changelistener interface. Now, the data on the swing component can be synchronized with the data of our business objects! Although the form and business objects are of different types, they seem to be the same object and have the same data! Execution Process of swing programs in MVC Mode1. You can see a GUI composed of swing components. 2. You can perform operations on the interface. 3. The controller of the swing component is triggered. 1) events that may trigger controllerchangelistener and cause business objects to automatically update using the model data of the swing component. 2) updating data of business objects will also stimulate changelistener events on business objects. This will cause all listening business objects to update their model data from their swing components, thus changing the display of swing components. 3) We may execute business-Layer Code for Business Objects for complex business computing. Obtain the value of the new business object. This will also stimulate the changelistener event of the Business Object, update the model of the swing component, and 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 the Controller as an internal class can use all the attributes and methods of the swing program. In my swing multi-threaded programming, I have discussed that swingui processing code and general business code should be allocated to different threads in swing programs. Therefore, in the Controller here, we should use the swingworker helper class to put the Business Code in PublicObject construct (){...} To directly modify the swing component UI. Public voidFinished (){...} Method. In this way, the interactivity, response capability and performance of swing programs will be greatly improved! As you can see, by associating the business object with the form object (the model of the swing component), we only need to "take the business object as the center" in the swing application ". You can manipulate Business Objects and perform business operations. The user interface of the swing program is automatically updated. This is a huge advantage of adopting the "Business Object-centered MVC model!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.