Thursday 24 April 2014

Observer Design Pattern

What???
  • Observer pattern comes under Behavioral Design Pattern ,As this patterns deals with How the objects and classes sends messages to each other....
  • In observer design pattern multiple observer objects registers with a subject for change notification. When the state of subject changes, it notifies the observers. Objects that listen or watch for change are called observers/listeners and the object that is being watched for is called subject.
  • Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.



Applicability

The observer pattern is used when:
  • The change of a state in one object must be reflected in another object without keeping the objects tight coupled.
  • The framework we are writing needs to be enhanced in future with new observers with minimal changes.
Some Classical Examples:

Model View Controller Pattern - The observer pattern is used in the model view controller (MVC) architectural pattern. In MVC the this pattern is used to decouple the model from the view. View represents the Observer and the model is the Observable object.

Event management - This is one of the domains where the Observer patterns is extensively used. Swing and .Net are extensively using the Observer pattern for implementing the events mechanism.

Example:

  • Lets assume that 3 or 4 users want to buy a product in one website called flipcart.
  • But currently that product is not available/out of stock state and It provide a option like NotifyMe When Product available.So These users will register into this site for notification.
  • After few days The Product is available in that website,so Perticular Notify method will send the notification to the all Registered users via email that the product they requested is availableNow and you can shop now.

    Note that in this example subject : Product Information,Observers:Users




Implimentation :


                     will update soon.......

Proxy DesignPattern

What Is PROXY?


  • Someone who takes the place of someone else is known as a proxy.
  • In Proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern.
  • Proxy design pattern allows you to create a wrapper class over real object. Wrapper class which is proxy, controls access to real object so in turn you can add extra functionalities to real object without changing real object's code.


  • Example:
  1. If a Person has account in a bank and he want to withdraw some 5000 from bank.If it is direct process ,He has to go to bank and fill the withdraw form and the employee check for the account balance and then process the withdraw request.
  2. Second Process:,the Person directly go to an ATM and enter pin and amount ,the machine checks the account details and send info to bank .and process the request.Here ATM is not the Real bank or real object to work on.
When?

Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer. Here are some situations when proxy pattern is applicable.

  1. remote proxy provides a local representative for an object in a different address space providing interface for remote resources such as web service or REST resources.
  2. virtual proxy creates expensive object on demand.
  3. protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
  4. smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  5. Adding a thread-safe feature to an existing class without changing the existing class's code.

How??


will update soon...


                 


Factory Design Pattern

What??

  • In this,we create object without exposing the creation logic to the client and refer to the newly created object using common interface 
  • At run time ,we get the object of the type based on the parameter we pass.
  • Actually we use this Factory design pattern in the situation like,If we have a super class and subclasses and based on the data provided we have to return the object of one of the subclasses.


will update soon.........

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 

Tuesday 22 April 2014

JAVA Design Patterns Categories Introduction

Creational Design Patterns:
This patterns are used in Class instantiation.

  • How to Creates objects Effectively.
  • How we can Reuse the objects effectivly.
More:
       object pool design pattern


Structural Design Patterns:
      These patters are all about class and object composition.
  • How objects and classes form a combine a larger structures.
  • Identifying a simple way to realize relationships between entities.
More:
  Private class Data Pattern
  Front controller pattern
  Module  Pattern

Behavioural Design Patterns:
  • How two objects communicate with each other.
  • Interaction between objects is loosely coupled.
  • How objects interact and How the classes/objects sends messages to talk each other in order to accomplish some functionality.
More:
       null Object  design Pattern


JAVA Design Patterns

WHAT:

       Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development.
A DESIGN PATTERN IS A WELL DESCRIBED SOLUTION TO A COMMON SOFTWARE PROBLEM.

WHY:
Design Pattern describes:
  • Each Design Pattern describes a problem in development and in which situation the problem occurs.
  • It also Describes Why this Problem occur
  • Provides solutions for every Problem.
  • We can use this solution many times.
  • Describes when to use this design patterns and what are the consequences.

Design Patterns have the follwing main usages in software development.

  1. Design Patterns are already defined and provides industry standard approach to solve a recurring problem, so it saves time if we sensibly use the design pattern.
  2. Using design patterns promotes reusability that leads to more robust and highly maintainable code. It helps in reducing total cost of ownership (TCO) of the software product.
  3. Since design patterns are already defined, it makes our code easy to understand and debug. It leads to faster development and new members of team understand it easily.
We Have  mainly 23 Design patterns in Java and Categorized into 3 types.

              
       To see  Categories design patterns explanation click here
                                           
                             

Thursday 10 April 2014

J2EE

J2EE??
J2EE stands for Java to Enterprise edition.Upto 2013 we wereusing J2EE version 6.But in 2013 sun released new version of J2EE 7 with some blasting features...(will update soon....)


  • J2EE is a platform for delivering enterprise applications. But what does it mean to say that J2EE is a platform? J2EE isn't hardware, although it can run on any hardware with an appropriate JVM. And J2EE isn't software exactly, since many vendors provide J2EE-compatible systems, and even provide their own JVMs. So what sort of a platform is J2EE, since it's neither a hardware platform, nor a specific software product?
  • In the world of software, a platform is a combination of hardware and software necessary to run applications.


 Basic J2EE Overview diagram



  • Business Layer:Any kind of Database interactions done by JPA(Java Persistence API).
  • Different persistence offered by Java--
                   JDBC
                   JDO(Java data object)
                   JPA(POJO based)
                   Entity Beans
  • Service Layer will use JPA to interact with database.
  • Service layer(EJB) deals with all bussiness operations eg.,sending mails,sending messages to message queuesand any business logic...
  • Web Layer is presentation layer with which clients will interact.Weblayer(JSF) will use the business operations in the service layer(EJB) and the service layer will use with Business layer(JPA) to interact with Database(Oracle/Mysql).
Packaging Structure:
  • Web layer which consists of JSP,JSF,Servlets will be packaged as .war file.
  • The business layer and services layer(means JPA and EJB ) will package together as .jar file.
  • Finally the Jar file and war file will bundled together into .ear file.(ear:enterprise archive)

Top Technologies in J2EE6

Web Services Technologies