XML第三章:建模(工厂模式)

XML第三章:建模(工厂模式)

1、工厂模式介绍和好处。

1、介绍:工厂模式是23设计模式(Design pattern)其中的一种。设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。项目中合理的运用设计模式可以完美的解决很多问题,每种模式在现在中都有相应的原理来与之对应,每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决方案,这也是它能被广泛应用的原因。

2、好处:使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

下面是XML解析:工厂模式。

在建立工厂模式是,要先确定结构和体系(就是制作的步骤的),先了解一下我要解析的XML的文件;
1、先了解他的结构,确定后再下一步。
2、制作从里往外。先把里的框架确定。
3、然后按照自己理解的思路,一步做下去

<config>
 <!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
 <action path="/regAction" type="test.RegAction">
  <!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串  redirect:只能是false|true,允许空,默认值为false -->
     <forward name="failed" path="/reg.jsp" redirect="false" />
     <forward name="success" path="/login.jsp" redirect="true" />
 </action>
 
 <action path="/loginAction" type="test.LoginAction">
      <forward name="failed" path="/login.jsp" redirect="false" />
      <forward name="success" path="/main.jsp" redirect="true" />
 </action>
</config>

1、从最里面的结构开始。<forward name="failed" path="/reg.jsp" redirect="false" />

package com.DZY.JianMao;

/**
 * <forward name="failed" path="/reg.jsp" redirect="false" />
 * @author Aromanic150
 *  
 */
 public class ForwardDemo {
  private  String name;
  private  String path;
  private  boolean redirect=true;
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public String getPath() {
 return path;
}
public void setPath(String path) {
 this.path = path;
}
public boolean getRedirect() {
 return redirect;
}
public void setRedirect(boolean redirect) {
 this.redirect = redirect;
}
}

讲解:name、path、redirect。这几个被看做成属性。里面的值是制作的商品。
redirect :他是固定类型 false和true,只能是boolean ;

2、然后下一个结构<action path="/regAction" type="test.RegAction">

package com.DZY.JianMao;

import java.util.HashMap;
import java.util.Map;
/**
 *  <!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
 <action path="/regAction" type="test.RegAction">
  <!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 
   redirect:只能是false|true,允许空,默认值为false -->
  <forward name="failed" path="/reg.jsp" redirect="false" />
  <forward name="success" path="/login.jsp" redirect="true" />
 </action>
 * @author Aromanic150
 *   
 */
 
public class ActionDemo {
   private  String  path;
   private  String  type;
   private  Map<String, ForwardDemo>  FMap=new  HashMap();
public String getPath() {
 return path;
}
public void setPath(String path) {
 this.path = path;
}
public String getType() {
 return type;
}
public void setType(String type) {
 this.type = type;
}
/**
 * 压栈  堆栈
 * @param forwardDemo
 */
public void setFMap(ForwardDemo forwardDemo) {
 FMap.put(forwardDemo.getName(), forwardDemo);
}
/**
 * 弹栈
 * @return
 */
public ForwardDemo getFMap(String  name) {
 return FMap.get(name);
} 
}

讲解:两个类需要关联起来。第一步的结构是第二步结构里面完成。那就把第一类放到第二里的一个集合里去。
FMap就是第一步。

两个方法:
setFMap.可以更具Key调用valus。而name 是不同的。

getFMap。获得key。

3、结构最后,也是最大的。<config></config>

package com.DZY.JianMao;

import java.util.HashMap;
import java.util.Map;
/**
 * <config>
 <!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
 <action path="/regAction" type="test.RegAction">
  <!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 
   redirect:只能是false|true,允许空,默认值为false -->
  <forward name="failed" path="/reg.jsp" redirect="false" />
  <forward name="success" path="/login.jsp" redirect="true" />
 </action>
 </config>
 * @author Aromanic150
 *
 */
 public class ConfigDemo {
    private   Map<String , ActionDemo>      AMap=new    HashMap();
    /**
     * 压栈  堆栈
     * @param forwardDemo
     */
    public void setAMap(ActionDemo actionDemo) {
     AMap.put(actionDemo.getPath(), actionDemo);
    }
    /**
     * 弹栈
     * @return
     */
    public ActionDemo getAMap(String  name) {
     return AMap.get(name);
    }  
    }

