设计模式

来源:互联网 发布:js中eval函数的用法 编辑:程序博客网 时间:2024/05/29 07:01
一.框架:框架是一套已经完成的代码,为我们的开发过程提供便利。
SSM:Spring + Spring MVC + Mybatis

MVC(Model + View + Controller)是一种设计理念,将程序中的各个部分划分出来,然后表现为不同的类,从而实现“每个类都有自己的定位,只用专注自身的业务或者功能,而不关心其它的部分是如何实现、完成的”!

添加spring-webmvc的3.2.8版本的jar包

二.设计模式 — 单例模式
单例模式的表现是:在任何时刻,某个类的实例(对象)只会存在1个!

1.饿汉式(Eager Singleton)

饿汉式的单例表现为实例在声明变量时就已经创建!

public class User {    private static User user = new User();    private User() {        super();    }    public static User getInstance() {        return user;    }}
2.懒汉式(Lazy Singleton)

懒汉式的单例表现为获取对象时才根据需要创建对象。

public class User {    private static User user = null;    private static Object lock = new Object();    private User() {}        public static User getInstance() {        if (user == null) {            synchronized (lock) {                if (user == null) {                    user = new User();                }            }        }        return user;    }}
3.bean的一些属性
<bean id="user" init-method="init" destroy-method="destroy" class="cn.tedu.spring.bean.User"/><bean lazy-init="true" scope="singleton" id="singletonUser" init-method="init" destroy-method="destroy" class="cn.tedu.spring.bean.SingletonUser"/><bean scope="prototype" id="prototypeUser" init-method="init" destroy-method="destroy" class="cn.tedu.spring.bean.PrototypeUser"/>

package cn.tedu.spring.bean;public class User {public User() {System.out.println("User->执行构造方法");}public void init() {System.out.println("User->init");}public void destroy() {System.out.println("User->destroy");}}

三.设计模式 — 工厂模式

工厂模式的作用是:生产对象,使得需要对象的那一方不用关心对象的生产过程
通常,应该先设计对象标准,即设计接口:

public interface IUserDao {
    void insert(User user);
}
然后,设计接口的实现类:

public class UserDaoImpl implements IUserDao {
    public void insert(User user) {}
}

再接下来,设计工厂类:

public class UserDaoFactory {
    public static IUserDao newInstance() {
        return new UserDaoImpl();
    }
}
以上完成后,当需要对象时:


IUserDao dao = UserDaoFactory.newInstance();
dao.insert(...);
可以发现,以上代码中并没有体现出实现类的名字!也就可以理解为:以上程序中,对UserDaoImpl没有太多的依赖性!也称之为“解耦”!即使某天UserDaoImpl类不足以满足程序的需求,只需要另写一个例如UserDaoImpl2也实现IUserDao接口,并修改工厂的返回值即可,整个项目中的其它代码都不需要调整!

四.Spring框架


1.特点:
(1)无侵入性
(2)各个组件之间的耦合极为松散
(3)无需程序员自己实现singleton模式
(4)通过AOP,可以实现事务管理和日志管理
(5)整合其他的框架,如:struts框架和hibernate
2.用途:解决了生产对象的问题,实现了解耦
3.开发Spring DEMO
(1)添加Spring配置文件(资源里)applicationContext.xml:
(2)创建被Spring管理的类,可以是任何类,例如创建User类
(3)创建测试运行的类


(4)配置Spring的配置文件
添加节点,在节点中添加id和class属性
- id:自定义名称,推荐能与类名联想得上
- class:被管理的类的完整名,即“包名+类名”

(5)测试运行
a) 加载配置文件
String file = “applicationContext.xml”;
ApplicationContext ac
= new ClassPathXmlApplicationContext(file);
b) 调用ac的getBean()方法获取对象
User user = (User) ac.getBean(“user”);
以上getBean()的参数是XML配置的的id

(6)关于getBean()的类型
由ApplicationContext定义的getBean()被重载了多次,其中,getBean(String, Class)的返回值是泛型的!
关于ApplicationContext的close()
ApplicationContext接口并没有声明close()方法,如果需要关闭,则应该将变量声明为AbstractApplicationContext。



4.通过Spring管理的类的对象的数量
在默认情况下,Spring管理的类的对象都是单例的,并且在加载Spring配置文件时就已经创建出了bean的对象!
在Spring配置文件中,为节点添加scope属性可以配置作用域,该属性的值可以是singleton,也是默认值,表示单例,也可以取值为prototype,非单例的,并且,在加载Spring配置文件时并不会直接创建对象,而是根据需要(获取对象时)才会创建对象!

5.内存泄漏与内存溢出
当尝试释放某个资源,却由于该资源正在被使用,无法释放,则可能导致该资源长期存在于内存中!这种状况称之为内存泄漏(Leak)!
其实,少量的内存泄漏是没有危害的!特别是单机运行的程序!
但是,泄漏的数据是无法继续被使用的,却占用了内存空间,也就是无谓的占用了内存!会影响整个计算机运行的性能!
并且,当泄漏的数据越来越多时,就会导致可用的内存越来越少,最极端的表现就是内存中有非常大数量的泄漏数据,导致内存几乎没有可用内存,此时再在内存中创建新的数据,内存会没有空间来存储,就会出现“内存溢出”!

五.实际Demo
1.applicationContext.xml文件:

<?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:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">        <!-- 通过构造方法 -->   <bean id="date" class="java.util.Date"/>   <!-- 通过静态工厂 --><bean id="calendar" class="java.util.Calendar" factory-method="getInstance"/><!-- 通过实例工厂 --><bean id="userFactory" class="cn.tedu.spring.bean.UserFactory"></bean><bean id="user" factory-method="getUser" factory-bean="userFactory"></bean>    </beans>

2.用户类:

package cn.tedu.spring.bean;public class User {}
3.用户工厂:
package cn.tedu.spring.bean;public class UserFactory {public User getUser() {return new User();}}

4.测试类:

package cn.tedu.spring.test;import java.util.Date;import java.util.Calendar;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.tedu.spring.bean.User;public class Test {public static void main(String[] args) {String file = "applicationContext.xml";// 加载配置文件AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(file);//日志System.out.println("加载配置完成! \n");//获取id=date对象Date date = ctx.getBean("date", Date.class);System.out.println("id=date: \n"+date+"\n");//获取id=calendar对象Calendar calendar = ctx.getBean("calendar", Calendar.class);System.out.println("id=calendar: \n"+calendar+"\n");//获取id=userUser user = ctx.getBean("user", User.class);System.out.println("id=user: \n"+user+"\n");//释放ctx.close();}}


原创粉丝点击