Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Thursday, May 21, 2015

Design Patterns

Design Patterns

design pattern  is a repeatable solution applicable to solve a generic design problem.

Types of Design patterns

  • Creational patterns offer the flexibility to decide who is responsible for object creation, how the object will be created, which object will be created, and when the creation will take place. in essence, creational patterns provide an abstraction for object instantiation.
    •  Examples: singleton, Factory, abstract Factory, and prototype.
  • Structural patterns are focused on how related classes (and objects) are composed together to form a larger structure.
    •  examples: Composite, decorator, proxy, and Façade.
  • Behavioral patterns define the communication among the objects and control the flow within the participating objects.
    • examples: Mediator, Chain of responsibility, Observer, state, and strategy.
The Singleton Design Pattern

The singleton design pattern is meant to ensure that only one instance of the class is created. The pattern implementation provides a single point of access to the class.
  • The constructor of the class is declared as private.
  • Get an instance of this class is to call the static member method of the class via the getInstance() method
  • This method checks whether a Logger object already exists or not. If not, it creates a Logger instance and assigns it to the static member variable
Example UML Diagram

Another implementation of the Logger class that is based on the “initialization on demand holder” idiom. This idiom uses inner classes and does not use any synchronization construct. It exploits the fact that inner classes are not loaded until they are referenced.

  public class Logger {
private Logger() {
// private constructor
}

public static Logger myInstance;

public static class LoggerHolder {
public static Logger logger = new Logger();
}

public static Logger getInstance() {
return LoggerHolder.logger;
}

public void log(String s) {
// log implementation
System.err.println(s);
}
}


The Factory Design Pattern

you can implement a factory that returns the required type of object(s) on demand in OOP. In this case, the factory decides which class(es) to instantiate to create the required object(s) and exactly how to create them.

Example UML Diagram



Client(SocialDataServiceImpl) invokes SocialDataProviderFactory to get an appropriate object from the Provider hierarchy. SocialDataProviderFactory creates one of the SocialDataProvider from the Provider hierarchy based on the provided information. Client(SocialDataServiceImpl) uses the Provider object without knowing which actual product it is using or how SocialDataProviderFactory created this Provider object.

Example :
public interface SocialDataProvider {
public UserInfo getUserInfobyToken(Tokens tokens) throws BusinessException;
}

public class GoogleDataProvider implements SocialDataProvider {

public UserInfo getUserInfobyToken(Tokens tokens) throws BusinessException {
// implementation
}

}

public class SocialDataProviderFactory {

private SocialDataProvider facebook;
private SocialDataProvider google;
private SocialDataProvider linkedin;

public void setFacebook(SocialDataProvider facebook){
this.facebook = facebook;
}

public SocialDataProvider getDataProvider(SocialNetworkProviderType type){
SocialDataProvider provider = null;
switch (type) {
case Facebook:
provider = facebook;
break;
case Google:
provider = google;
break;
case Linkedin:
provider = linkedin;
break;
}
return provider;
}
}

public class SocialDataServiceImpl implements SocialDataService {

private SocialDataProviderFactory socialDataProviderFactory;

public UserInfo getUserInfo(SocialNetworkProviderType providerType, String code) throws BusinessException {
SocialDataProvider provider = socialDataProviderFactory.getDataProvider(providerType);
userInfo = provider.getUserInfobyToken(tokens);
}

}

in Java SDK where the factory pattern is used, including the following:

  • createStatement() of java.sql.Connection interface, which creates a new Statement to communicate to database.
  • createSocket() of java.rmi.server.RmiClientSocketFactory interface, which returns a new client Socket.
Differences Between Factory and Abstract Factory Design Patterns

factory design pattern creates (or manufactures) the requested type of object on demand. By contrast, the abstract factory is basically a factory of factories.


The Data Access Object (DAO) Design Pattern

This design pattern abstracts the details of the underlying persistence mechanism and offers
you an easy-to-use interface for implementing the persistence feature in your application.

The DAO pattern hides the implementation details of the data source from its clients, thereby introducing loose coupling between your core business logic and your persistence mechanism.




Benefits of the DAO design pattern:
  • The pattern introduces an abstraction: the DAO hides the implementation details of the actual data source from the core business logic. The business logic need not know about the nitty-gritty of the data source, which results in easy-to-understand, less complicated code.
  • The pattern separates the persistence mechanism from rest of the application code and puts it together in one class specific to one data source. This centralization enables easier maintenance and easier bug-tracing.
  • It is quite easy to extend support for other data sources using this pattern. For instance, if you want to provide support for XML-based repository or Using Hibernate , JPA, Ibatis Implementation.



No comments:

Post a Comment