讲解:下步包含着上步。

4、现在是要解析的文件的地址,

package com.DZY.JianMao;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * java中有23中设计模式 工厂模式好处和使用 1、能够提高代码的复用性(可以更换其他的产品,但是结构和体系不能变。就是可以更改内容)
 * 2、只要建立一个方法,去生产指定的你需要的对象; 3、去生产指定的你需要的对象,以便重复使用(对于一个对象,重复调用)
 * 
 * @author Aromanic150
 *
 */
public class ConfigDemoFactory {
 /**
  * 默认资源文件mvc.xml是放在建模类的同包下
  * 
  * @return
  * @throws DocumentException
  */
   private static ConfigDemo build() throws DocumentException {
  return build("mvc.xml");
 }
 /**
  * 
  * @param xmlpath
  * @return
  * @throws DocumentException 
  */
 private static ConfigDemo build(String xmlpath) throws DocumentException {
  ConfigDemo configDemo = new ConfigDemo();
  ActionDemo actionDemo = null;
  ForwardDemo forwardDemo = null;
  InputStream in = ConfigDemoFactory.class.getResourceAsStream(xmlpath);
  SAXReader reader = new SAXReader();
  Document doc = reader.read(in);
  List<Element> actionEles = doc.selectNodes("config/action");
  for (Element actionEle : actionEles) {
   actionDemo = new ActionDemo();
   // 填充actionModel
   actionDemo.setPath(actionEle.attributeValue("path"));
   actionDemo.setType(actionEle.attributeValue("type"));
   List<Element> forwardEles = actionEle.selectNodes("forward");
   for (Element forwardEle : forwardEles) {
    forwardDemo = new ForwardDemo();
    // 填充forwardModel
    forwardDemo.setName(forwardEle.attributeValue("name"));
    forwardDemo.setPath(forwardEle.attributeValue("path"));
    forwardDemo.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
    actionDemo.setFMap(forwardDemo);
   }
   configDemo.setAMap(actionDemo);
  }
  return configDemo;
 }
 public static void main(String[] args) throws DocumentException {
  ConfigDemo configModel = ConfigDemoFactory.build();
  ActionDemo actionModel = configModel.getAMap("/loginAction");
  System.out.println(actionModel.getType());
  System.out.println(actionModel.getFMap("success").getPath());
 }
}

这里解析导入包都是dom4j;
build(“mvc.xml”);这是在同一个包里调用;

2、对一个XML进行解析,文件浏览。

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
   <servlet-name>jrebelServlet</servlet-name>
   <servlet-class>com.zking.DZY.JrebelServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
   <servlet-name>jrebelServlet</servlet-name>
   <url-pattern>/jrebelServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
   <servlet-name>jrebelServlet2</servlet-name>
   <servlet-class>com.zking.DZY.JrebelServlet2</servlet-class>
  </servlet>
  
  <servlet-mapping>
   <servlet-name>jrebelServlet2</servlet-name>
   <url-pattern>/jrebelServlet2</url-pattern>
   <url-pattern>/jrebelServlet3</url-pattern>
  </servlet-mapping>
</web-app>

1、第一步:

package com.DZY.JianMao2;

public class ServletNameModel {
private String text;

public String getText() {
	return text;
}
public void setText(String text) {
	this.text = text;
}
}

2、第二步:

package com.DZY.JianMao2;

public class ServletClassModel {
private String text;

public String getText() {
	return text;
}
public void setText(String text) {
	this.text = text;
}
}

3、第三步;

package com.DZY.JianMao2;

public class UrlPatternModel {
private String text;

public String getText() {
	return text;
}
public void setText(String text) {
	this.text = text;
}
}

4、第四步;

package com.DZY.JianMao2;

public class ServletModel {
private ServletNameModel ServletName;
private ServletClassModel ServeltClass;
public ServletNameModel getServletName() {
 return ServletName;
}
public void setServletName(ServletNameModel servletName) {
 ServletName = servletName;
}
public ServletClassModel getServeltClass() {
 return ServeltClass;
}
public void setServeltClass(ServletClassModel serveltClass) {
 ServeltClass = serveltClass;
}
}

