Design Patterns in jive

Source: Internet
Author: User
Design Pattern in jive)
Keywords: Jive, design pattern.
Time: 2001-4-1
Abstract:
Jive is an open-source Forum project, which is a common BBS.
Compared with the huge architecture of J2EE, the company's JSP technology has a very
Refined, suitable for small and medium websites and building their own forum systems. Let's take a look at this article.
Design Pattern Applied in jive ).

Body:
This article does not explain the design pattern in detail. It just takes a look at the design pattern in
So before reading this article, we assume that you
Has a perceptual understanding of its specific application and implementation methods, and is eager to understand its thoughts,
I have used jive. This article will discuss this issue together. Why do I choose jive instead of a new one?

How can I start the example? There are two reasons: 1. Many of us are familiar with something like BBS,
It is clear that BBS has some basic functions. If you design such a web BBS as a designer,
What do you think, and then let's look at how others achieve it. Only by comparing them can you understand the shortcomings of your design and see others.
(2) jive is not very complex and includes a complete implementation
From the bottom layer to the top layer, from the back end to the front-end, there are good documents, which can help us better understand
It.
Here, our jive version uses its developer as the officially released Version 1.0, and its latest version
For 1.21, the structure was slightly modified, mainly adding support for JSP tags. This technology does not belong to me.
In the future, we will have the opportunity to learn from each other.
The design patterns used in jive are three types of design patterns: creation, structure,
Behavior Type-all involved, so that we can fully understand the design model. Let's design it ourselves first,
Using object-oriented thinking, you can easily know that the entire system mainly needs these objects:
1. Forum-a forum, that is, a layout.
2. Thread-a clue, that is, all the replies on the same topic.
3. Message: a message, that is, a post sent by a user.
(In the future, we will use "Post)
4. User: a user in the discussion board.
Well, everything we need is there, and the relationship between them is very complicated. How can we organize them?
Is it easy to expand in line with our ideas? I think everyone has their own ideas. "I can do this ",
"I can design it like this." Let's take a look at how jive works. below is the overall structure:
| ~~~~~~~~~~~~~~~~~~ |
| Skin designer |

| __________________ |
|
| Use
//
| ~~~~~~~~~~~~~~~~~ |
| Interfaces of various objects |
| _________________ |
|
| Implemented
//
| ~~~~~~~~~~~~ |
| Permission control |
| ____________ |
|
| Control
//
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| Various objects for database operations |
| _________________________ |
|
| Connection fetch
//

| ~~~~~~~~~~~~~~~~ |
| Database connection pool |
| ________________ |
(Figure 1)
The following shows the general inheritance of the class:
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| Interface a |
| ___________________________________ |
|
| Implements |
|
| ~~~~~~~~~~~~~~~~~ |
| Proxy a |
| _________________ |
|
|
| ~~~~~~~~~~~~~~~~~~ |
| Database a |
| __________________ |
(Figure 2)
Now, if you have a good understanding of the design pattern, you can
See some familiar things. Please let me make some explanations. The figure above shows the class inheritance relationship, generation

For the four objects mentioned above, interface a represents an interface named.
Interfaces play an important role in Java. Proxy A represents a class named proxya,
Implement interface A. Database A indicates a class named dBA, and implement interface A. But the design mode is
The design model does not show how to better organize the logical relationship between objects,
How can we better expand existing things without making great changes, not just class inheritance.
Another thing to note is that the general principle of the design pattern is interface programming, rather than its specific
Implementation. In this way, we can build a framework. We also need to make a lot of specific programming to truly complete the system.
Next, let's look at the three types of design patterns that jive uses.
1. creational patterns)
This type of design pattern represents the object creation process and the object used by the user.
.
1. In jive, a layer of forumfactory is added to the Forum to implement some control over the forum.
Such as creating a new forum and deleting a forum. This class is actually the portal of the entire system,
Everything done in JSP starts from getting an instance of this class. Its subclass and its relationship are as follows:
Below:
| ~~~~~~~~~~~~~~~~~ |
| Forumfactory | Abstract
| _________________ |
|
| Extends |
|
| ~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~ |

