Wednesday, 23 April 2014

Java Design Patterns Links Page



  1. Singleton
  2. Factory
  3. AbstractFactory
  4. Builder
  5. Prototype
  6. Adapter
  7. Composite
  8. Proxy
  9. Fly Weight
  10. Facade
  11. Bridge
  12. Decorator
  13. Template Method
  14. Mediator
  15. Chain Of Responsibility
  16. Observer
  17. Strategy
  18. Command
  19. State
  20. Visitor
  21. Iterator
  22. Interpreter
  23. Memento

All Design Patterns

I have referred some blogs and videos and finally list out the  DESIGN PATTERNS as follows.

All Categories Of Design Patterns 

The following are the different categories in design patterns and each category have number of Design Patterns to deal with.
  • Creational Design patterns
  • Structural Design Patterns
  • Behavioural Design patterns
  • J2EE Patterns
  • Domain Logic Patterns
  • Data Source Architectural Patterns    
  • Object Relational Behavioral Patterns
  • Object Relational Structural Patterns
  • Object Relational Metadata Mapping Patterns
  • Web Presentation Patterns
  • Distribution Patterns
  • Concurrency Design Patterns
  • Base Pattern
  • Session State Patterns                      #:-s

Singleton Design Pattern

Singleton Design Pattern

When?
  • Sometimes it's appropriate to have exactly one instance of a class and provide a global point of access to it.
  • EX:Lets Assume that there is a product store and It would like to maintain the products of their store in a List.We dont know how many stores access this list and update the list details.In this case,if we create multiple instances if store A update one instance,Store B doest not know the new list if it  access another instance of this class.So to avoid this,we will create only one instance and allow all the stores access the same one instance.
  • Note:In this Pattern only one instance is created in  entire JVM for this class.If we have in distributed system ,for every JVM container there will be one instance.If we have two JVMs in our machine, we have 2 instances per each JVM.

How??

step1:  Create a class and restrict the class as no body can access the class.To do this Mark the constructor as Private.

            public class SingletonDemo {
private SingletonDemo () {}
}

step2:As we mark the constructor as private ,No other class create a instance of this class.So We need to create a instance of this class and write a method to return the instance.Mark this method as static.so that other classes can access this method.


 public class SingletonDemo {

         private static SingletonDemo instance = null;
private SingletonDemo () {}

public static SingletonDemo getInstance() {
                    
                if (instance == null) {
                    instance = new SingletonDemo();
                }
                    
        return instance;
    }
}


Done....


Note:This simple singleton pattern is not working in multithread environment.Assume that two thread are actually call this method at a time it will call the if block and create two instances (prob)

Solution:Mark themethod as synchronized