门面设计模式
2016-04-28 13:47
309 查看
下面就是GoFo设计模式的UML设计图
PS : 门面模式就是隐藏细节,让对方知道更少东东。原则就是迪特米原则
其实说白了,看这个图一般还是看不明白有什么毛用。
举例
1,比如说我们吃的,坑的鸡,香村鸡,我们去点一个狮子头双拼,鸡店暴露给我们的直接就是我们包装好点,菜,饭,筷子。
具体的细节,比如说菜谱,菜,饭是怎么做的。其实我们不是知道的。
2,我们天天乘坐的电梯也是,电梯暴露给我们就只是几个按钮。具体电梯是这么运转的,其实我们是不知道的。
装逼完了,大家应该明白了,下面的案例就是简单版Servlet的功能:
tomcat 的门面模式使用很多。init(ServletConfig)就是包装之后的,Service(Request,respone)也是包装之后的,doFilter(Request,respone) 也是包装之后的
ServletContext也是包装之后的。
网关系统就是门面模式的写真
PS : 门面模式就是隐藏细节,让对方知道更少东东。原则就是迪特米原则
其实说白了,看这个图一般还是看不明白有什么毛用。
举例
1,比如说我们吃的,坑的鸡,香村鸡,我们去点一个狮子头双拼,鸡店暴露给我们的直接就是我们包装好点,菜,饭,筷子。
具体的细节,比如说菜谱,菜,饭是怎么做的。其实我们不是知道的。
2,我们天天乘坐的电梯也是,电梯暴露给我们就只是几个按钮。具体电梯是这么运转的,其实我们是不知道的。
装逼完了,大家应该明白了,下面的案例就是简单版Servlet的功能:
public class Request implements ServletRequest { private InputStream input; private String uri; public Request(InputStream input) { this.input = input; } public String getUri() { return uri; } private String parseUri(String requestString) { int index1, index2; index1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; } //通过输入流转换url 这个方法是属于Request对象的,但是这个方法又不想让外面的系统知道。 public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j=0; j<i; j++) { request.append((char) buffer[j]); } System.out.print(request.toString()); uri = parseUri(request.toString()); } /* implementation of the ServletRequest*/ public Object getAttribute(String attribute) { return null; } public Enumeration getAttributeNames() { return null; } public String getRealPath(String path) { return null; } public RequestDispatcher getRequestDispatcher(String path) { return null; } public boolean isSecure() { return false; } public String getCharacterEncoding() { return null; } public int getContentLength() { return 0; } public String getContentType() { return null; } public ServletInputStream getInputStream() throws IOException { return null; } public Locale getLocale() { return null; } public Enumeration getLocales() { return null; } public String getParameter(String name) { return null; } public Map getParameterMap() { return null; } public Enumeration getParameterNames() { return null; } public String[] getParameterValues(String parameter) { return null; } public String getProtocol() { return null; } public BufferedReader getReader() throws IOException { return null; } public String getRemoteAddr() { return null; } public String getRemoteHost() { return null; } public String getScheme() { return null; } public String getServerName() { return null; } public int getServerPort() { return 0; } public void removeAttribute(String attribute) { } public void setAttribute(String key, Object value) { } public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException { } } public class RequestFacade implements ServletRequest { private ServletRequest request = null; //这里有一个真实处理对象的依赖 public RequestFacade(Request request) { this.request = request; } /* implementation of the ServletRequest*/ public Object getAttribute(String attribute) { return request.getAttribute(attribute); } public Enumeration getAttributeNames() { return request.getAttributeNames(); } public String getRealPath(String path) { return request.getRealPath(path); } public RequestDispatcher getRequestDispatcher(String path) { return request.getRequestDispatcher(path); } public boolean isSecure() { return request.isSecure(); } public String getCharacterEncoding() { return request.getCharacterEncoding(); } public int getContentLength() { return request.getContentLength(); } public String getContentType() { return request.getContentType(); } public ServletInputStream getInputStream() throws IOException { return request.getInputStream(); } public Locale getLocale() { return request.getLocale(); } public Enumeration getLocales() { return request.getLocales(); } public String getParameter(String name) { return request.getParameter(name); } public Map getParameterMap() { return request.getParameterMap(); } public Enumeration getParameterNames() { return request.getParameterNames(); } public String[] getParameterValues(String parameter) { return request.getParameterValues(parameter); } public String getProtocol() { return request.getProtocol(); } public BufferedReader getReader() throws IOException { return request.getReader(); } public String getRemoteAddr() { return request.getRemoteAddr(); } public String getRemoteHost() { return request.getRemoteHost(); } public String getScheme() { return request.getScheme(); } public String getServerName() { return request.getServerName(); } public int getServerPort() { return request.getServerPort(); } public void removeAttribute(String attribute) { request.removeAttribute(attribute); } public void setAttribute(String key, Object value) { request.setAttribute(key, value); } public void setCharacterEncoding(String encoding) throws UnsupportedEncodingException { request.setCharacterEncoding(encoding); } } public class ServletProcessor { public void process(Request request, Response response) { String uri = request.getUri(); String servletName = uri.substring(uri.lastIndexOf("/") + 1); URLClassLoader loader = null; try { // create a URLClassLoader URL[] urls = new URL[1]; URLStreamHandler streamHandler = null; File classPath = new File(Constants.WEB_ROOT); // the forming of repository is taken from the createClassLoader method in // org.apache.catalina.startup.ClassLoaderFactory String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ; // the code for forming the URL is taken from the addRepository method in // org.apache.catalina.loader.StandardClassLoader class. urls[0] = new URL(null, repository, streamHandler); loader = new URLClassLoader(urls); } catch (IOException e) { System.out.println(e.toString() ); } Class myClass = null; try { myClass = loader.loadClass(servletName); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } Servlet servlet = null; //这里暴露给Servlet里的Service方法,就是包装处理过的 RequestFacade requestFacade = new RequestFacade(request); ResponseFacade responseFacade = new ResponseFacade(response); try { servlet = (Servlet) myClass.newInstance(); servlet.service((ServletRequest) requestFacade, (ServletResponse) responseFacade); } catch (Exception e) { System.out.println(e.toString()); } catch (Throwable e) { System.out.println(e.toString()); } } } public class HttpServer { // shutdown command private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received private boolean shutdown = false; public static void main(String[] args) { HttpServer server = new HttpServer(); server.await(); } public void await() { ServerSocket serverSocket = null; int port = 8080; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // Loop waiting for a request while (!shutdown) { Socket socket = null; InputStream input = null; OutputStream output = null; try { socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); // create Request object and parse Request request = new Request(input); request.parse(); // create Response object Response response = new Response(output); response.setRequest(request); //check if this is a request for a servlet or a static resource //a request for a servlet begins with "/servlet/" if (request.getUri().startsWith("/servlet/")) { ServletProcessor processor = new ServletProcessor(); processor.process(request, response); } else { StaticResourceProcessor processor = new StaticResourceProcessor(); processor.process(request, response); } // Close the socket socket.close(); //check if the previous URI is a shutdown command shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } }
tomcat 的门面模式使用很多。init(ServletConfig)就是包装之后的,Service(Request,respone)也是包装之后的,doFilter(Request,respone) 也是包装之后的
ServletContext也是包装之后的。
网关系统就是门面模式的写真
相关文章推荐
- Drupal8社区文档之用户、权限、角色
- 使用RawSocket进行网络抓包
- JAVA科学计数法,金额数字,转换为中文大写字
- Openssl 加密,解密,CA认证
- 一张图看懂UML类图
- 获得内核函数地址的四种方法
- Lab 相关---四相机同步拍摄说明
- S2SH 学习笔记之easyui——表单传递类对象
- HTTP协议详解(很经典)
- 用递归思想解决二叉树的实际应用
- 创建maven项目,有3个目录没有显示,也新建不了,解决
- 简单实用的android 图片的压缩
- 王高利:Kvm虚拟化(4)__为客户机添加virsh console支持
- TabHost中控制activity生命周期
- 查询数据库时like和regepx的用法
- Android源码编译
- Linux上搭建nginx,及简单配置
- springMVC工作原理
- 北京林业大学“计蒜客”杯程序设计竞赛 网络赛 B-大钉骑马走江湖【bfs】
- IntelliJ IDEA 12 创建Web项目 教程 超详细版