1.基础的简单工厂类
//简单工厂设计模式
interface Vehicle //写一个接口,但是我更喜欢叫它标准,所有符合这个标准的类均归为同一类
{
public void howToWork();
}
class Car implements Vehicle //汽车类实现交通工具接口(或者说是汽车类符合交通工具标准)
{
public void howToWork()
{
System.out.println("开车去!");
}
}
class Bike implements Vehicle
{
public void howToWork()
{
System.out.println("骑车去!");
}
}
class Airplane implements Vehicle
{
public void howToWork()
{
System.out.println("坐飞机去!");
}
}
class Factory //工厂类
{
public static Vehicle create(String vehicle) //实现一个静态方法用于创建实例
{
Vehicle sample = null;
switch(vehicle)
{
case "car":
sample = new Car();
break;
case "bike":
sample = new Bike();
break;
case "airplane" :
sample = new Airplane();
break;
}
return sample;
}
}
public class SimpleFactory
{
public static void main(String args[])
{
Vehicle vehicle = Factory.create(args[0]);
vehicle.howToWork();
}
}
2.利用反射实现简单工厂类的解耦合
package reflect;
//工厂设计模式升级版,简单工厂也可以解决之前难以维护的问题
interface Fruit // 定义接口
{
public void eat();
}
class Apple implements Fruit
{
@Override
public void eat()
{
// TODO Auto-generated method stub
System.out.println("eat Apple");
}
}
class Banana implements Fruit
{
@Override
public void eat()
{
// TODO Auto-generated method stub
System.out.println("eat Banana");
}
}
class Orange implements Fruit
{
@Override
public void eat()
{
// TODO Auto-generated method stub
System.out.println("eat Orange");
}
}
class Factory
{
public static Fruit getInstance(String className) throws Exception
{
Fruit f = null;
f = (Fruit) Class.forName(className).newInstance(); //利用反射实现实例化Fruit的子类
return f;
}
}
public class TestFactory
{
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
Fruit f = Factory.getInstance("reflect.Orange");
f.eat();
}
}
3.总结:
基础的简单工厂类最大的问题就是新增子类后,修改代码的问题,但是有了反射机制的引入,实现了可以任意增加子类且不会增加代码的维护难度。
4.使用属性配置文件让类的创建更加灵活
还是拿水果来举例子
Fruit.java
package cn.limbo.study;
/**
* Created by limbo on 2016/10/31.
*/
public interface Fruit {
void eat();
}
Apple.java
package cn.limbo.study;
/**
* Created by limbo on 2016/10/31.
*/
public class Apple implements Fruit {
@Override
public void eat() {
System.out.println("eat apple");
}
}
Orange.java
package cn.limbo.study;
/**
* Created by limbo on 2016/10/31.
*/
public class Orange implements Fruit {
@Override
public void eat() {
System.out.println("Orange");
}
}
FruitFactory.java
package cn.limbo.study;
/**
* Created by limbo on 2016/10/31.
*/
public class FruitFactory {
public static Fruit getFruit(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Fruit fruit = (Fruit) Class.forName(className).newInstance();
return fruit;
}
}
ReadProperties.java
package cn.limbo.study;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Created by limbo on 2016/10/31.
*/
public class ReadProperties {
public static Properties getProperties() throws IOException {
Properties pro = new Properties();
File f = new File("src/fruit.properties");
if(f.exists()) {
pro.load(new FileInputStream(f));
}else{
System.out.println("null");
}
return pro;
}
}
fruit.properties
apple=cn.limbo.study.Apple
orange=cn.limbo.study.Orange
test.java
@Test
public void eatFruit() {
Properties pro = null;
Fruit apple = null;
try {
pro = ReadProperties.getProperties();
apple = FruitFactory.getFruit(pro.getProperty("apple"));
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(apple != null){
apple.eat();
}else{
System.out.println("null");
}
}