前端控制器、拦截过滤器、服务定位器、传输对象-荆花知了窝小史店吧
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)
前端控制器模式
HomeView.java
public class HomeView {public void show(){System.out.println("Displaying Home Page"); }}
StudentView.java
public class StudentView {public void show(){System.out.println("Displaying Student Page"); }}
Dispatcher.java
public class Dispatcher {private StudentView studentView; private HomeView homeView; public Dispatcher(){studentView = new StudentView();homeView = new HomeView(); }public void dispatch(String request){if(request.equalsIgnoreCase("STUDENT")){studentView.show();}else{homeView.show();} }}
FrontController.java
public class FrontController {private Dispatcher dispatcher; public FrontController(){dispatcher = new Dispatcher(); }private boolean isAuthenticUser(){System.out.println("User is authenticated successfully.");return true; }private void trackRequest(String request){System.out.println("Page requested: " + request); }public void dispatchRequest(String request){//记录每一个请求trackRequest(request);//对用户进行身份验证if(isAuthenticUser()){dispatcher.dispatch(request);} }}
FrontControllerPatternDemo.java
public class FrontControllerPatternDemo {public static void main(String[] args) {FrontController frontController = new FrontController();frontController.dispatchRequest("HOME");frontController.dispatchRequest("STUDENT"); }}
拦截过滤器模式
Filter.java
public interface Filter {public void execute(String request);}
AuthenticationFilter.java
public class AuthenticationFilter implements Filter {public void execute(String request){System.out.println("Authenticating request: " + request); }}
DebugFilter.java
public class DebugFilter implements Filter {public void execute(String request){System.out.println("request log: " + request); }}
Target.java
public class Target {public void execute(String request){System.out.println("Executing request: " + request); }}
FilterChain.java
import java.util.ArrayList;import java.util.List;public class FilterChain {private List<Filter> filters = new ArrayList<Filter>(); private Target target; public void addFilter(Filter filter){filters.add(filter); }public void execute(String request){for (Filter filter : filters) {filter.execute(request);}target.execute(request); }public void setTarget(Target target){this.target = target; }}
FilterManager.java
public class FilterManager {FilterChain filterChain; public FilterManager(Target target){filterChain = new FilterChain();filterChain.setTarget(target); }public void setFilter(Filter filter){filterChain.addFilter(filter); }public void filterRequest(String request){filterChain.execute(request); }}
Client.java
public class Client {FilterManager filterManager; public void setFilterManager(FilterManager filterManager){this.filterManager = filterManager; }public void sendRequest(String request){filterManager.filterRequest(request); }}
InterceptingFilterDemo.java
public class InterceptingFilterDemo {public static void main(String[] args) {FilterManager filterManager = new FilterManager(new Target());filterManager.setFilter(new AuthenticationFilter());filterManager.setFilter(new DebugFilter());Client client = new Client();client.setFilterManager(filterManager);client.sendRequest("HOME"); }}
服务定位器模式
Service.java
public interface Service {public String getName(); public void execute();}
Service1.java
public class Service1 implements Service {public void execute(){System.out.println("Executing Service1"); }@Overridepublic String getName() {return "Service1"; }}
Service2.java
public class Service2 implements Service {public void execute(){System.out.println("Executing Service2"); }@Overridepublic String getName() {return "Service2"; }}
InitialContext.java
public class InitialContext {public Object lookup(String jndiName){if(jndiName.equalsIgnoreCase("SERVICE1")){System.out.println("Looking up and creating a new Service1 object"); return new Service1();}else if (jndiName.equalsIgnoreCase("SERVICE2")){System.out.println("Looking up and creating a new Service2 object"); return new Service2();}return null; }}
Cache.java
import java.util.ArrayList;import java.util.List;public class Cache {private List<Service> services; public Cache(){services = new ArrayList<Service>(); }public Service getService(String serviceName){for (Service service : services) {if(service.getName().equalsIgnoreCase(serviceName)){System.out.println("Returning cached "+serviceName+" object");return service; }}return null; }public void addService(Service newService){boolean exists = false;for (Service service : services) {if(service.getName().equalsIgnoreCase(newService.getName())){exists = true; }}if(刘季是谁!exists){services.add(newService);}}}
ServiceLocator.java
public class ServiceLocator {private static Cache cache; static {cache = new Cache(); }public static Service getService(String jndiName){Service service = cache.getService(jndiName);if(service 甄心爱桦 != null){return service;}InitialContext context = new InitialContext();Service service1 = (Service)context.lookup(jndiName);cache.addService(service1);return service1; }}
ServiceLocatorPatternDemo.java
public class ServiceLocatorPatternDemo {public static void main(String[] args) {Service service = ServiceLocator.getService("Service1");service.execute();service = ServiceLocator.getService("Service2");service.execute();service = ServiceLocator.getService("Service1");service.execute();service = ServiceLocator.getService("Service2");service.execute(); }}
传输对象模式
StudentVO.java
public class StudentVO {private String name; private int rollNo; StudentVO(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; }}
StudentBO.java
import java.util.ArrayList;import java.util.List;public class StudentBO {//列表是当作一个数据库 List<StudentVO> students; public StudentBO(){students = new ArrayList<StudentVO>();StudentVO student1 = new StudentVO("Robert"韩宜邦 ,小王爷0);StudentVO student2 = new StudentVO("John"捕快春秋 ,1);students.add(student1);students.add(student2); }public void deleteStudent(StudentVO student) {students.remove(student.getRollNo());System.out.println("Student: Roll No " + student.getRollNo() +"诸康妮 , deleted from database"); }//从数据库中检索学生名单 public List<StudentVO> getAllStudents() {return students; }public StudentVO getStudent(int rollNo) {return students.get(rollNo); }public void updateStudent(StudentVO student) {students.get(student.getRollNo()).setName(student.getName());System.out.println("Student: Roll No " + student.getRollNo() +"江静碧云天 , updated in the database"); }}
TransferObjectPatternDemo.java
public class TransferObjectPatternDemo {public static void main(String[] args) {StudentBO studentBusinessObject = new StudentBO();//输出所有的学生for (StudentVO student : studentBusinessObject.getAllStudents()) {System.out.println("Student: [RollNo : " +student.getRollNo()+"蔡东家 , Name : "+student.getName()+" ]");}//更新学生StudentVO student =studentBusinessObject.getAllStudents().get(0);student.setName("Michael");studentBusinessObject.updateStudent(student);//获取学生studentBusinessObject.getStudent(0);System.out.println("Student: [RollNo : "+student.getRollNo()+"九头怪猫 ,郑翠萍 Name : "+student.getName()+" ]"); }}