View RSS Feed

Design Patterns

Design patterns in Java.

  1. Builder Pattern - Specific problems and implementation

    by , 03-09-2012 at 07:16 PM
    Builder & the Abstract Factory

    Builder design pattern and the abstract Factory pattern have a lot of similar things. This is the reason why it is necessary to come up with certain different condition b/w situations, when we use one of them. For abstract factory, factory methods are used by the client so that to make its own objects. In case of builder, the builder class gets instructions regarding the way to create object. However in which way the class will be put together depends ...
  2. Builder Pattern - Applicability & Examples

    by , 03-09-2012 at 07:15 PM
    Builder pattern could be used when:
    • Creation algorithms of complexed objects are not dependent upon parts that compose the objects.
    • System allows the representation of many different objects.


    Vehicle Manufacturer

    Consider an example of vehicle manufacture that creates bicycle, a car, a scooter and a motorcycle. In such case, builder would be the Vehicle Builder. Interface is being specified to build any kind of vehicle in the above list by using similar ...
  3. Builder Pattern - Implementation

    by , 03-09-2012 at 07:13 PM
    Factory builder pattern is used by the builder design patter so that to make a decision which concrete class for initiation to build the desired object type.

    In this pattern, the participant classes are:
    • An abstract interface is specified by the builder class to create the product object parts.
    • ConcreteBuilder creates and puts the product parts together by doing the implementation of the builder interface. It also keeps the proper representation track that has been created
    ...
  4. Builder Pattern

    by , 03-09-2012 at 07:12 PM
    Complexity of the objects as well as classes increases along with the complexity of an application. Complex objects basically are manufactured of those parts that are produced by the various other objects which require special attention while building. A proper mechanism is needed by the application to build the complex objects, independent of those that build object. If you are also facing the same problems then try to use the Builder design pattern.

    Such patterns permit the client ...
    Categories
    Builder Pattern
  5. Class Registration - using reflection

    by , 03-09-2012 at 07:11 PM
    Registration of the new product class could be done to the factory in case you use reflection (e.g in .NET languages or Java), without making any change in factory itself.Without being aware of the object type for creation of the objects in factory class, a map is kept b/w class type and productID. In this particular case, when new product gets added to the application then it shall be made registered to factory. No change is required in factory class code for this kind of operation.
    ...
  6. Factory Pattern Applicability & Examples

    by , 03-09-2012 at 07:09 PM
    Factory pattern is most widely used pattern.

    E.g, graphical applications do their work along with shapes. In implementations, shapes are products and drawing frameworks are the clients. An abstract shape is responsible for the derivation of the shapes. Shape class defines the move operations and draw that shall be implemented by concrete shapes. Now let us assume that a particular command has been selected from menu so that to make a new circle. Shape type is being received by the ...
  7. Factory Pattern

    by , 03-09-2012 at 07:07 PM
    Most widely used design pattern is the Factory design pattern in modern programming languages, for example C# and Java. It comes in many different implementations and variants. If you are seeking them you will probably find out certain references regarding GoF patterns: Abstract Factory & Factory Method.
    Commonly used factory patterns are defined here and also you might check out the similar original factory method patterns.
    • Without exposure of the instantiation logic, objects
    ...
    Categories
    Factory Pattern
  8. Serialization

    by , 03-09-2012 at 06:50 PM
    If the java.io.Serializable interface is being implemented by the Singleton class ( when serialization and deserialization of singleton takes place more than one time) then number of Singleton instances will be created. For avoiding this, implement the readResolve method.
    Java Code:
    	public class Singleton implements Serializable {
    		...
    
    		// This method is called immediately after an object of this class is deserialized.
    		// This method returns the singleton instance.
    ...
    Categories
    Singleton Pattern
  9. Protected constructor

    by , 03-09-2012 at 06:49 PM
    For allowing the singletons sub-classing, there is a possibility to use the protected constructor. Such technique consists of two main drawbacks which makes the inheritance of a singleton impractical.

    • If constructor is protected then instantiation of the class can be done by calling the constructor from some other class present in the similar package. Creation of the singletons separate package can avoid it.
    • For using the derived class, change all getInstance calls present
    ...
  10. Early instantiation using implementation with static field

    by , 03-09-2012 at 06:48 PM
    The singleton object gets instantiated in case the class gets loaded & is not when it is used first, in the given implementation. This happens because the instance member has been declared static. Thats why there is no need of synchronization of any part of the code, in such cases. Class gets loaded once when the uniquity of the object is guaranteed.
    Java Code:
    //Early instantiation using implementation with static field.
    class Singleton
    {
    	private static Singleton
    ...
  11. Lazy instantiation using double locking mechanism

    by , 03-09-2012 at 06:45 PM
    The standard implementation is a thread-safe implementation. However, it is not the best implementation as when you talk about performance, the synchronization is quite expensive.When the object gets initialized, there is no need to check the synchronized method getInstance for synchronization. When singleton object has already been created, just return it without making use of synchronized block.

    When for the first time getInstance() method is called, singleton instance is created. ...
  12. Singleton Pattern Applicability & Examples

    by , 03-09-2012 at 06:43 PM
    Singleton Pattern according to its definition shall be only used if there is exact one instance of class and also when its accessibility is given from a global access point to the clients.Few real situations are given here where singleton is used:

    Logger Classes

    In design of the logger class the singleton pattern is used. Such classes are basically implemented as singletons so that to provide a global access point for all the components of application. For this purpose ...
  13. Singleton Pattern Implementation

    by , 03-09-2012 at 06:39 PM
    A static member is involved in Singleton class, a static public method and a private constructor while doing implementation. This makes a reference to be returned to the static member.

    A getInstance operation is being defined by the Singleton pattern which gives exposure to the unique instance. This unique instance is accessible by the clients. Responsibility of the getInstance() is to create class unique instance if it is not built uptill now and also to return the instance.
  14. Singleton Pattern

    by , 03-09-2012 at 06:38 PM
    Singleton pattern is considered to be a design pattern in software engineering which is used for the implementation of the mathematical singletons concepts. It is done by making restrictions at the instantiation of one class to 1 object. It is more beneficial in those cases when just 1 object is required to make the coordination of the actions, across the system. This concept is basically generalized to all those systems which are operating efficiently when 1 object is peresnt, or when instantiation ...
  15. Construction for Your Java Classes with Many Parameters

    by , 11-29-2011 at 01:10 AM
    Traditionally when programmers have to create a constructor that has lots of parameters, some that are required and other that are optional, they often will either build several constructors or use the telescoping constructor pattern. For those of you not aware, the telescoping constructor pattern is where you provide constructors for the class with the required parameters, and then others constructors that cover the other optional parameters. So depending on the number of parameters and whether ...

    Updated 11-30-2011 at 02:06 PM by Design Patterns

    Categories
    Builder Pattern