MVC业务代表组合实体数据访问对象

Posted 荆花知了窝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVC业务代表组合实体数据访问对象相关的知识,希望对你有一定的参考价值。


J2EE 模式
这些设计模式特别关注表示层。这些模式是由 Sun Java Center 鉴定的。
  • MVC 模式(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模式

MVC、业务代表、组合实体、数据访问对象

Student.java

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;
  }
}

StudentView.java

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);
  }
}

StudentController.java

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());
  }
}

MVCPatternDemo.java

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;
  }
}

业务代表模式

MVC、业务代表、组合实体、数据访问对象

BusinessService.java

public interface BusinessService {
public void doProcessing();
}

EJBService.java

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");
  }
}

BusinessLookUp.java

public class BusinessLookUp {
public BusinessService getBusinessService(String serviceType){
if(serviceType.equalsIgnoreCase("EJB")){
return new EJBService();
     }else {
return new JMSService();
     }
}
}

BusinessDelegate.java

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();    
  }
}

Client.java

public class Client {

BusinessDelegate businessService;

  public Client(BusinessDelegate businessService){
this.businessService = businessService;
  }

public void doTask(){
businessService.doTask();
  }
}

BusinessDelegatePatternDemo.java

public class BusinessDelegatePatternDemo {

public static void main(String[] args) {

BusinessDelegate businessDelegate = new BusinessDelegate();
     businessDelegate.setServiceType("EJB");

     Client client = new Client(businessDelegate);
     client.doTask();

     businessDelegate.setServiceType("JMS");
     client.doTask();
  }
}

组合实体模式

MVC、业务代表、组合实体、数据访问对象

DependentObject1.java

public class DependentObject1 {

private String data;

  public void setData(String data){
this.data = data;
  }

public String getData(){
return data;
  }
}

DependentObject2.java

public class DependentObject2 {

private String data;

  public void setData(String data){
this.data = data;
  }

public String getData(){
return data;
  }
}

CoarseGrainedObject.java

public class CoarseGrainedObject {
DependentObject1 do1 = new DependentObject1();
  DependentObject2 do2 = new DependentObject2();

  public void setData(String data1, String data2){
do1.setData(data1);
     do2.setData(data2);
  }

public String[] getData(){
return new String[] {do1.getData(),do2.getData()};
  }
}

CompositeEntity.java

public class CompositeEntity {
private CoarseGrainedObject cgo = new CoarseGrainedObject();

  public void setData(String data1, String data2){
cgo.setData(data1, data2);
  }

public String[] getData(){
return cgo.getData();
  }
}

Client.java

public class Client {
private CompositeEntity compositeEntity = new CompositeEntity();

  public void printData(){
for (int i = 0; i < compositeEntity.getData().length; i++) {
System.out.println("Data: " + compositeEntity.getData()[i]);
     }
}

public void setData(String data1, String data2){
compositeEntity.setData(data1, data2);
  }
}

CompositeEntityPatternDemo.java

public class CompositeEntityPatternDemo {
public static void main(String[] args) {
Client client = new Client();
      client.setData("Test", "Data");
      client.printData();
      client.setData("Second Test", "Data1");
      client.printData();
  }
}

数据访问对象模式

Student.java

public class Student {
private String name;
  private int rollNo;

  Student(String name, int rollNo){
this.name = name;
     this.rollNo = rollNo;
  }

public String getName() {
return name;
  }

public void setName(String name) {
this.name = name;
  }

public int getRollNo() {
return rollNo;
  }

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
  }
}

StudentDao.java

import java.util.List;

public interface StudentDao {
public List<Student> getAllStudents();
  public Student getStudent(int rollNo);
  public void updateStudent(Student student);
  public void deleteStudent(Student student);
}

StudentDaoImpl.java

import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

//列表是当作一个数据库
  List<Student> students;

  public StudentDaoImpl(){
students = new ArrayList<Student>();
     Student student1 = new Student("Robert",0);
     Student student2 = new Student("John",1);
     students.add(student1);
     students.add(student2);    
  }
@Override
public void deleteStudent(Student student) {
students.remove(student.getRollNo());
     System.out.println("Student: Roll No " + student.getRollNo()
+", deleted from database");
  }

//从数据库中检索学生名单
  @Override
public List<Student> getAllStudents() {
return students;
  }

@Override
public Student getStudent(int rollNo) {
return students.get(rollNo);
  }

@Override
public void updateStudent(Student student) {
students.get(student.getRollNo()).setName(student.getName());
     System.out.println("Student: Roll No " + student.getRollNo()
+", updated in the database");
  }
}

DaoPatternDemo.java

public class DaoPatternDemo {
public static void main(String[] args) {
StudentDao studentDao = new StudentDaoImpl();

     //输出所有的学生
     for (Student student : studentDao.getAllStudents()) {
System.out.println("Student: [RollNo : "
           +student.getRollNo()+", Name : "+student.getName()+" ]");
     }


//更新学生
     Student student =studentDao.getAllStudents().get(0);
     student.setName("Michael");
     studentDao.updateStudent(student);

     //获取学生
     studentDao.getStudent(0);
     System.out.println("Student: [RollNo : "
        +student.getRollNo()+", Name : "+student.getName()+" ]");    
  }
}


以上是关于MVC业务代表组合实体数据访问对象的主要内容,如果未能解决你的问题,请参考以下文章

Asp.net MVC 项目管理

业务代表模式

业务代表模式

业务代表模式

设计模式业务代表模式

EBI架构 VS. MVC