5、第五步;

package com.DZY.JianMao2;

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

public class ServletMappingModel {
private ServletNameModel ServletName;
private List<UrlPatternModel> UrlPattern=new ArrayList<>();
public ServletNameModel getServletName() {
 return ServletName;
}
public void setServletName(ServletNameModel servletName) {
 ServletName = servletName;
}
public void pushUrlPattern(UrlPatternModel urlPattern) {
 UrlPattern.add(urlPattern);
}
public List<UrlPatternModel> getUrlPattern(){
 return UrlPattern;
   }
}

6、第六步;

package com.DZY.JianMao2;

import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> ServletModel=new ArrayList<>();
private List<ServletMappingModel> ServletMappingModel=new ArrayList<>();

public void pushServletModel(ServletModel servletModel) {
	ServletModel.add(servletModel);
}

public List<ServletModel> getServletModel(){
	return ServletModel;
}

public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
	ServletMappingModel.add(servletMappingModel);
}

public List<ServletMappingModel> getServletMappingModel(){
	return ServletMappingModel;
   }
}

7、第七步;

package com.DZY.JianMao2;

import java.io.InputStream;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class WebAppModelFactory {
 /**
  * 默认资源文件web.xml是放在建模类的同包下
  * @return
  * @throws Exception 
  */
 public static WebAppModel buildWebAppModel() throws Exception {
  return buildWebAppModel("web.xml");
 }
 /**
  * 建模
  * 
  * @param xmlPath
  * @return
  * @throws Exception 
  */
 public static WebAppModel buildWebAppModel(String xmlPath) throws Exception {
  InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
  SAXReader saxReader = new SAXReader();
  WebAppModel webAppModel = new WebAppModel();
   Document doc = saxReader.read(in);
      //将servlet的标签内容填充进WebApp
   List<Element> servletEles = doc.selectNodes("/web-app/servlet");
   for (Element servletEle : servletEles) {
    ServletModel servletModel = new ServletModel();
                //给servlet填充内容
    Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
    Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
    ServletNameModel servletNameModel = new ServletNameModel();
    ServletClassModel servletClassModel = new ServletClassModel();
    servletNameModel.setText(servletNameEle.getText());
    servletClassModel.setText(servletClassEle.getText());
    servletNameModel.setText(servletNameEle.getText());
    servletClassModel.setText(servletClassEle.getText());
    webAppModel.pushServletModel(servletModel);
			}
 return webAppModel;
 }			
 /**
  * 通过浏览器输入的网址自动找到对应的后台处理类
  * 
  * @param webAppModel
  *            建模后的实体类
  * @param url
  *            浏览器访问的网址
  * @return
  */
 public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
  String servletClass = "";
  /*
   * 找到浏览器网址对应的servlet-name
   */
  String servletName = "";
  List<ServletMappingModel> servletMappingModels = webAppModel.getServletMappingModel();
  for (ServletMappingModel servletMappingModel : servletMappingModels) {
   List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPattern();
   for (UrlPatternModel urlPatternModel : urlPatternModels) {
    if (url.equals(urlPatternModel.getText())) {
     ServletNameModel servletNameModel = servletMappingModel.getServletName();
     servletName = servletNameModel.getText();
    }
   }
  }

/*
   * 找到servlet-name对应的后台处理类
   */
  List<ServletModel> servletModels = webAppModel.getServletModel();
  for (ServletModel servletModel : servletModels) {
   ServletNameModel servletNameModel = servletModel.getServletName();
   if (servletName.equals(servletNameModel.getText())) {
    ServletClassModel servletClassModel = servletModel.getServeltClass();
    servletClass = servletClassModel.getText();
   }
  }
  return servletClass;
 }
public static void main(String[] args) throws Exception {
  WebAppModel webAppModel = WebAppModelFactory.buildWebAppModel();
  String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
  String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
  String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
  System.out.println(res);
  System.out.println(res2);
  System.out.println(res3);
   }
}

打印結果:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值