public class Demo {
public static void main(String[] args) {
Car car = new Car();
car.run();
}
}
public class Car {
public void run(){
System.out.println("汽车正在向前跑...");
}
}
复制代码
new
产生一辆汽车, 而是应该经过调用Car
类中的某个方法对外提供车.public class Car {
private static Car car = new Car();//用于提供给外界, 始终是同一辆车
private Car(){};//私有构造方法, 在类以外不能经过new得到本类对象了, 保证了单例
public Car getInstance(){
return car;
}
public void run(){
System.out.println("汽车正在向前跑...");
}
}
public static void main(String[] args) {
Car car = Car.getInstance();
car.run();
}
复制代码
Moveable
接口, 在接口中声明run()
方法, 全部的交通工具类都实现该接口.public interface Moveable {
void run();
}
public class Car implements Moveable{
public Car(){};//私有构造方法, 在类以外不能经过new得到本类对象了, 保证了单例
public void run(){
System.out.println("汽车正在向前跑...");
}
}
public abstract class VehicleFactory {
public abstract Moveable create();
}
public class CarFactory extends VehicleFactory {
@Override
public Moveable create() {
return new Car();
}
}
//Test
public static void main(String[] args) {
VehicleFactory factory = new CarFactory();
Moveable m = factory.create();
m.run();
}
复制代码
//test
public static void main(String[] args) {
AbstractFactory factory = new Factory1();
Vehiche v = factory.createVehiche();
Weapon w = factory.createWeapon();
Food f = factory.createFood();
v.run();
w.fire();
f.eat();
}
public abstract class Vehiche {//交通工具的抽象类
public abstract void run();
}
public abstract class Weapon {//武器的抽象类
public abstract void fire();
}
public abstract class Food {//食物的抽象类
public abstract void eat();
}
public class Car extends Vehiche{一种具体的交通工具
@Override
public void run() {
System.out.println("小汽车启动...");
}
}
public class AK47 extends Weapon {//一种具体的武器
@Override
public void fire() {
System.out.println("哒哒哒...");
}
}
public class Apple extends Food{//一种具体的食物
@Override
public void eat() {
System.out.println("大口吃苹果...");
}
}
//抽象工厂
public abstract class AbstractFactory {
public abstract Vehiche createVehiche();
public abstract Weapon createWeapon();
public abstract Food createFood();
}
//抽象工厂的实现1
public class Factory1 extends AbstractFactory {
@Override
public Vehiche createVehiche() {
return new Car();
}
@Override
public Weapon createWeapon() {
return new AK47();
}
@Override
public Food createFood() {
return new Apple();
}
}
复制代码
盔甲
抽象类, 那么抽象工厂以及对应的实现都要修改源码了.spring
的工厂实现, 它给出了一种解决方案.
Moveable
接口, 里面有run
方法, Car
小汽车类实现了该接口.public static void main(String[] args) {
Moveable m = new Car();
m.run();
}
public interface Moveable {
void run();
}
public class Car implements Moveable{
@Override
public void run() {
System.out.println("小汽车往前跑...");
}
}
复制代码
new
关键字获取的, 而是经过配置文件获取的.class
对象, 而后经过class
对象建立具体的实例对象.public static void main(String[] args) throws Exception {
//获取配置文件
Properties props = new Properties();
props.load(Test.class.getClassLoader().getResourceAsStream("spring.properties"));
//获取配置文件中配置的类
String vehicheTypeName = props.getProperty("vehicheTypeName");
//反射生成对应的对象
Moveable m = (Moveable) Class.forName(vehicheTypeName).newInstance();
m.run();
}
//spring.properties
vehicheTypeName=designPattern.factory.springFactory.Car
复制代码
spring
中bean工厂使用的模拟, 下面咱们使用真实的spring
来生成Car
对象, 对比一下.public static void main(String[] args) throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
Vehiche v = (Vehiche)bf.getBean("v");
v.run();
}
//配置文件
<bean id="v" class="designPattern.factory.Car">
</bean>
复制代码
spring
使用起来就是这么简单, 下面咱们模拟一下spring的bean工厂实现.
spring
是个bean
容器, 如下的代码将展现它是如何生成bean
, 并把bean
放入容器中供用户获取的.BeanFactory
工厂接口, 添加方法getBean()
.BeanFactory
的实现类ClassPathXmlApplicationContext
. 将在该实现类中展现IOC的具体实现.ClassPathXmlApplicationContext
须要一个container
容器存放建立的bean对象, 这里使用HashMap
实现.ClassPathXmlApplicationContext
的构造方法中读取spring
的配置文件, 这里使用到了dom4j
. 读取配置文件后根据bean
的class
属性使用反射建立出bean
对象. 而后把id
和bean
对象分别做为key
和value
添加到容器中.getBean()
方法时, 从容器中找到对应的bean
并返回.public static void main(String[] args) throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
Vehiche v = (Vehiche) bf.getBean("v");
v.run();
}
//BeanFactory的实现类
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String, Object> container = new HashMap<>();//用于存放bean对象的容器
//在构造方法中读取xml配置文件, 把bean对象都建立好并放入容器中
public ClassPathXmlApplicationContext(String propAddr) throws Exception {
SAXReader reader = new SAXReader();
File file = new File(this.getClass().getClassLoader().getResource(propAddr).toURI());
Document document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
Object bean = Class.forName(child.attributeValue("class")).newInstance();
container.put(child.attributeValue("id"), bean);
}
}
@Override
public Object getBean(String beanId) {
return container.containsKey(beanId) ? container.get(beanId) : null;
}
}
//极简BeanFactory
public interface BeanFactory {
Object getBean(String beanId);
}
//xml中配置的bean
<bean id="v" class="designPattern.factory.Car">
</bean>
复制代码