Sei sulla pagina 1di 9

Java Singleton Design Patern

This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the
JVM.

The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other
Classes). A static modifier is applied to the instance method that returns the object as it then makes this method a
class level method that can be accessed without creating an object.

Example:

public class SingletonObjectDemo {

private static SingletonObject singletonObject;


// Note that the constructor is private
private SingletonObjectDemo() {
// Optional Code
}
public static SingletonObjectDemo getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
return singletonObject;
}
}
Abstract Factory Patern

Defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a
class defer instantiation to subclass.

 The client should be independent of how the products are created.


 Application should be configured with one of multiple families of products.
 Objects needs to be created as a set, in order to be compatible.

public abstract class FinancialToolsFactory {


public abstract TaxProcessor createTaxProcessor();
public abstract ShipFeeProcessor createShipFeeProcessor();
}
public class CanadaFinancialToolsFactory extends FinancialToolsFactory {
public TaxProcessor createTaxProcessor() {
return new CanadaTaxProcessor();
}
public ShipFeeProcessor createShipFeeProcessor() {
return new CanadaShipFeeProcessor();
}
}
public class EuropeFinancialToolsFactory extends FinancialToolsFactory {
public TaxProcessor createTaxProcessor() {
return new EuropeTaxProcessor();
}
public ShipFeeProcessor createShipFeeProcessor() {
return new EuropeShipFeeProcessor();
}
}
public abstract class ShipFeeProcessor {
abstract void calculateShipFee(Order order);
}
public abstract class TaxProcessor {
abstract void calculateTaxes(Order order);
}
public class EuropeShipFeeProcessor extends ShipFeeProcessor {
public void calculateShipFee(Order order) {
// insert here Europe specific ship fee calculation
}
}
public class CanadaShipFeeProcessor extends ShipFeeProcessor {
public void calculateShipFee(Order order) {
// insert here Canada specific ship fee calculation
}
}
public class EuropeTaxProcessor extends TaxProcessor {
public void calculateTaxes(Order order) {
// insert here Europe specific taxt calculation
}
}
public class CanadaTaxProcessor extends TaxProcessor {
public void calculateTaxes(Order order) {
// insert here Canada specific taxt calculation
}
}

// Client
public class OrderProcessor {
private TaxProcessor taxProcessor;
private ShipFeeProcessor shipFeeProcessor;

public OrderProcessor(FinancialToolsFactory factory) {


taxProcessor = factory.createTaxProcessor();
shipFeeProcessor = factory.createShipFeeProcessor();
}
public void processOrder (Order order){
// ....
taxProcessor.calculateTaxes(order);
shipFeeProcessor.calculateShipFee(order);
// ....
}
}

Business delegate pattern

An intermediate class decouples between presentation tier-client and business services.

 Access the business-tier components from presentation-tier components and clients, such as devices,
web services, and rich clients.
 Minimize coupling between clients and the business services, thus hiding the underlying
implementation details of the service, such as lookup and access.
 Avoid unnecessary invocation of remote services.
 Translate network exceptions into application or user exceptions.
 Hide the details of service creation, reconfiguration, and invocation retries from the clients.
Example:

Public Class AdminRequestBD {

public AdminRequestBD() throws AdminBDException {


try {
OPCAdminFacadeHome home = (OPCAdminFacadeHome)
ServiceLocator.getInstance().getRemoteHome(OPC_ADMIN_NAME,OPCAdminFacadeHome.class);
opcAdminEJB = home.create();
} catch (ServiceLocatorException sle) {
throw new AdminBDException(sle.getMessage());
} catch (CreateException ce) {
throw new AdminBDException(ce.getMessage());
} catch (RemoteException re) {
throw new AdminBDException(re.getMessage());
}
}
}

MVC Pattern

The Model/View/Controller(MVC) is an architecture design pattern. Model means data, View means
representation and Controller works on data and representation. MVC focuses on decouple the triad
relationships among data,representation and controller.

Where to use and benifites:

Application architecture design. Any data related design, including non-visual application.Decouple complex
object to improve maintainability.Increase object reusability.
Service Locator Pattern

The Service Locator pattern centralizes distributed service object lookups, provides a centralized point of control, and
may act as a cache that eliminates redundant lookups. It also encapsulates any vendor-specific features of the lookup
process.

 Use the JNDI API to look up and use business components, such as enterprise beans and JMS
components, and services such as data sources.
 Centralize and reuse the implementation of lookup mechanisms for J2EE application clients.
 Encapsulate vendor dependencies for registry implementations, and hide the dependency and
complexity from the clients.
 Avoid performance overhead related to initial context creation and service lookups.

Example:

public class ServiceLocator {

private InitialContext ic;


private Map cache;

private static ServiceLocator me;

static {
try {
me = new ServiceLocator();
} catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}
private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
}
}

static public ServiceLocator getInstance() {


return me;
}
public EJBHome getHome(String name, Class clazz, boolean remote) throws
ServiceLocatorException
{

// Lookup the JNDI name and get the EJBHome interface…


}

public EJBLocalHome getLocalHome(String name) throws ServiceLocatorException {

// Lookup the JNDI name and get the EJBLocalHome interface…


}
}

public static DataSource getDataSource(String name) {

// Lookup the JNDI name and get the Data Source


}

}//end ServiceLocator
Session Façade Pattern

The Session Facade pattern defines a higher-level business component that contains and centralizes complex
interactions between lower-level business components. A Session Facade is implemented as a session enterprise
bean. It provides clients with a single interface for the functionality of an application or application subset. It also
decouples lower-level business components from one another, making designs more flexible and comprehensible.

 Avoid giving clients direct access to business-tier components, to prevent tight coupling with the
clients.
 Provide a remote access layer to your Business Objects and other business-tier components.
 Aggregate and expose your Application Services and other services to remote clients.
 You want to centralize and aggregate all business logic that needs to be exposed to remote clients.
Data Access Object Pattern

Adapt a uniform interface to access multiple databases like relational, unrelational, object-oriented, etc

 Implement data access mechanisms to access and manipulate data in a persistent storage.
 Decouple the persistent storage implementation from the rest of your application.
 Provide a uniform data access API for a persistent mechanism to various types of data sources, such as
RDBMS, LDAP, OODB, XML repositories, flat files, and so on.
 Organize data access logic and encapsulate proprietary features to facilitate maintainability and
portability.
 Hide the data source implementation details from its clients.
public interface CatalogDAO {

public Category getCategory(String categoryID, Locale l)


throws CatalogDAOSysException;

public Page getCategories(int start, int count, Locale l)


throws CatalogDAOSysException;

public Product getProduct(String productID, Locale l)


throws CatalogDAOSysException;

public class CloudscapeCatalogDAO implements CatalogDAO {


//implement CatalogDAO methods….
public Category getCategory(String categoryID, Locale l)throws CatalogDAOSysException {
//implement the getCategory methods(create db connection and get data from DB)

}
public Page getCategories(int start, int count, Locale l)throws CatalogDAOSysException{
//implement the getCategories methods(create db connection and get data from DB)
}
}

Potrebbero piacerti anche