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>
複製代碼