Spring入门之单态模式、工厂模式

wrox《Expert one on one J2EE design and development》
-- Rod Johnson
Spring的优点:
@低侵入式设计,代码污染极低;
@独立于各种应用服务器,可以真正实现Write Once,Run Anywhere的承诺;
@Spring的DI机制降低了业务对象替换的复杂性;
@Spring并不完全依赖于Spring,开发者可以自由选用Spring框架的部分或全部

一、Spring实现两种设计模式
单态模式的示例代码:
// 单态模式测试类
public   class  SingletonTest
{
    
//该类的一个普通属性
    int value;
    
//使用静态属性类保存该类的一个实例
    private static SingltonTest instance;
    
//构造器私有化,避免该类被多次实例化
    private SingletonTest() {
        System.out.println(
"正在执行构造器...");
    }

    
//提供静态方法来返回该类的实例
    public static SingletonTest getInstance() {
        
//实例化类实例前,先检查该类的实例是否存在
        if (instance == null)
        
{
            
//如果不存在,则新建一个实例
            instance = new SingletonTest();
        }

        
//返回该类的成员变量:该类的实例
        return instance;
    }

    
//以下提供对普通属性value的setter和getter方法
    public int getValue() {
        
return value;
    }

    
public void setValue(int value) {
        
this.value = value;
    }

    
public static void main(String[] args) {
        SingletonTest t1 
= SingletonTest.getInstance();
        SingletonTest t2 
= SingletonTest.getInstance();
        t2.setValue(
9);
        System.out.println(t1 
== t2);
    }

}


二、工厂模式的回顾
示例代码:
// Person接口定义
public   interface  Person
{
    
/**
     * 
@param name 对name打招呼
     * 
@return 打招呼的字符串
     
*/

     
public String sayHello(String name);
    
/**
     * 
@param name 对name告别
     * 
@return 告别的字符串
     
*/

     
public String sayGoodBye(String name);
}

// American类实现Person接口
public   class  American  implements  Person
{
    
public String sayHello(String name) {
        
return name + " ,Hello";
    }

    
public String sayGoodBye(String name) {
        
return name + " ,GoodBye";
    }

}

// Chinese
public   class  Chinese  implements  Person
{
    
public String sayHello(String name) {
        
return name + " ,您好";
    }

    
public String sayGoodBye(String name) {
        
return name + " ,再见";
    }

}

然后看Person工厂的代码:
public   class  PersonFactory
{
    
/**
     * 获得Person实例的实例工厂方法
     * 
@param ethnic 调用该实例工厂方法传入的参数
     * 
@return 返回Person实例
     
*/

     
public Person getPerson(String ethnic) {
        
//根据参数返回Person接口的实例
        if (ethnic.equalsIgnoreCase("china"))
        
{
            
return new Chinese();
        }

        
else
        
{
            
return new American();
        }

     }

}

以上是最简单的工厂模式框架,其主程序部分如下:
public   class  FactoryTest
{
    
public static void main(String[] args) {
        
//创建PersonFactory的实例,获得工厂实例
        PersonFactory pf = new PersonFactory();
        
//定义接口Person的实例,面向接口编程
        Person p = null;
        
//使用工厂获得Person的实例
        p = pf.getPerson("china");
        
//下面是调用Person接口的方法
        System.out.println(p.sayHello("prolove"));
        System.out.println(p.sayGoodBye(
"prolove"));
        
//使用工厂获得Person的另一个实例
        p = pf.getPerson("ame");
        
//再次调用Person接口的方法
        System.out.println(p.sayHello("prolove"));
        System.out.println(p.sayGoodBye(
"prolove"));
    }

}


三、Spring对单态与工厂模式的实现
下面是关于两个实例的配置文件:
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " chinese "   class = " prolove.Chinese " />
    
< bean id = " american "   class = " prolove.American " />
</ beans >
主程序部分如下:
public   class  SpringTest
{
    
public static void main(String[] args) {
        
//实例化Spring容器
        ApplicationContext ctx =
            
new FileSystemXmlApplicationContext ("bean.xml");
        
//定义Person接口的实例
        Person p = null;
        
//通过Spring上下文获得chinese实例
        p = (Person)ctx.getBean("chinese");
        
//执行chinese实例的方法
        System.out.println(p.sayHello("prolove"));
        System.out.println(p.sayGoodBye(
"prolove"));
        p 
= (Person)ctx.getBean("american");
        System.out.println(p.sayHello(
"prolove"));
        System.out.println(p.sayGoodBye(
"prolove"));
    }

}

使用Spring时,即使没有工厂类PersonFactory,程序一样可以使用工厂模式,
Spring完全可以提供所有工厂模式的功能
下面对主程序部分进行简单的修改:
public   class  SpringTest
{
    
public static void main(String[] args) {
        
//实例化Spring容器
        ApplicationContext ctx =
            
new FileSystemXmlApplicationContext ("bean.xml");
        
//定义Person接口的实例p1
        Person p1 = null;
        
//通过Spring上下文获得chinese实例
        p1 = (Person)ctx.getBean("chinese");
        
//定义Person接口的实例p2
        Person p2 = null;
        
//通过Spring上下文获得american实例
        p2 = (Person)ctx.getBean("american");
        System.out.println(p1 
== p2);
    }

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值