JIVE design mode

zhaozj2021-02-08  240

JIVE design mode

Massle: JIVE is an open source forum project, which is the BBS we are using, using Sun's JSP technology, compared to J2EE this huge architecture, its entire design thoughts are very refined. Applicable to small and medium-sized websites, build their own forum system. This article we will take a look at Design Pattern. About design mode, this article does not explain in detail, just combined with jive To look at the application of design patterns in an actual project and its overall design ideas. So before reading this article, suppose you have a sense of sensibility in design mode, some questions about its specific applications, and implementation, and desire to understand Its thoughts and uses Jive. This article will come to explore this problem. Why choose Jive instead of choosing a new example again? There are two reasons: 1, many of us is more familiar with BBS, very Clear some of the basic functions of BBS, if you use a designer to design such a web bbs, what will you think, then look at how others achieve, there is comparison to understand the shortcomings of yourself, see the advantages of others Advance faster. 2, Jive is not very complicated, including a complete implementation, from the bottom to the top, have a good document from the back to the front end, all of which can better help us understand It. Here we use the version of the JIVE uses its developer as a 1.0 version of the official release, its latest version of 1.21, has a small amount of change in its structure, mainly adding JSP TAG support, this technology does not belong to our discussion Range, there will be a chance to learn together. The design patterns used in Jive, three types of design patterns - creation, structural, behavior - all involve, which can comprehensively understand design patterns. Let us first design, use object-oriented ideas, it can be easy to know that the entire system mainly needs these objects: Forum - a discussion area, that is, a layout. Thread - a clue, that is, the same All of the themes. Message - a message, which is a note sent. (In the future, we use "post" this called method) User - a user, that is, the user of the discussion area Ok, what we need is all in it. The relationship between them is very complicated. How can I organize them in line with our thinking? I think everyone has my own ideas. "I can do this." So doing this, "I can design like this. "

Let's take a look at how Jive is doing. The following is its overall structure: | ~~~~~~~~~~~~~~~~~~ | | SKIN designer | | __________________ | | | | | Use / / | ~~~~~~~~~~~~~~~~~ | | Interfaces of various objects | | _________________ | | | | | is implemented / / | ~~~~~~~~~~ ~~~ | | access control | | ____________ | | | | | control / / | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | Various objects for the database | | ______________________________________________ | ~~~~~~~~~~~~~~~~ | | Database Connecting Pool | | ____________________________________ | (Figure 1) Below is the probably inheritance of the class: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | Interface A | | ____________________________________________________ | | | | ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ | | Database A | | __________________________ | (Figure 2) See here, if you know the design pattern, you can see it from the false name written above. Some familiar things. Please let me do some explanations. The above figure indicates the inheritance relationship of the class, and the four objects mentioned above, Interface A indicates an interface named A, I believe everyone is not Unfamiliar, the interface has an important role in Java. Proxy A represents a class named Proxya, implements a interface. Database A means a class named DBA, implement A interface. But the design mode is not reflected, design mode What is going to be better organizes the logical relationship between objects, how to better expand existing things without need to make a lot of changes, not just the inheritance of the class. There is a little need to explain It is that the general principle of design patterns is for interface programming, and not to care about its specific implementation, so that a shelf is required, you need to make many specific programming to really finish In the system. Below, we look at the three types of design patterns. 1. Creational Patterns This type of design mode is designed, and what is the object's creation process and The relationship between the objects used by the user. On the other, a layer is added, and the ForumFactory is implemented to implement some of the control, such as the creation of a new discussion area, delete a discussion area. This class is actually It is the entrance to the entire system. Everything you do in JSP begins with an example of this class. Some subclasses and its relationships are as follows: | ~~~~~~~~~~~~~~~ ~~ | | forumfactory | Abstract | __________________ | | | | | | ~~~~~~~~~~~~~~~~ ~~~~~~~ | | ForumFactoryProxy | | DbForumFactory | | ____________________ | | _________________ | (3) we get a look at ForumFactory example of the process: FactoryForum factory = ForumFactory.getInstance (aAuthorization);

