JIVE design mode: Jive, Design Pattern. Time: 2001-4-1 Summary: 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, the entire design idea is very refined, suitable for small and medium-sized websites, build their own forum system. This article Let's take a look at the design model applied in JIVE (Design " Pattern). Text: About design mode, this article does not explain in detail, just combined with jive to see the design pattern in an actual project and its overall design ideas. So before reading this article, suppose you design The model has a sense of understanding, some questions about its specific applications and implementations, and is eager to understand their thoughts, and use JIVE. This article will come to explore this problem. Why choose Jive instead of choosing a new example Re-start? There are two reasons: 1. Many of our people are more familiar with the BBS, very clearly some of the basic functions of BBS. If you want to design such a web bbs as designers, what will you think, then look at others is How to achieve, comparison can understand the shortcomings you designed, see the advantages of others can make progress faster. 2, JIVE is not very complicated, including a complete implementation, from the bottom to the top, from the later End to the front end, there is a good document, which can better help us understand it. The version we used to use its developer as a 1.0 version of the official release, its latest version is 1.21, the structure A small amount of changes, mainly adding JSP TAG support, which does not belong to our discussion, and have the opportunity to learn. The design pattern used in Jive, three types of design patterns - creation, structure Type, behavioral - all involved, which can also comprehensively understand design patterns. Let's take yourself, use object-oriented ideas, you can easily know that the entire system mainly needs these objects: 1, forum - A discussion area, that is, a layout .2, thread - a clue, which is related to all the comments in the same topic .3, Message - a message, which is a note sent by a user. (In the future, we use "post" this called method) 4, user - a user, that is, the user of the discussion area. Ok, what we need is in, and the relationship between them is very complicated, how to put They organize very well Can we easily expand? I think everyone has my own ideas, "I can do this so", "I can design like this"
Let's take a look at how jive is doing. The following is its overall structure: | ~~~~~~~~~~~~~~~~~~ || Skin designer || _________________ || || | || Use / / | ~~~~~~~~~~~~~ || ___________________________ || ||| | ~~~ || Permissions Control || ____________ || || | Control / / | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Various objects for the database || _____________________________ || || | Take Connection / | ~~~~~~~~~~~~~~~~ || Database Connection Pool || _________________________________________________ | (Figure 1) Here is the probably inheritance of the class: | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ || ___________________________________ || || || Proxy A | || __________________________________________________________________ ~~~~~~~~~~~ || Database a || ___________________________ | (Figure 2) I saw this, if you have a designed pattern, you can see the pseudo name written by the 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, the general principle of design patterns is for interface programming, not to care about its specific implementation, which is a shelf, but also makes many specific programming to real completion of the system. Below, we will separate from design patterns Three types to see which of the JIVE uses it. First, create a type CREATIONAL PATTERNS This type of design mode is designed, and it is necessary to express the creation process of the object and the relationship between the objects used by the user. 1, and a layer above forum is added over the forum, forumfactory, For some controls for Forum, such as creating a new discussion area, deleting a discussion area, etc. This class is actually the entrance to the entire system. Everything you do in JSP begins with an instance of this class. It's some Subclass and its relationships are as follows: | ~~~~~~~~~~~~~~~~~ || forumfactory | abstract | _________________ || || ~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ || forumFactoryProxy | | dbforumfactory || _____________________________ | (Figure 3) Let's Look at the process of getting a ForumFactory instance: factoryforum factory = forumfactory.getInstance (AAUTHORIZATION); gets the formFactory instance, this end user (SKIN Designer) is used by its subclass forumFactoryProxy instance, (which involves Another mode, will be mentioned later), but actually actually works that DBForumFactory or an instance of a specified class, the relevant code is as follows: from it forumfactory.javaprivate static string classname = "
com.coolservlets.forum.database.DbForumFaactory "; // a concrete subclass of the default system ForumFactory .private static ForumFactory factory = null; ForumFactory.getInstance () String classNameProp = PropertyManager.getProperty (" ForumFactory.className ") // You can select other specific subclasses by formulating files. {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 forumfactory class" classname ". Jive Cannot Function Normal."); E.PrintStackTrace (); Return null;} It uses the Abstract Factory design mode. Give the user an interface to use a series of related objects without specifying its specific classes. That is, the SKIN designer should not write in JSP. The statement like New DBForumFactory. The AuthorizationFactory in Jive also uses this design mode 2. There is a very good idea in Jive. It is to filter the content and title of the post, such as filtering HTML filtering some swearing, for additional The code is highlighted, converted links, etc. If I want to achieve such functions, I am fortunate that the rhythm steel method: (1) Control in Message.getBody () getSubject (), (2) Get a rude in Thread After Essage, it is necessary to consider the problem that these filtrations must be capable of adding deletion. The design method used by the uneven objectives is different. JIVE is doing this: Stri layers, put these filtration It is considered to be the attribute of Angiting, The filter is only valid for the layout of its belongings, so it is used in JIVE. This is not the main thing. It is important to organize these filters. Let's take a look at the demand: You can dynamically add delete, function similar How to create a specifically created a lot of posts, how to express it. It seems that the target is only one - prototype design mode. Take a look at the specific implementation of Jive. | ~~~~~~~~~~~~~~~~ ~~~~~~ || ForumMessage || ____________________ ||| IMPLEments || ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ || FORUMTHREAD | ----------->
| Forummessagefilter || ------------------------------- || GetMessage () O | | clone () || ______________ | _ | | _____________________ || / || ~~~~~~~~~~~~~~~~~~~ | | ~~~~~~~~~~~~~ || Afilter.clone () | | highlightcode | | html || ________________ | --------------- | | ------------- | ... | clone () o | | clone () o || ___________ | ___ | | ___________ | _ ||| ~~~~~ ~~~~~~~~~~ | | ~~~~~~~~~~~~~~~ || Return to an instance | | Return an instance || ______________________ | (Figure 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.javapublic ForumMessage getMessage (int messageID) throws ForumMessageNotFoundException {ForumMessage message = factory .getMessage (MessageID); // Apply Filters to message.Message = forum.applyfilters (message); // is implemented via FORUM, because Filter is the properties of Forum, // Thread can only access it through Forum interface. Return Message } From: dbforum.javapublic 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 suitable in jive). The purpose of Proxy is to provide another object to control the access to it. Theproxy mode has always been always through the JIVE, almost all the objects involved. Its structure is shown in Figure 2. From the previous side, ForumFactory is the beginning of the entire system. Take a look at the final: from itumfactory.javaForumFactory.GetInstance () Final: ForumFactoryProxy proxy = New ForumFactoryProxy (Factory, Authorization, Factory. getPermissions (authorization)); return proxy;.. obtained above are examples DbForumFactory the factory, this example also used here encapsulate ForumFactoryProxy ForumFactoryProxy last return an instance of the designer is to say jsp skin is actually used ForumFactory ForumFactoryProxy Then look at what happened in ForumFactoryProxy, the small fragment is the example: Factory in its constructor is an instance of DBforumFactory, which is made by it. Authorization can be considered a certified current user (finger The actual browser user), forumpermissions can be considered to be the privilege of the current user. Public forum createforum (String name, string description) throws unauthorizedException {// This is checked here, with system administrator privileges, can be performed The corresponding operation, / / otherwise throw an exception. IF (permissions.get (forumpermissions.system_admin) {forum newforum = factory.createforum (name, description); Return New Fo rumProxy (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 ); // crete a new permissions Object with the combination of the // permissions of this object and temppermissions.forumpermissions newlermissions = new forumpersions; // 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 ( Forumpermissions.System_admin))) {throw new unauthorizedException ();} // That is the .// 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 mode is concerned about the algorithm and The task assignment between the objects. It is not just the design mode of the object or class, but also the 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 simpler, in line with the principle of liability for the responsibility of the object we mentioned before. Maybe you will say, if you want to modify, Not all modified? Where is it, this is true, but it is limited to a small system, if the system is large, then it is possible to make a simple query in dbForum and some of the more complex queries is not one. People, this involves more places that need to be changed, but after separation, only one person can change very little place. Go back and look at the problem, here to return a group of FORTHREAD objects, and they may Have a certain relationship, how to do this? Itrator design mode is a suitable choice. Itrator mode provides a method of continuously accessing a large group of objects, without knowing their performance form, such as what way to sort, etc. Wait. Ok, 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 DBforumItemrator (this, factory } From dbforumiterator: (Do a change) PUBLIC CLASS DBFORUMITERATOR IMPLEMENTS ITERATOR {public dbForumiterator (...) {...} public boolean Hasnext () // Is there any element {...} public object next () // Get the next element {...} ...} So you can access this: iterator threads = Aform.Threads (); while (threads.hasnext ()) {foruMThread thread = (foruMthread) threads.next );