Spring的工厂模式

注:本例来自于《JavaEE实用教程》郑阿奇主编的Spring章节中的一例。

本项目是Java Project,可见Spring框架还适合Java应用程序开发。

目录结构









第一部分:自定义工厂类

创建IHuman接口

package iface;

//定义IHuman接口
public interface IHuman {
	void eat(); //人都会eat
	void walk(); //人都会walk
}
实现中国人Chinese.java和美国人American.java
package face;

import iface.IHuman;

//中国人是人需要实现IHuman
public class Chinese implements IHuman {

	@Override
	public void eat() {
		System.out.println("中国人很会吃!");
	}

	@Override
	public void walk() {
		System.out.println("中国人健步如飞!");
	}

}
package face;

import iface.IHuman;

//美国人是人需要实现IHuman
public class American implements IHuman {

	@Override
	public void eat() {
		System.out.println("美国人吃西餐!");
	}

	@Override
	public void walk() {
		System.out.println("美国人经常坐车");
	}

}
创建生成Chinese实例和American实例的工厂类Factory.java
package factory;

import face.American;
import face.Chinese;
import iface.IHuman;

public class Factory {
	public IHuman getHuman(String human) { //用工厂类产生人类,由于不知道返回值类型,因此用IHuman类型作为返回值
		if(human.equals("Chinese")) {
			return new Chinese(); //参数是Chinese返回中国人的实例
		} else if(human.equals("American")) {
			return new American(); //参数是American返回美国人的实例
		} else {
			throw new IllegalArgumentException("参数不正确"); //其他参数均抛出异常
		}
	}
}
测试Test.java
package test;

import face.American;
import face.Chinese;
import iface.IHuman;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		IHuman human = null;
		human = new Factory().getHuman("Chinese");
		human.eat();
		human.walk();
		human = new Factory().getHuman("American");
		human.eat();
		human.walk();
	}

}
第二部分:使用Spring框架

在MyEclipse中,右键SpringExample选择MyEclipse再选择Add Spring Capabilities,本项目选择的是Spring 2.0,仅创建了核心库。

生成了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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="chinese" class="face.Chinese"></bean>
	<bean id="american" class="face.American"></bean>

</beans>
完成后可以删除factory.Factory。修改Test.java
package test;

import face.American;
import face.Chinese;
import iface.IHuman;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
/*		IHuman human = null;
		human = new Factory().getHuman("Chinese");
		human.eat();
		human.walk();
		human = new Factory().getHuman("American");
		human.eat();
		human.walk(); */
		ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
		IHuman human =null;
		human = (Chinese)ac.getBean("chinese"); //getBean返回Object,需要强制转换
		human.eat();
		human.walk();
		human = (American)ac.getBean("american");
		human.eat();
		human.walk();
	}

}
第三部分:输出结果

上面两例均在Console中输出:

中国人很会吃!
中国人健步如飞!
美国人吃西餐!
美国人经常坐车

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值