Just get the find example, this end user (SKIN Designer) is used by its subclass forumfactoryProxy instance, (which involves another mode, which will be mentioned later), but actually actually doing actual work Is the case of DBForumFactory or a specified class, the relevant code is as follows: from final classname = "com.coolservlets.forum.database.dbforumfaactory"; // System default ForumFactory a specific subclass . private static ForumFactory factory = null; ForumFactory.getInstance () String classNameProp = PropertyManager.getProperty ( "ForumFactory.className") // can select other specific subclass if (classNameProp = null!) by formulating file {className. = ClassNameProp;} try {// load the class and create an instance. Class C = class.Forname (classname); factory = (forumfactory) c.newinstance ();} catch (exception e) {system.err.println "Failed to Load Forum Factory Class" ClassName ". Jive Cannot Function Normal."); E.PrintStackTrace (); return null;} It is using the Abstract Factory design mode. Give the user a series of related The object's interface does not need to specify its specific classes. That is to say, the statement in the JSP written by the SKIN designer should not appear. The AuthorizationFactory in Jive also uses this design pattern. JIVE has a very good The idea is that the content and title of the post can be carried out. Filter, such as filtering HTML filtering some swearing, highlighting the additional code, conversion link, etc. If I want to achieve such functions: (1) in Message.getBody () getSubject () The control is performed, (2) After the Message is obtained in Thread, it is necessary to consider the problem that these filtrations must be capable of adding deletion. The design method used by the non-objective goals is different. JIVE is Doing this: Stri layers, regarding these filters as the attribute of Ansiting, the filter is only valid for the layout of it, so it is used in Jive (2), which is not mainly, important to these filtering How to organize. Let's take a look at the demand: You can dynamically add delete, the function is similar, the post display shows how it is created, how to express it. It seems that the target is only one - prototype (prototype) design mode. Take a look JIVE specific implementation. | ~~~~~~~~~~~~~~~~~~~ | | ~~~~ | Prototype | ~~~~~~~~~~~~~~~~~~~~~ | | ForumThread | --------->

