J2EE模式特别关注表示层,这些模式是由 Sun Java Center 鉴定的,
包括:
1、 MVC模式(MVCPattern);
2、 业务代表模式(BusinessDelegatePattern);
3、 组合实体模式(CompositeEntityPattern);
4、 数据访问对象模式(DataAccessObjectPattern);
5、 前端控制器模式(FrontControllerPattern);
6、 拦截过滤器模式(InterceptingFilterPattern);
7、 服务定位器模式(ServiceLocatorPattern);
8、 传输对象模式(TransferObjectPattern);
MVC模式代表 Model-View-Controller(模型-视图-控制器) 模式
MVC模式用于应用程序的分层开发
Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO 它也可以带有逻辑,在数据变化时更新控制器
View(视图) - 视图代表模型包含的数据的可视化
Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开
实现
1、 定义一个作为模型的Student对象;
2、 StudentView是一个把学生详细信息输出到控制台的视图类;
3、 StudentController是负责存储数据到Student对象中的控制器类,并相应地更新视图StudentView;
4、 最后类MVCPatternDemo使用StudentController来演示MVC模式的用法;
范例
public class Student {
private String rollNo;
private String name;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StudentView {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}
public class StudentController {
private Student model;
private StudentView view;
public StudentController(Student model, StudentView view){
this.model = model;
this.view = view;
}
public void setStudentName(String name){
model.setName(name);
}
public String getStudentName(){
return model.getName();
}
public void setStudentRollNo(String rollNo){
model.setRollNo(rollNo);
}
public String getStudentRollNo(){
return model.getRollNo();
}
public void updateView(){
view.printStudentDetails(model.getName(), model.getRollNo());
}
}
public class MVCPatternDemo {
public static void main(String[] args) {
//从数据可获取学生记录
Student model = retriveStudentFromDatabase();
//创建一个视图:把学生详细信息输出到控制台
StudentView view = new StudentView();
StudentController controller = new StudentController(model, view);
controller.updateView();
//更新模型数据
controller.setStudentName("John");
controller.updateView();
}
private static Student retriveStudentFromDatabase(){
Student student = new Student();
student.setName("Robert");
student.setRollNo("10");
return student;
}
}
编译运行以上 Java 范例,输出结果如下
Student:
Name: Robert
Roll No: 10
Student:
Name: John
Roll No: 10
业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦
业务代表模式用来减少通信或对表示层代码中的业务层代码的远程查询功能
在业务层中我们有以下实体:
客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码
业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问
查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问
业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑
实现
1、 创建Client、BusinessDelegate、BusinessService、LookUpService、JMSService和EJBService来表示业务代表模式中的各种实体;
2、 定义类BusinessDelegatePatternDemo使用BusinessDelegate和Client来演示业务代表模式的用法;
范例
public interface BusinessService {
public void doProcessing();
}
public class EJBService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking EJB Service");
}
}
JMSService.java
public class JMSService implements BusinessService {
@Override
public void doProcessing() {
System.out.println("Processing task by invoking JMS Service");
}
}
public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType){
if(serviceType.equalsIgnoreCase("EJB")){
return new EJBService();
}else {
return new JMSService();
}
}
}
public class BusinessDelegate {
private BusinessLookUp lookupService = new BusinessLookUp();
private BusinessService businessService;
private String serviceType;
public void setServiceType(String serviceType){
this.serviceType = serviceType;
}
public void doTask(){
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
}
public class Client {
BusinessDelegate businessService;
public Client(BusinessDelegate businessService){
this.businessService = businessService;
}
public void doTask(){
businessService.doTask();
}
}
public class BusinessDelegatePatternDemo {
public static void main(String[] args) {
BusinessDelegate businessDelegate = new BusinessDelegate();
businessDelegate.setServiceType("EJB")