| Forumfactoryproxy | dbforumfactory |
| ____________________ | _________________ |
(Figure 3)
Let's take a look at the process of getting a forumfactory instance:
Factoryforum factory = forumfactory. getinstance (Aauthorization );
The forumfactory instance is obtained. This end user (skin designer) uses its sub-
The instance of forumfactoryproxy class (which involves another mode, which will be mentioned later), but the actual
Dbforumfactory or an instance of a specified class is actually working. The related code is as follows:
Below:
From forumfactory. Java
Private Static string classname = "com. coolservlets. Forum. database. dbfor
Umfaacloud ";
// A specific subclass of the system default forumfactory.
Private Static forumfactory factory = NULL;
Forumfactory. getinstance ()
String classnameprop = propertymanager. getproperty ("forumfactory. classna
Me ")
// You can select other specific sub-classes through the preparation file.
If (classnameprop! = NULL ){
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 normally .");
E. printstacktrace ();
Return NULL;
}
It uses Abstract Factory design patterns to give users a series of related
Object interface without specifying its specific class. That is to say, the JSP written by the skin designer should not
The authorizationfactory in the new dbforumfactory statement. Jive also uses this
Design Mode

2. A good idea in jive is that the content and title of the post can be filtered, such as filtering.
HTML filters some dirty words, highlights the additional code, converts links, and so on.
Function, lucky
Method: (1) control in message. getbody () getsubject (), (2)
The question to be considered is that these filtering operations must be able
It is very convenient to add and delete.
The design method is different for the purpose of, jive is doing this: Take the layout as the main, consider these filters

Saddle
The filter is only valid for the layout to which it belongs. Therefore, jive uses (2), which is not the main one,
The important thing is how to organize these filters. Let's first take a look at the requirement: Dynamic deletion can be added, functions are similar,
The posts show how to create a competitor and show irrelevant information.
. It seems that there is only one target-prototype (prototype) design pattern.
The specific implementation of jive.
| ~~~~~~~~~~~~~~~~~~~~ |
| Forummessage |
| ____________________ |
|
| Implements
|
| ~~~~~~~~~~~~~~~~ | Prototype | ~~~~~~~~~~~~~~~~~~~~~ |
| Forumthread | -----------> | forummessagefilter |
| ---------------- | --------------------- |
| Getmessage () o | clone () |
| ______________ | _ | _____________________ |
|/|
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~ |
| Afilter. Clone () | highlightcode | HTML |
| ________________ | --------------- | ------------- | ......
| Clone () o |

| ___________ | ___ | ___________ | _ |
|
| ~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~ |
| Returns an instance. |
| _______________ |
(Figure 4)
A little simplified. When using jive, these filters are stored in the database and can be dynamically set.
Attribute, more convenient. See some code:
From: dbforumthread. Java
Public forummessage getmessage (INT messageid)
Throws forummessagenotfoundexception
{
Forummessage message = factory. getmessage (messageid );
// Apply filters to message.
Message = Forum. applyfilters (Message );
// Implement it through Forum, because filter is the attribute 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 <filters. length; I ++ ){

Message = filters [I]. Clone (Message );
}
// There may be multiple filters to operate in sequence.
Return message;
}
2. Structural Patterns)
This type of pattern focuses on how to organize classes and objects to form a large structure. It mainly uses inheritance.
Organization interface or implementation.
1. Let's continue to think about the differences between users. There are guest users who can let them see
View, but not post, formal users can post, view their personal information, layout manager (called
Moderators) should be able to control posts, such as adding appropriate tags, revenue serum areas, and even deleting posts.
System Administrators should have higher permissions, such as opening new pages and Deleting Users. How can this problem be solved?
Function? We know that all actual operations in jive are implemented by classes in the database directory,
If you add permission control to the database layer, this layer will not only be bloated, but will also be changed if you want
There are many places to be modified, which are prone to errors. Therefore, you can add a layer above this layer and enter
In this way, we can separate "this should not be done" from "How to do it" to facilitate future modification. In fact, this
It is also an idea of a face object-an object should not bear too much responsibility.
The proxy mode is like the relationship between the manufacturer and the agent.
The purpose of proxy is to provide another object with a proxy to control its access.
The proxy mode always runs through jive, and almost all the objects involved are required. Its structure 2 is shown.
As we already know, forumfactory is the beginning of the entire system. Let's take a look at the code of forumfactory:
From forumfactory. Java