ForumMessageFilter | | --------------------------------- | | GetMessage () O | | clone () | | ______________ | _ | | _____________________ | | | ~~~~~~~~~~~~~~~~~ | | ~~~~~~~~~~~~~~ | | Afilter.clone () | | HIGHLIGHTCODE | | HTML | | ________________ | --------------- | | ------------- | ... | clone () o | | Clone () o | | ___________ | ___ | | ___________ | _ | | | ~~~~~ ~~~~~~~~~~ | | ~~~~~~~~~~~~~~~ | | Return to an instance | | Return an instance | | ____________________________________ 4) . Jive made a little simplified when using a database and the presence of these filters may be dynamically set properties, look at some more convenient codes:. From: DbForumThread.java public ForumMessage getMessage (int messageID) throws ForumMessageNotFoundException {ForumMessage message = Factory.getMessage (MessageID); // Apply Filters to Message. Message = forum.applyfilters (message); // Implemented through Forum, because Filter is the properties of Forum, // Thread can only be accessed through the Forum interface. Return Message;} from: dbforum.java public forumMessage Applyfilters (forummessage message) {for (int i = 0; i

Separate, it is good to be modified later. In fact, this is also an idea of ​​an object - an object does not bear too much responsibility. This method is called Proxy (proxy) mode in design mode. Good comparison manufacturer and agent Relationship. (Of course, this metaphor is not appropriate in jive). The purpose of Proxy is to provide another object to control the access to it. The Proxy mode has been always through the JIVE always, and the object involved is required. Its structure is shown in Figure 2. From the previous side already know, ForumFactory is the beginning of the entire system. Take a look at the final: from forumfactory.java forumfactory.getInstance () Last: ForumFactoryProxy Proxy = New ForumFactoryProxy (Factory, Authorization, Factory) .GETPERMISSIONS (Authorization); Return Proxy; Previously obtained Factory is an instance of DBforumFactory, where this instance is also encapsulated in the ForumFactoryProxy. Finally returns an instance of ForumFactoryProxy. That is to say, the ForumFactory used by JSP Skin is actually actually FORUMFAACTORYPROY. Then look at what happened in ForumFactoryProxy. The small fragment is an example: Factory in its constructor is an instance of DBforumFactory, which is a specific job. Authorization can be considered a certified current user ( Refers to the actual browser user), and ForumPERMISSIONS can be considered to be the current user's permissions. Public forum createforum (String name, string description) throws unauthorizedException {// Here is checked, with system administrator privileges, Perform a corresponding action, / / ​​otherwise thus thus throwing an exception. IF (permissions.get (forumpermissions.system_admin) {forum newforum = factory.createforum (Name, Descri ption); return new ForumProxy (newForum, authorization, permissions);} else {throw new UnauthorizedException ();}} public Forum getForum (int ID) throws ForumNotFoundException, UnauthorizedException {Forum forum = factory.getForum (ID); ForumPermissions forumPermissions = forum.getPermissions (authorization); // Create a new permissions object with the combination of the // permissions of this object and tempPermissions ForumPermissions newPermissions = new ForumPermissions (permissions, forumPermissions).;

// Check and see if the user has READ permissions. If not, throw an // an UnauthorizedException. If (! (NewPermissions.get (ForumPermissions.READ) || newPermissions.get (ForumPermissions.FORUM_ADMIN) || newPermissions.get ( Forum_Admin))) {throw new unauthorizedException ();} // The above. // The forum here is a DBFORUM instance, like the ForumFactory, // Return to a packaged agent object, come to Forum Permissions control. Return New ForumProxy (forum, authorization, newpermissions);} All Other objects are similar. Third, the mode of behavioral pattern (BehaviORAL PATTERNS) This type of behavior is the algorithm and The task assignment between the objects. It is not just the design mode of the object or class, but also communication mode between them. 1, look at how to get some Thread from a Forum. Of course, here should involve the database We first design a simple database table, table name: thread, field threadid Int, forumID int, other content we don't care. This then, for example, a method in Forum, getThreads () to return all of the current Forum all Thread. And then You can do this: public forumthread [] getthreads () {1, query from the database, remove all ThreadID, 2, constructed the ForuMThread object according to the ThreadID, 3, return an array. Ok, I have to look at the demand. For example, according to time sorting, I have to modify this method, that is, I need to modify the DBForum object. Why don't you take this operation separately? This kind of benefit is the function independentization Make DBFORUM easier, in line with the burden of the object we mentioned before. Too many responsibilities. Maybe you will say, if you want to modify it, don't you have to modify it? What is the same, this is right, but it is limited to a small system, if the system is big, then you may do it Simple queries in dbForum and some of the more complex queries are not a person, which involves more places that need to be changed, but after separation, only one person can change very little place. Tall back Look at the problem, here you have to return a group of ForuMThread objects, and there may be some ancestors, how to do this? Itrator design mode is a suitable option. Itrator mode provides a continuous access to a large group of objects Method, do not need to know their expression, such as what to sort, etc. Well, let's take a look at the specific implementation of JIVE. Since Java itself has such an interface, Iterator interface, so as long as this interface can be . From DbForum: public Iterator threads () {return new DbForumIterator (this, factory);} From DbForumIterator: (made changes) public class DbForumIterator implements Iterator {public DbForumIterator (...) {...} public boolean hasNext ( ) // Is there any element {...} public object next () // get the next element {...} ...} The JSP can be accessed in the JSP: itute threads = Aform.Threads ();

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

New Post(0)