首页 > 其他分享 >设计模式十二(访问者模式,mvc模式,业务代表模式)

设计模式十二(访问者模式,mvc模式,业务代表模式)

时间:2022-11-02 18:34:06浏览次数:46  
标签:String void 模式 class mvc new model 设计模式 public

访问者模式

 

 

 

 

public interface ComputerPart {
   public void accept(ComputerPartVisitor computerPartVisitor);
}
public class Monitor  implements ComputerPart {
 
   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      computerPartVisitor.visit(this);
   }
}
public class Mouse  implements ComputerPart {
 
   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      computerPartVisitor.visit(this);
   }
}
public class Computer implements ComputerPart {
   
   ComputerPart[] parts;
 
   public Computer(){
      parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()};      
   } 
 
 
   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      for (int i = 0; i < parts.length; i++) {
         parts[i].accept(computerPartVisitor);
      }
      computerPartVisitor.visit(this);
   }
}
public interface ComputerPartVisitor {
   public void visit(Computer computer);
   public void visit(Mouse mouse);
   public void visit(Keyboard keyboard);
   public void visit(Monitor monitor);
}
public class ComputerPartDisplayVisitor implements ComputerPartVisitor {
 
   @Override
   public void visit(Computer computer) {
      System.out.println("Displaying Computer.");
   }
 
   @Override
   public void visit(Mouse mouse) {
      System.out.println("Displaying Mouse.");
   }
 
   @Override
   public void visit(Keyboard keyboard) {
      System.out.println("Displaying Keyboard.");
   }
 
   @Override
   public void visit(Monitor monitor) {
      System.out.println("Displaying Monitor.");
   }
}
public class VisitorPatternDemo {
   public static void main(String[] args) {
 
      ComputerPart computer = new Computer();
      computer.accept(new ComputerPartDisplayVisitor());
   }
}
Displaying Mouse.
Displaying Keyboard.
Displaying Monitor.
Displaying Computer.

总结:computer中注入父类数组,并在构造方法中实例化该数组。然后computer讲访问者作为参数执行主要方法。访问者的父类接口中会有每个compuerpart类子类。每个执行子类中属性会调用自身。利用的是重载

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  = retrieveStudentFromDatabase();
 
      //创建一个视图:把学生详细信息输出到控制台
      StudentView view = new StudentView();
 
      StudentController controller = new StudentController(model, view);
 
      controller.updateView();
 
      //更新模型数据
      controller.setStudentName("John");
 
      controller.updateView();
   }
 
   private static Student retrieveStudentFromDatabase(){
      Student student = new Student();
      student.setName("Robert");
      student.setRollNo("10");
      return student;
   }
}

 

}
Student: 
Name: Robert
Roll No: 10
Student: 
Name: John
Roll No: 10

 

 

 

 总结:controller注入model,和view,并将两个作为构造方法。通过controller调用model和view。

 

业务代表模式

业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减少通信或对表示层代码中的业务层代码的远程查询功能。在业务层中我们有以下实体。

  • 客户端(Client) - 表示层代码可以是 JSP、servlet 或 UI java 代码。
  • 业务代表(Business Delegate) - 一个为客户端实体提供的入口类,它提供了对业务服务方法的访问。
  • 查询服务(LookUp Service) - 查找服务对象负责获取相关的业务实现,并提供业务对象对业务代表对象的访问。
  • 业务服务(Business Service) - 业务服务接口。实现了该业务服务的实体类,提供了实际的业务实现逻辑。
public interface BusinessService {
   public void doProcessing();
}
public class EJBService implements BusinessService {
 
   @Override
   public void doProcessing() {
      System.out.println("Processing task by invoking EJB Service");
   }
}
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");
 
      Client client = new Client(businessDelegate);
      client.doTask();
 
      businessDelegate.setServiceType("JMS");
      client.doTask();
   }
}
Processing task by invoking EJB Service
Processing task by invoking JMS Service

总结:BusinessLookUp根据参数进行调用指定Service,BusinessDelegate负责代理和执行业务。Client注入BusinessDelegate并执行方法。

标签:String,void,模式,class,mvc,new,model,设计模式,public
From: https://www.cnblogs.com/czfblog-01/p/16851947.html

相关文章

  • Java-抽象模板模式
    什么是模板模式?定义程序的骨架,而将一些步骤延迟到子类中。模板模式使得子类可以不需要改变程序的结构即可重定义该程序的某些特定步骤。通俗的讲,模......
  • 行为型模式-责任链模式
    概述在现实生活中,常常会出现这样的事例:一个请求有多个对象可以处理,但每个对象的处理条件或权限不同。例如,公司员工请假,可批假的领导有部门负责人、副总经理、总经理等,但每......
  • SpringBoot自定义注解+异步+观察者模式实现业务日志保存
    一、前言我们在企业级的开发中,必不可少的是对日志的记录,实现有很多种方式,常见的就是基于AOP+注解进行保存,但是考虑到程序的流畅和效率,我们可以使用异步进行保存,在高并发情......
  • 设计模式十一(空对象模式,策略模式、模板模式)
    在空对象模式(NullObjectPattern)中,一个空对象取代NULL对象实例的检查。Null对象不是检查空值,而是反应一个不做任何动作的关系。这样的Null对象也可以在数据不可用的......
  • CH582开启睡眠模式(LowerPowerSleep)下低功耗测试
             上图为手册中在睡眠模式下的功耗值,以此为参考依据。GPIOA_ModeCfg(GPIO_Pin_All,GPIO_ModeIN_PU);GPIOB_ModeCfg(GPIO_Pin_All,......
  • 爱上源码,重学Spring MVC深入
    1.1gradle搭建源码调试环境1)搭建gradle环境4个步骤1、File-New-Module选择java和web2、填写包信息3、存储路径2)增加起步依赖依赖的项目,直接复制粘贴上去1、对......
  • 设计模式
    设计模式什么是设计模式设计模式是前辈们对代码开发经验的总结,是解决特定问题的一系列套路。它不是语法规定,而是一套用来提高代码可复用性、可维护性、可读性、稳健性......
  • 华为荣耀5X(畅玩版 全网通)USB调试模式如何开启教程(开发者模式 开发者选项打开)
    作者:程序员小冰前一段时间,公司买了一款华为荣耀畅玩版5X全网通,进行测试。发现拿usb数据线连接PC电脑,无法进行调试。找不到开发者选项。利用应用宝连接也没成功。最后各......
  • 【JAVA】设计模式之懒汉式与恶汉式的单例模式实现的方法与详解
    作者:程序员小冰,GitHub主页:​​https://github.com/QQ986945193​​​新浪微博:​​​http://weibo.com/mcxiaobing​​长期维护的Android项目,里面包括常用功能实现,以及知识......
  • 行为型模式-命令模式
    概述日常生活中,我们出去吃饭都会遇到下面的场景。   定义将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,......