This package contains a dynamic polymorphic factory...

  •  New class can be add dynamically to the factory... even during runtime (dynamic)
  • Factory methods are in a separate class as virtual functions. (polymorphism)
  • Different types of factories can be subclassed from the basic factory.. (abstract)
  • Useful iin case of licence problem, since Concrete classes are created at runtime, and only need to reside in classpath (If they are not present the code still compile). Below, the example show multiple authorization and autorisation scheme, that can be switche on/off very fast.
  • Factory can be driven with an external condition (properties, registry ....)
 
Notice also that the specific concrete classes are dynamically loaded on demand...(class.forname())


Source Code

/**
 * Creation date: (7/19/2002 2:50:45 PM)
 * 
 * @author: Cedric Walter
 */
public interface AuthentificationIF {

        public boolean Authentificate(HttpServletRequest req,                        HttpServletResponse resp);        public boolean hasAutorisation(HttpServletRequest req,                        HttpServletResponse resp);}

public abstract class AuthentificationA implements AuthentificationIF {
/**
  * AuthentificationA constructor comment.
  */
        public AuthentificationA() {
                super();        }

        
/**
  * Authentificate method comment.
  */
        
public abstract boolean Authentificate(                        
javax.servlet.http.HttpServletRequest req,                        
javax.servlet.http.HttpServletResponse resp);
}

abstract class AuthentificationFactoryA {

        private static java.util.Map factories = new java.util.HashMap();        
/**
  * ComputeFactory constructor comment.
  */
        public AuthentificationFactoryA() {
                super();        }

        public static void addFactory(String id, AuthentificationFactoryA f) {
                factories.put(id, f);        }

        public static final AuthentificationIF createAuthentification(String id)                        
throws FactoryCreationException { if (!factories.containsKey(id)) { try { // Load dynamically Class.forName(id);
}
catch (ClassNotFoundException e) { throw new FactoryCreationException(id);
} // verify that it has been stored if (!factories.containsKey(id))
throw new FactoryCreationException(id);
} return ((AuthentificationFactoryA) factories.get(id)).getAuthentification();
} protected abstract AuthentificationIF getAuthentification();} /** * concrete class of the abstract factory */ public class MyAuthentificationFactory extends AuthentificationFactoryA { public MyAuthentificationFactory() { super(); } /** * not use since it is subclass */ protected AuthentificationIF getAuthentification() { return null; } } /** * @author: Cedric Walter */ public class NimiusAuthentification extends AuthentificationA implements AuthentificationIF { private static class Factory extends AuthentificationFactoryA { protected AuthentificationIF getAuthentification() { return new NimiusAuthentification(); } } static { AuthentificationFactoryA.addFactory("com.waltercedric.gof.pattern.factory.NimiusAuthentification", new NimiusAuthentification.Factory()); }
/**
  * Local constructor comment.
  */
        public NimiusAuthentification() {
                super();        }

        
/**
  * Authenficate method comment.
  */
        public boolean Authentificate(javax.servlet.http.HttpServletRequest req, 
javax.servlet.http.HttpServletResponse resp)
{ //do some stuff return true; } public boolean hasAutorisation(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
{ //do some stuff return true; } } /** * @author: Cedric Walter */ public class NoAuthentification extends AuthentificationA implements AuthentificationIF { private static class Factory extends AuthentificationFactoryA { protected AuthentificationIF getAuthentification() { return new NoAuthentification(); } } static { AuthentificationFactoryA.addFactory( "com.waltercedric.gof.pattern.factory.NoAuthentification", new NoAuthentification.Factory()); } /** * Local constructor comment. */ public NoAuthentification() { super(); } /** * Authenficate method comment. */ public boolean Authentificate(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) { return true; } /** * hasAutorisation method comment. */ public boolean hasAutorisation(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) { return true; } } /** * @author: Cedric Walter */ public class ObtreeAuthentification extends AuthentificationA implements AuthentificationIF { private static class Factory extends AuthentificationFactoryA { protected AuthentificationIF getAuthentification() { return new ObtreeAuthentification(); } } static { AuthentificationFactoryA.addFactory( "com.waltercedric.gof.pattern.factory.ObtreeAuthentification", new ObtreeAuthentification.Factory()); } /** * Local constructor comment. */ public ObtreeAuthentification() { super(); } /** * Authenficate method comment. */ public boolean Authentificate(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) { return true; } /** * hasAutorisation method comment. */ public boolean hasAutorisation(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp) { return true; } }

You might like also

No Thumbnail was found
You do not create your objects but describe (using cofiguration file) how they should be created and wired together in code. A container (for ex: in case of Spring framework, the IOC container) is then responsible for hooking it all up. In a typical IOC scenario, the container creates all the objects, wires them together by setting the necessary properties, and determines when methods will be invoked. The three implementation pattern types for IOC are: Type 1 Services need to …
5019 Days ago
Type Safe Enumeration
Create a class that approximates an equivalent Pascal enumeration or C enum. A specific enumeration will be represented by a class with specific instances corresponding to each element of the enumerations and public static final fields to access the instances.Existing in #Java Tiger 1.5Elements are ordered and comparableEnumeration elements are serializableEnumeration element Name lookup is supportedMethods sequencing is includedSource Code/** * Type Safe Enumeration: Colors elements: Red, Blue, Green, Yellow, White */ public final class Colors implements Comparable, Serializable /** …
5476 Days ago
Singleton
Ensure a class only has one instance, and provide a global point of access to it. Statically Initialized The singleton class is implemented by defining a static field that is statically initialized (that is, the field is initialized when the class is initialized). This has the advantage that invocations of the method used to access the singleton do not incur the overhead of checking whether or not the instance has been created. Dynamically Initialized The singleton class is implemented by …
5476 Days ago
Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.Source Code/** * Declares interface for cloning itself. * @role __Prototype */ public interface Prototype /** * Implements an operation for cloning itself. */ public class ConcretePrototype1 implements Prototype public Prototype createCopy() } /** * Implements an operation for cloning itself. */ public class ConcretePrototype1 implements Prototype public Prototype createCopy() } …
5483 Days ago
Abstract factory
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.Source Code/** * Abstract factory declares an interface for operations that create abstract * product objects. * * @role __Factory */ public interface AbstractFactory /** * Abstract factory declares an interface for operations that create abstract * product objects. * * @role __Factory */ public interface AbstractFactory /** * Abstract product - an interface for a type of Product object. * * @role __Product * …
5483 Days ago
Builder
Separate the construction of a complex object from its representation so that the same construction process can create different representations.Source Code /** * The interface Product defines interface to create parts of the * Product. */ public interface Builder /** * The ConcreteBuilder is the product of a concrete builder. * */ public class ConcreteBuilder implements Product /** * The ConcreteBuilderBuilder creates and assembles parts of * the Product. * */ public class ConcreteBuilderBuilder implements Builder /** * Construct the …
5483 Days ago