J2EE Design Patterns
MVC is abbreviation of Model-View-Controller. Models are basically POJO's, which are used as blueprints/models for all of the objects that will be used in the application. Views represent the presentational aspect of the data and information located in the models. And the Controllers controls both of these.
Controller serve as a connection between the Model and View. It instantiate, update and delete models, populate them with information, and then send the data to the views to present to the end-user.
Front Controller Pattern
Upon sending a request, the Front Controller is the first controller it reaches. Based on the request, it decides which controller is the most adequate to handle it, after which it passes the request to the chosen controller. The Front Controller is most often used in Web Applications in the form of a Dispatcher Servlet.
Business Delegate Pattern
This pattern is used to decouple the presentation layer from the business layer. It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code. In business tier we have following entities.
Composite Entity Pattern
A Composite entity is an EJB entity bean which represents a graph of objects. When a composite entity is updated, internally dependent objects beans get updated automatically as being managed by EJB entity bean. Following are the participants in Composite Entity Bean.
Service Locator Pattern
A pattern often seen in Web Applications, the Service Locator pattern is used to decouple the Service Consumers and the concrete classes like DAO implementations.
The pattern looks for the adequate service, saves it in cache storage to reduce the number of requests and therefore the strain on the server and provides the application with their instances.
-K Himaanshu Shuklaa..
- MVC Pattern
- Business Delegate Pattern
- Composite Entity Pattern
- Data Access Object Pattern
- Front Controller Pattern
- Intercepting Filter Pattern
- Service Locator Pattern
- Transfer Object Pattern
MVC is abbreviation of Model-View-Controller. Models are basically POJO's, which are used as blueprints/models for all of the objects that will be used in the application. Views represent the presentational aspect of the data and information located in the models. And the Controllers controls both of these.
Controller serve as a connection between the Model and View. It instantiate, update and delete models, populate them with information, and then send the data to the views to present to the end-user.
Front Controller Pattern
Upon sending a request, the Front Controller is the first controller it reaches. Based on the request, it decides which controller is the most adequate to handle it, after which it passes the request to the chosen controller. The Front Controller is most often used in Web Applications in the form of a Dispatcher Servlet.
Business Delegate Pattern
This pattern is used to decouple the presentation layer from the business layer. It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code. In business tier we have following entities.
- Business Service: Business Service interface. Concrete classes implement this business service to provide actual business implementation logic.
- LookUp Service: Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.
- Business Delegate: A single entry point class for client entities to provide access to Business Service methods.
- Client: Presentation tier code may be JSP, servlet or UI java code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//our business services | |
public interface MobileBusinessService { | |
public void print(); | |
} | |
//two implementation of business service | |
public class IOService implements MobileBusinessService { | |
@Override | |
public void print() { | |
System.out.println("Processing IOS."); | |
} | |
} | |
public class AndroidService implements MobileBusinessService { | |
@Override | |
public void print() { | |
System.out.println("Processing Android."); | |
} | |
} | |
//The lookup service object should provide the relative | |
//business implementations and business object access | |
//to the business delegate logic | |
public class BusinessLookUp { | |
public MobileBusinessService getBusinessService(String type) { | |
if (type.equalsIgnoreCase("ios")) { | |
return new IOService(); | |
} else if (type.equalsIgnoreCase("android")) { | |
return new AndroidService(); | |
} else { | |
return null; | |
} | |
} | |
} | |
public class BusinessDelegate { | |
private BusinessLookUp lookupService = new BusinessLookUp(); | |
private MobileBusinessService businessService; | |
private String type; | |
public void setServiceType(String type) { | |
this.type = type; | |
} | |
public void print() { | |
businessService = lookupService.getBusinessService(type); | |
businessService.print(); | |
} | |
} | |
public class Client { | |
BusinessDelegate businessDelegate; | |
public Client(BusinessDelegate businessDelegate) { | |
this.businessDelegate = businessDelegate; | |
} | |
public void print() { | |
businessDelegate.print(); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
BusinessDelegate businessDelegate = new BusinessDelegate(); | |
businessDelegate.setServiceType("ios"); | |
Client clienti = new Client(businessDelegate); | |
clienti.print(); | |
businessDelegate.setServiceType("android"); | |
Client clienta = new Client(businessDelegate); | |
clienta.print(); | |
} | |
} |
Composite Entity Pattern
A Composite entity is an EJB entity bean which represents a graph of objects. When a composite entity is updated, internally dependent objects beans get updated automatically as being managed by EJB entity bean. Following are the participants in Composite Entity Bean.
- Dependent Object: Dependent object is an object which depends on coarse grained object for its persistence lifecycle.
- Coarse-Grained Object: This object contains dependent objects. It has its own life cycle and also manages life cycle of dependent objects.
- Composite Entity: It is primary entity bean. It can be coarse grained or can contain a coarse grained object to be used for persistence purpose.
- Strategies: Strategies represents how to implement a Composite Entity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Employee does well, the Manager is satisfied | |
//And when manager is satisfied, Employee does well | |
public class Employee { | |
private String name; | |
private String jobSuccess; | |
//getters setters | |
} | |
public class Manager { | |
private String name; | |
private String satisfaction; | |
//getters setters | |
} | |
public class CoarseGrainedObject { | |
Employee employee = new Employee(); | |
Manager manager = new Manager(); | |
public void setData(String jobSuccess, | |
String satisfaction) { | |
employee.setJobSuccess(jobSuccess); | |
manager.setSatisfaction(satisfaction); | |
} | |
public String[] getData() { | |
return new String[] { | |
"Employee : " + employee.getJobSuccess(), | |
"Manager: " +manager.getSatisfaction()}; | |
} | |
} | |
public class CompositeEntity { | |
private CoarseGrainedObject cgo = new CoarseGrainedObject(); | |
public void setData(String jobSuccess, String satisfaction) { | |
cgo.setData(jobSuccess, satisfaction); | |
} | |
public String[] getData() { | |
return cgo.getData(); | |
} | |
} | |
public class Client { | |
private CompositeEntity compositeEntity = new CompositeEntity(); | |
public void print() { | |
for (int i = 0; i < compositeEntity.getData().length; i++) { | |
System.out.println(compositeEntity.getData()[i]); | |
} | |
} | |
public void setData(String jobSuccess, String satisfaction) { | |
compositeEntity.setData(jobSuccess, satisfaction); | |
} | |
} | |
public class Main { | |
public static void main(String[] args) { | |
Client client = new Client(); | |
client.setData("Successful", "Satisfied"); | |
client.print(); | |
client.setData("Failed", "Unsatisfied"); | |
client.print(); | |
} | |
} |
Service Locator Pattern
A pattern often seen in Web Applications, the Service Locator pattern is used to decouple the Service Consumers and the concrete classes like DAO implementations.
The pattern looks for the adequate service, saves it in cache storage to reduce the number of requests and therefore the strain on the server and provides the application with their instances.
-K Himaanshu Shuklaa..
No comments:
Post a Comment