Singleton session pattern
Posted by:
Artem Yegorov On April 29, 2004 @ 01:43 PM
Single-instance session mode enhances code readability, and can fully use Java inheritance and abstraction definition and access to variables in the single instance application and framework, such as Struts. In Struts, every behavior is initialized once and is reused in Context, (until restarting and reloading), so all these variables must be passed to a custom behavior, this behavior is necessary Define local to pass to a method in which it is executed. We can create a session class where there is a getobject (object key) and setObject (Object Key, Object Obj) family, and define all necessary variables and objects to use two methods, while delivering unique session instances to all necessary methods. .
public class StrongTypedSingletonSession {private String a = null; private String b = null; private String c = null; public void setA (String a) {this.a = a;} public String getB (String b) {this.b = b PUBLIC STRING GETC (String C) {this.c = C;} public string geta () {Return this.a;} public string getb () {Return this.b;} public string getc () {Return THIS. c;}} In this example you know before hand what objects you need to set and access, including those object's types.It could also be weakly-typed, eg.:public class WeaklyTypedSingletonSession {private Map objects = new HashMap (); public void setObject (String key, Object obj) {synchronized (this.objects) {this.objects.put (key, obj);}} public Object getObject (String key) {Object obj = null; synchronized (this.objects) { Obj = this.Objects.get (key);} Return Obj;}} This example allows you to define any number of objects that determine the special object in the program.
This mode helps me a lot of time when I use that Struts framework, so my framework module can be accessed on the Web and benefit from Struts, and my framework itself is in any specified specific access method. However, the program plug-in is instantiated to use requests and response objects, which are available in Struts Action, and I will be defined. Contains with other objects Action Mapping and Action Form In a Weakly Tape Single Us-Example Session, the class guarantees flexibility and type abstraction.