Joomla Extensions Demo

Support

Do not submit a bug report if you need technical support or have questions.

Forums

Post your suggestions ask for help in the community forums

Contact Me

Missing images/links, any comments, suggestions, need help? Contact me

Skype

Need desperately help?
Skype Me™! But dont abuse of it!

Inversion of Control pattern or dependency injection

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 implement a dedicated interface through which they are provided with an object from which they can look up dependencies (for example, additional needed services).

Type 2 Dependencies are assigned through JavaBeans properties (for example, setter methods).

Type 3 Dependencies are provided as constructor parameters and are not exposed as JavaBeans properties.


Category: Design Patterns Creational patterns

Dynamic polymorphic abstract factory

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; } }
Category: Design Patterns Creational patterns

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.5

  • Elements are ordered and comparable
  • Enumeration elements are serializable
  • Enumeration element Name lookup is supported
  • Methods sequencing is included



Source Code

/**
 * Type Safe Enumeration: Colors elements: Red, Blue, Green, Yellow, White
 */
public final class Colors implements Comparable, Serializable {
	/**
	 * The map of enumeration elements to names.
	 */
	private static HashMap nameLookup = new HashMap(5);

	/**
	 * The array of enumeration elements.
	 */
	private static Colors[] ordinalLookup = new Colors[5];

	/**
	 * The "Red" enumeration element
	 */
	public static final Colors Red = new Colors("Red", 0);

	/**
	 * The "Blue" enumeration element
	 */
	public static final Colors Blue = new Colors("Blue", 1);

	/**
	 * The "Green" enumeration element
	 */
	public static final Colors Green = new Colors("Green", 2);

	/**
	 * The "Yellow" enumeration element
	 */
	public static final Colors Yellow = new Colors("Yellow", 3);

	/**
	 * The "White" enumeration element
	 */
	public static final Colors White = new Colors("White", 4);

	/**
	 * The string representation of the enumeration.
	 */
	private final String printName;

	/**
	 * The ordinal value of the enumeration used for comparison purposes.
	 */
	private final int ordinal;

	/**
	 * Create an enumeration element. Prevent instances of this class from being
	 * created externally.
	 * 
	 * @param name
	 *            the name of the enumeration element
	 * @param position
	 *            the ordinal position of the enumeration element
	 */
	private Colors(String name, int position) {
		this.ordinal = position;
		this.printName = name;
		ordinalLookup[position] = this;
		nameLookup.put(name, this);
	}

	/**
	 * Compare two enumeration elements
	 * 
	 * @param arg
	 *            the object to compare this to
	 * @return the difference between the ordinal values
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object arg) {
		return this.ordinal - ((Colors) arg).ordinal;
	}

	/**
	 * Return the string representation of the enumeration.
	 * 
	 * @return the name of the enumeration element
	 */
	public String toString() {
		return printName;
	}

	/**
	 * Resolve the enumeration element.
	 * 
	 * @return the resolved enumeration element
	 * @throws ObjectStreamException
	 *             if the enumeration element could not be resolved.
	 */
	private Object readResolve() throws ObjectStreamException {
		return ordinalLookup[ordinal];
	}

	/**
	 * Return the first enumeration element
	 * 
	 * @return the first enumeration element
	 */
	public static Colors first() {
		return ordinalLookup[0];
	}

	/**
	 * Return the last enumeration element
	 * 
	 * @return the last enumeration element
	 */
	public static Colors last() {
		return ordinalLookup[ordinalLookup.length - 1];
	}

	/**
	 * Return the enumeration element preceding this element
	 * 
	 * @return the enumeration element preceding this element
	 */
	public Colors predecessor() {
		return (this == first()) ? null : ordinalLookup[ordinal - 1];
	}

	/**
	 * Return the enumeration element following this element
	 * 
	 * @return the enumeration element following this element
	 */
	public Colors successor() {
		return (this == last()) ? null : ordinalLookup[ordinal + 1];
	}

	/**
	 * Return the enumeration element with the given name
	 * 
	 * @param name
	 *            the name of the enumeration element to find
	 * @return the named enumeration element
	 */
	public static Colors valueOf(String name) {
		return (Colors) nameLookup.get(name);
	}

}
Category: Design Patterns Creational patterns

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 defining a static field that is dynamically initialized (that is, the field is initialized the first time its value is requested). This has the advantage that the singleton is not allocated unless it is needed, potentially reducing memory usage.



Links

Source Code

/**
 * Represents a singleton.
 */
public class Singleton {
	/**
	 * Holds the singleton instance.
	 */
	private static Singleton instance;

	/**
	 * constructor must be private to avoid automatic creation of default
	 * constructor by the compiler
	 *  
	 */
	private Singleton() {
		super();
	}

	/**
	 * Returns the singleton instance.
	 * 
	 * synchronized to prevent race problem
	 * 
	 * @return the singleton instance
	 */
	public static synchronized Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}
}

Read more: Singleton

Category: Design Patterns Creational patterns

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 {

    /**
     * Construct part of the complex Product.
     */
    public void buildPart();

    /**
     * Construct the Product.
     *
     * @return the constructed product
     */
    public Product getProduct();
}

/**
 * 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 {

    /**
     * Reference to Product being constructed
     */
    private ConcreteBuilder concreteBuilder;

    /**
     * Construct part of the complex Product.
     */
    public void buildPart() {
        // put your code here
    }

    /**
     * Construct the Product.
     *
     * @return the constructed product
     */
    public Product getProduct() {
        return concreteBuilder;
    }
}


/**
 * The ConcreteBuilderClient initialized the Director with a
 * Concrete Bulder to create the Product and gets result from the Builder.
 *
 */
public class ConcreteBuilderClient {

    /**
     * Use the Builder to create the Product.
     */
    public void createProduct() {
        ConcreteBuilderBuilder builder = new ConcreteBuilderBuilder();
        new Director(builder).construct();
        Product product = builder.getProduct();
    }
}


/**
* The class Director manages Product creation using Builder.
*/
public class Director {

    /**
     * Reference to Builder currently used
     */
    private Builder builder;

    /**
     * Create a new Director instance.
     *
     * @param builder the builder which will create the product
     */
    public Director(Builder builder) {
        this.builder = builder;
    }

    /**
     * Construct the Product using the Builder.
     */
    public void construct() {
        builder.buildPart();
    }
}


/**
 * The interface Product defines a complex object that is
 * constructed part-by-part with Builder.

 */
public interface Product {
}






Category: Design Patterns Creational patterns

More Articles...

  1. Abstract factory
  2. Prototype

Donations

Thank You for supporting my work
Subscribe to me on YouTube

Latest Articles

  • In this series of post I will outline some common techniques to help Joomla extensions development. As you know Jooml... ...
  • CedTag  has been updated to version 2.5.3 and correct a lot of bugs and contains some nice features. CedTag is t... ...
  • CedThumbnails has been updated to version 2.5.5 and contains 1 new features for both Joomla 1.7 and Joomla 2.5. For ex... ...
  • CedSmugmug  has been updated to version 2.5.2 and correct some bugs and contains some nice features. CedSmugmug&... ...
  • If you want an extra gigabyte of storage on your Dropbox account, the online cloud service invites you to compete in i... ...

Subscribe

Latest Comments

Popular Posts

rockettheme advertisement

dropbox logo

Help Us & Leave Feedback!

  • Do you have an excellent article idea you would like to read about here? Share it!
  • Do you have some interesting tips how we could improve our site?
  • Something missing here? Help us make this blog a better place, leave feedback!
We would love to hear from you! Be active! Write us now!