目标:熟悉静态工厂方法单例多例熟悉简单工厂熟悉抽象工厂熟悉简单工厂和抽象工厂的优缺点Simulate Spring的BeanFactory
静态工厂方法解析:示例代码:产品类:Car.java
package org.chinasoft.dp.singleton; public class Car implements Moveable { private static Car car = new Car(); private Car() { } public static Car getInstance() { return car; } public void run() { System.out.println("咔咔。。。"); } }
Moveable.java package org.chinasoft.dp.singleton; public interface Moveable { void run(); } 测试类: Test1.java package org.chinasoft.dp.singleton; public class Test1 { public static void main(String[] args) { Moveable m = Car.getInstance(); m.run(); } } 分析: public static Car getInstance() { return car; } 对于产品Car来说,它不能被外部直接通过new关键字创建,而是要通过调用Car自身提供的静态方法返回,由于此方法控制了Car这种产品,而且还控制了该产品的生产过程,有一个叫法称该方法为静态工厂方法,简单说就是如果有一个方法是静态的而它有控制着某以对象的创建过程。 引申:多例 package org.chinasoft.dp.singleton; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Car implements Moveable { private static List<Car> cars = new ArrayList<Car>(); static { cars.add(new Car()); cars.add(new Car()); cars.add(new Car()); } private Car() { } public static Car getInstance() { Car car = null; for(Iterator<Car> iter = cars.iterator(); iter.hasNext(); ) { car = (Car)iter.next(); } return car; } public void run() { System.out.println("咔咔。。。"); } } 拥有一个保存该类对象的集合,在该类加载的时候,初始化该集合,调用getInstance()方法取得一个Car的实例。
简单工厂: 主要对产品品种的控制 产品类:Car.java package org.chinasoft.dp.factory; public class Car implements Moveable { private static Car car = new Car(); public Car() {} public static Car getInstance() { return car; } public void run() { System.out.println("咔咔。。。"); } } Car工厂类:CarFactory.java package org.chinasoft.dp.factory; public class CarFactory implements VehicleFactory { private static Car car = Car.getInstance(); public Moveable product() { return car; } } 工厂类的抽象类:Vehicle.java package org.chinasoft.dp.factory; public interface VehicleFactory { Moveable product(); } 交通工具的抽象类:Moveable.java package org.chinasoft.dp.factory; public interface Moveable { void run(); } 测试类:Test.java package org.chinasoft.dp.factory; public class Test { public static void main(String[] args) { //Car c = Car.getInstance(); //Plane p = Plane.getInstance(); //c.run(); //p.run(); //Moveable m = CarFactory.product(); //Moveable m = PlaneFactory.product(); VehicleFactory factroy = new CarFactory(); Moveable m = factroy.product(); m.run(); } } 现在如果要定制不同的交通工具,换掉对应的交通工具工厂实现类即可,程序很灵活。 抽象工厂: 主要对产品系列的控制。 产品类: Car.java package org.chinasoft.dp.factory.abstractfactory; public class Car extends Vehicle { public void run() { System.out.println("飞奔而去。。。"); } } Train.java package org.chinasoft.dp.factory.abstractfactory; public class Train extends Vehicle { public void run() { System.out.println("啦啦啦的开跑了。。"); } } AK47.java package org.chinasoft.dp.factory.abstractfactory; public class AK47 extends Weapon { public void shoot() { System.out.println("shoot..."); } } Sword.java package org.chinasoft.dp.factory.abstractfactory; public class Sword extends Weapon { public void shoot() { System.out.println("刷刷刷。。。"); } } Banana.java package org.chinasoft.dp.factory.abstractfactory; public class Banana extends Food { public void getColor() { System.out.println("yellow..."); } } Vehicle.java package org.chinasoft.dp.factory.abstractfactory; public abstract class Vehicle { public abstract void run(); } Weapon.java package org.chinasoft.dp.factory.abstractfactory; public abstract class Weapon { public abstract void shoot(); } Food.java package org.chinasoft.dp.factory.abstractfactory; public abstract class Food { public abstract void getColor(); } AFactory.java package org.chinasoft.dp.factory.abstractfactory; public class AFactory extends AbstractFactory { @Override public Food createFood() { return new Banana(); } @Override public Vehicle createVehicle() { return new Car(); } @Override public Weapon createWeapon() { return new AK47(); } } BFactory.java package org.chinasoft.dp.factory.abstractfactory; public class BFactory extends AbstractFactory { @Override public Food createFood() { return new Banana(); } @Override public Vehicle createVehicle() { return new Train(); } @Override public Weapon createWeapon() { return new Sword(); } } 抽象工厂类:AbstractFactory.java package org.chinasoft.dp.factory.abstractfactory; public abstract class AbstractFactory { public abstract Vehicle createVehicle(); public abstract Weapon createWeapon(); public abstract Food createFood(); } 测试类:Test.java package org.chinasoft.dp.factory.abstractfactory; public class Test { public static void main(String[] args) { AbstractFactory factory = new BFactory(); Vehicle vehicle = factory.createVehicle(); Weapon weapon = factory.createWeapon(); Food food = factory.createFood(); vehicle.run(); weapon.shoot(); food.getColor(); } } 具体的工厂控制着系列产品的生产,如果要换系列产品,直接换掉工厂即可。 分析:简单工厂和抽象工厂的优缺点 简单工厂:每一种产品都有它对应的产品工厂,不断的添加新的产品系列,会造成工厂泛滥现象。 抽象工厂:添加新的产品品种上,比如有个“T恤类”就必须要有它对应的抽象类,而且还要动抽象工厂对外的接口,具体的实现工厂也要动,改的地太多。 Simulate Spring的BeanFactory 产品类:Car.java package org.chinasoft.spring.simulate; public class Car implements Moveable { public void run() { System.out.println("car is running..."); } } Moveable.java package org.chinasoft.spring.simulate; public interface Moveable { void run(); } BeanFactory.java package org.chinasoft.spring.simulate; public interface BeanFactory { Object getBean(String id); } ClassPathXmlApplicationContext.java package org.chinasoft.spring.simulate; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String, Object> m = new HashMap<String, Object>(); public ClassPathXmlApplicationContext() throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(ClassPathXmlApplicationContext.class .getClassLoader().getResourceAsStream("org/chinasoft/spring/simulate/application.xml")); Element root = doc.getRootElement(); List<Element> Childern = root.getChildren(); for (Iterator<Element> iter = Childern.iterator(); iter.hasNext();) { Element child = (Element) iter.next(); String id = child.getAttributeValue("id"); String clazz = child.getAttributeValue("class"); //System.out.println(id + "----" + clazz); Object o = Class.forName(clazz).newInstance(); m.put(id, o); } } public Object getBean(String id) { return m.get(id); } } Plane.java package org.chinasoft.spring.simulate; public class Plane implements Moveable { public void run() { System.out.println("plane is flying..."); } } 配置文件: application.java <?xml version="1.0" encoding="utf-8" ?> <beans> <bean id="car" class="org.chinasoft.spring.simulate.Car"> </bean> <bean id="plane" class="org.chinasoft.spring.simulate.Plane"> </bean> </beans> 测试文件 Test.java package org.chinasoft.spring.simulate; public class Test { public static void main(String[] args) throws Exception { /* Moveable m = new Car(); m.run(); */ BeanFactory factory = new ClassPathXmlApplicationContext(); Moveable m = (Plane)factory.getBean("plane"); m.run(); } } Spring的BeanFactory,如果需要某个对象的时候,比如Moveable m = new Car(); 就不要直接new了,需要某个对象就通过配置文件读;在实现了BeanFactory的接口的子类中有一个Map这样的映射结果集,根据getBean(String id),返回给对应的实例化对象。 注意:以上方式是通过JDOM来解析XML,解析XML的方式有四种:DOM,SAX,JDOM,DOM4J,熟悉其中的一种解析操作即可。 使用java.util.Properties类解析XML 产品类: Car.java package org.chinasoft.spring.factory; public class Car implements Moveable { public void run() { System.out.println("飞奔而去。。。"); } } Plane.java package org.chinasoft.spring.factory; public class Plane implements Moveable { public void run() { System.out.println("云层中飞奔而去。。。"); } } Broom.java package org.chinasoft.spring.factory; public class Broom implements Moveable { public void run() { System.out.println("一路沙尘暴,疾驰而来。。。"); } } 实现的接口: Moveable.java package org.chinasoft.spring.factory; public interface Moveable { void run(); } 配置文件:spring.xml 注意:此配置文件的格式,是通过Properties类的public void storeToXML(OutputStream os, String comment) throws IOException方法生成的关于配置文件的相关操作语法,先只会使用即可。 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="car">org.chinasoft.spring.factory.Car</entry> <entry key="plane">org.chinasoft.spring.factory.Plane</entry> <entry key="broom">org.chinasoft.spring.factory.Broom</entry> </properties> 测试代码: Test2.java package org.chinasoft.spring.factory; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; public class Test2 { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { Properties props = new Properties(); try { props.loadFromXML(Test2.class.getClassLoader().getResourceAsStream( "org/chinasoft/spring/factory/spring.xml")); } catch (IOException e) { e.printStackTrace(); } Enumeration en = props.propertyNames(); Object o = null; while (en.hasMoreElements()) { String key = (String) en.nextElement(); String value = props.getProperty(key); System.out.println(key + "---" + value); o = Class.forName(value).newInstance(); Moveable m = (Moveable) o; m.run(); } } } Spring BeanFactory Car.java package org.chinasoft.spring.test; public class Car implements Moveable { public void run() { System.out.println("car is going..."); } } Moveable.java package org.chinasoft.spring.test; public interface Moveable { void run(); } 配置文件:applicationContext.java <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="car" class="org.chinasoft.spring.test.Car"> </bean> </beans> 测试类:Test.java package org.chinasoft.spring.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("org/chinasoft/spring/test/applicationContext.xml"); Object o = factory.getBean("car"); Moveable m = (Moveable)o; m.run(); } } 注意使用的是Spring的3.0版本,需要加入commons logging的jar文件。 现在需要一个对象,就不用直接通过new来创建了,直接从Spring中拿就可以了,给它一个ID Spring就返回给你一个Object的对象。 注意: 理解Spring中BeanFactory的基本实现原理。
posted on 2010-10-24 18:58 freehell 阅读(217) 评论(0) 编辑 收藏 举报
Powered by: 博客园 Copyright © 2024 freehell Powered by .NET 8.0 on Kubernetes