Last of forumfactory. getinstance:
Forumfactoryproxy proxy = new forumfactoryproxy (
Factory,
Authorization,
Factory. getpermissions (authorization)
);
Return proxy;
The factory obtained above is the instance of dbforumfactory. Here we use this instance again
Forumfactoryproxy is encapsulated. Finally, an instance of forumfactoryproxy is returned. That is
The forumfactory used by the JSP skin designer is actually forumfactoryproxy.
Let's take a look at what happened in forumfactoryproxy. The following is an example of a small segment:
The factory in its constructor is an instance of dbforumfactory.
Work. authorization can be considered as an authenticated current user (the actual browser user ),
Forumpermissions can be considered as the permissions of the current user.
Public Forum createforum (string name, string description)
Throws unauthorizedexception
{
// The permission is checked here. If you have the system administrator permission, you can perform corresponding operations,
// Otherwise, an exception is thrown.
If (permissions. Get (forumpermissions. system_admin )){
Forum newforum = factory. createforum (name, description );
Return new forumproxy (newforum, authorization, permissions );

}
Else {
Throw new unauthorizedexception ();
}
}
Public Forum getforum (int id) throws forumnotfoundexception,
Unauthorizedexception
{
Forum = factory. getforum (ID );
Forumpermissions = Forum. getpermissions (authorization );
// Create a new permissions object with the combination of
// 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 unauthorizedexception.
If (! (
Newpermissions. Get (forumpermissions. Read) |
Newpermissions. Get (forumpermissions. forum_admin) |
Newpermissions. Get (forumpermissions. system_admin)
))
{

Throw new unauthorizedexception ();
}
// As described above.
// The Forum obtained here is a dbforum instance, which is the same as forumfactory,
// Return a encapsulated proxy object to control the permissions of forum.
Return new forumproxy (Forum, authorization, newpermissions );
}
All other objects are similar.

3. behavioral patterns)
This type of pattern focuses on algorithms and task allocation between objects. It not only describes objects or classes
And communication between them.
1. Let's take a look at how to get some threads from a forum. Of course, the database is involved here. We
First, design a simplest database table, table name: thread, field threadid int, forumid int, its
We don't care about the content. Then, for example, a method in Forum, getthreads () will return the current forum
All threads. Then you can do this:
Public forumthread [] getthreads ()
{
1. query from the database and retrieve all threadids,
2. Construct a forumthread object based on threadid,
3. An array is returned.
}

This is the easiest way to do this, but is it okay? It depends on the requirements, for example, I want to sort by time,
You have to modify this method, that is, you need to modify the dbforum object. Why not take the thread
What if the operation is taken out separately? The advantage is that the function is independent, making dbforum simpler and conforming to the previous steps.
We mentioned the principle of not putting too many responsibilities on objects. Maybe you will say that if you want to modify it, you don't have
Modify? Where is the same? That's right, but only limited to small systems. If the system is large, you can
Programmers who can perform simple queries in dbforum and some complicated queries are not alone. This involves
There are many changes to be made, but after separation, only a few changes can be done.
Let's look at the problem. Here we will return a group of forumthread objects, and there may be a certain degree of priority between them.
Department, how to do this job? The iterator design mode is an appropriate choice. The iterator mode provides
Instead of having to know their representation, such
And so on.
Let's take a look at the specific implementation of jive. Because Java already has such an interface, iterator connects
Port, so you only need to implement this interface.
From dbforum:
Public iterator threads (){
Return new dbforumiterator (this, factory );
}
From dbforumiterator: (modified)
Public class dbforumiterator implements iterator {
Public dbforumiterator (...)
{
...

}
Public Boolean hasnext () // whether there are other elements
{
...
}
Public object next () // get the next element
{
...
}
...
}
In JSP, you can access:
Iterator threads = aforum. threads ();
While (threads. hasnext ())
{
Forumthread thread = (forumthread) threads. Next ();
Perform some operations.
}
It can be seen that some specific details of threads are blocked by using iterator.
Provides a unified interface. In jive, this design mode is also used by many users,
Multiple la S, multiple clues, and multiple posts need to be implemented.

Summary:
We have discussed the application of the Design Pattern in jive. Of course, it is very simple and
It is superficial and one-sided, but we can understand the design pattern. In fact, the design pattern is absorbing many
People's experience sums up some important and repeated patterns in the design and gives a system name,
Give explanations and comments. The work was first done by four software masters who wrote a book together --
Design Pattern: Elements of reusable object-oriented software.
They are called gof (Gang of Four ).
The design patterns may be consciously and unconsciously used in our actual projects, such as factory
Method mode, abstract mode, Singleton mode, iterator mode, and so on, but the concept is not
Chelaile? The design may not be reasonable, and it is in a state that follows the feeling. I believe there are a lot of experience.
The designers did not come into contact with the design model. Once they come into contact, there will be an unexpected idea.
This is the case. Learning the design model can help us design well. Under the same problem and background,
You can use it directly. Sometimes you don't know which one to choose. You need to analyze the problem further.
Comprehensive trade-offs and a deeper understanding of the design model are required to achieve good results. This is also an improvement.
Process.
For the author, I just came into contact with the design model and had a rough understanding, so I took the liberty to write this article.
Snacks are also a challenge to yourself. please correct me for some mistakes made in the middle. Thank you.
References:
1, design pattern: Elements of reusable object-oriented software,
2. Jive source code

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.