体系结构汇报

124 阅读3分钟

工厂模式

简单工厂模式

抽象类

package com.dm.product;
public interface Vehicle {
     void run();
}

具体类1 Car

实现抽象类

package com.dm.product;
public class Car implements Vehicle{
     @Override
     public void run() {
         // TODO Auto-generated method stub
         System.out.println("car run....");
     }
}

具体类2 Bus

package com.dm.product;
public class Bus implements Vehicle{
     @Override
     public void run() {
         // TODO Auto-generated method stub
         System.out.println("bus run....");
     }
}

工厂类

package com.dm.product;
public class Factory {
     public static Vehicle produce(String type) {
         Vehicle vehicle = null;
         if(type.equals("car")) {
             vehicle = new Car();
         }
         if(type.equals("bus")) {
             vehicle = new Bus();
         }
         
         return vehicle;
     }
}

测试类

package com.dm.test;
import static org.junit.Assert.*;
import com.dm.product.Factory;
import com.dm.product.Vehicle;
public class Test {
     @org.junit.Test
     public void test() {
         Vehicle vehicle = Factory.produce("car");
         vehicle.run();
     }
}
——————
car run …

工厂方法模式

抽象类

package com.dm.product2;
public interface Vehicle {
     void run();
}
—————————————————————————————————
package com.dm.product2;
public interface Factory {
     Vehicle produce();
}

具体类-Bus

package com.dm.product2;
public class BusFactory implements Factory{
     @Override
     public Vehicle produce() {
         // TODO Auto-generated method stub
         return new Bus();
     }
}
—————————————————————————————————————————————————————
package com.dm.product2;
public class Bus implements Vehicle{
     @Override
     public void run() {
         // TODO Auto-generated method stub
         System.out.println("bus run...");
     }
}

具体类-Car

package com.dm.product2;
public class CarFactory implements Factory{
     @Override
     public Vehicle produce() {
         // TODO Auto-generated method stub
         return new Car();
     }
     
}
———————————————————————————————————————————
package com.dm.product2;
public class Car implements Vehicle{
     @Override
     public void run() {
         // TODO Auto-generated method stub
         System.out.println("car run ...");
     }
}

测试类

package com.dm.test;
import static org.junit.Assert.*;
import com.dm.product2.*;
import org.junit.Test;
public class Test2 {
     @Test
     public void test2() {
         Factory carFactory = new CarFactory();
         Vehicle car = carFactory.produce();
         car.run();
     }
     
}

单例模式

饿汉式

public class Main {
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         Singleton instance = Singleton.getInstance();
         Singleton instance2 = Singleton.getInstance();
         System.out.println(instance==instance2);
     }
}
class Singleton{
//1.构造方法私有化(防止new)
     private Singleton() {
     }
     //2.类的内部创建对象
     private final static Singleton instance = new Singleton();
     //3.向外暴露一个静态公共方法
     public static Singleton getInstance() {
         return instance;
     }
}

缺点:类加载的时候就完成实例化,如果没有用到这个对象,就会造成内存的浪费 懒汉式 懒汉式

public class Main {
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         Singleton instance = Singleton.getInstance();
         Singleton instance2 = Singleton.getInstance();
         System.out.println(instance==instance2);
     }
}
class Singleton{
     private Singleton() {
     }
     
     private  static Singleton instance;
     
     //使用到 getInstance 才去创建
     public static Singleton getInstance() {
         if(instance==null) {
             instance = new Singleton();
         }
         return instance;
     }
}

spring 工厂模式单例模式应用

工厂模式应用:spring ioc容器帮我们创建对象

新建一个person类

public class Person {
     private String lastName;
     private Integer age;
     private String gender;
     private String email;
    //省略get,set和toString方法
}

新建一个spring bean configuration ,xml文件,取名ioc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="person01" class="com.xx.bean.Person">
  <property name="lastName" value="张三"></property>
  <property name="age" value="18"></property>
  <property name="email" value="zhangsan@qq.com"></property>
  <property name="gender" value="男"></property>
</bean> 
</beans>

测试类

public class IOCtest {
     ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
     @Test
     public void test() {
         Person bean = (Person) ioc.getBean("person01");
         System.out.println(bean);
     }
}

单例模式应用:每一个bean默认是单例的

测试类

public class IOCtest {
     ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
     @Test
     public void test() {
         Person bean = (Person) ioc.getBean("person01");
         Person bean2 = (Person) ioc.getBean("person01");
         System.out.println(bean==bean2);
     }
}
=====================================================
true

可以设置不是单例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="person01" class="com.xx.bean.Person"  scope="prototype">
  <property name="lastName" value="张三"></property>
  <property name="age" value="18"></property>
  <property name="email" value="zhangsan@qq.com"></property>
  <property name="gender" value="男"></property>
</bean> 
</beans>

rest

spring中有一个filter,可以把普通请求转化为规定的形式的请求 创建一个post表单类型,表单中携带_method参数,这个_method可以转化请求为put或者delete

<filter>
         <filter-name>HiddenHttpMethodFilter</filter-name>
         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>HiddenHttpMethodFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

后台接受请求

@Controller
public class BookController {
     @RequestMapping(value="book/{bid}",method = RequestMethod.GET)
     public String getBook(@PathVariable("bid")Integer id,Model model) {
         String idd = "查询";
         model.addAttribute("idd", idd);
         return "success";
     }
     
     @RequestMapping(value="book/{bid}",method = RequestMethod.DELETE)
     public String deleteBook(@PathVariable("bid")Integer id,Model model) {
         String idd = "删除";
         model.addAttribute("idd", idd);
         return "success";
     }
     @RequestMapping(value="book/{bid}",method = RequestMethod.PUT)
     public String updateBook(@PathVariable("bid")Integer id,Model model) {
         String idd = "更新";
         model.addAttribute("idd", idd);
         return "success";
     }
     
     @RequestMapping(value="book",method = RequestMethod.POST)
     public String addBook(Model model) {
         String idd = "添加查询";
         model.addAttribute("idd", idd);
         return "success";
     }
     
}

页面请求

//查询
<a href="book/1">查询图书</a><br/>
//添加
<form action="book" method="post">
<input type="submit" value="添加1号图书">
</form>
<br/>
//删除
<form action="book/1" method="post">
<input name="_method" value="delete" type="hidden"/>
<input type="submit" value="删除号图书">
</form>
//更新
<form action="book/1" method="post">
<input name="_method" value="put" type="hidden"/>
<input type="submit" value="更新号图书">
</form>

www.cnblogs.com/earvin/p/10…