您的位置:首页 > 其它

门面设计模式

2016-04-28 13:47 309 查看
下面就是GoFo设计模式的UML设计图



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也是包装之后的。

网关系统就是门面模式的写真
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: