[color=green][b]首先先创建一个接口Fruit.java,我们的工厂就是专门造水果的[/b][/color]
[color=green][b]我们的工厂可以早的水果是那些呢??就以苹果、香蕉、橘子为例[/b][/color]
[color=green][b]然后是一个配置文件 resouse.properties。实际上就相当于类别清单。[/b][/color]
[color=green][b]对配置文件呢要有一个处理类:InitProperty.java[/b][/color]
[color=green][b]接下来最重要的是我们的工厂类,这个类仔细瞧好了我们用单例模式来把它写出FactoryOfFruit.java[/b][/color]
[color=green][b]我们这个工厂到底怎么样呢,能不能生产水果呢。那就让我们测试一下写我们的Test.java[/b][/color]
[color=green][b]
结果:
***吃香蕉****
[/b][/color]
[color=green][b]
接下来你就可以吃自己想要的水果喽!!
[/b][/color]
package factory.myinterface;
/**
* @author zoupengfei
*
*/
public interface Fruit {
final String name="";
public void eated();
}
[color=green][b]我们的工厂可以早的水果是那些呢??就以苹果、香蕉、橘子为例[/b][/color]
//苹果
package factory.myimpl;
import factory.myinterface.Fruit;
/**
* @author zoupengfei
*
*/
public class Apple implements Fruit {
@Override
public void eated() {
System.out.println("***吃苹果****");
}
}
//香蕉
package factory.myimpl;
import factory.myinterface.Fruit;
/**
* @author zoupengfei
*
*/
public class Banana implements Fruit {
@Override
public void eated() {
System.out.println("***吃香蕉****");
}
}
//橘子
package factory.myimpl;
import factory.myinterface.Fruit;
/**
* @author zoupengfei
*
*/
public class Orange implements Fruit {
@Override
public void eated() {
System.out.println("***吃橘子****");
}
}
[color=green][b]然后是一个配置文件 resouse.properties。实际上就相当于类别清单。[/b][/color]
apple=myimpl.Apple
orange=myimpl.Orange
banana=myimpl.Banana
[color=green][b]对配置文件呢要有一个处理类:InitProperty.java[/b][/color]
package factory.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* @author zoupengfei
*
*/
public class InitProperty {
public static Properties getProperty(String filePath){
Properties pro=new Properties();
File file=new File(filePath);
if(file!=null){
try {
FileInputStream in=new FileInputStream(file);
pro.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
pro.setProperty("apple", "myimpl.Apple");
}
return pro;
}
}
[color=green][b]接下来最重要的是我们的工厂类,这个类仔细瞧好了我们用单例模式来把它写出FactoryOfFruit.java[/b][/color]
package factory.myfactory;
import java.util.Properties;
import factory.myinterface.Fruit;
import factory.util.InitProperty;
/**
* @author zoupengfei
*
*/
public class FactoryOfFruit {
private static FactoryOfFruit factoryOfFruit;
private Properties pro=new Properties();
private FactoryOfFruit(){
}
public static FactoryOfFruit getInstance(){
if(factoryOfFruit==null){
synchronized(FactoryOfFruit.class) {
factoryOfFruit=new FactoryOfFruit();
}
}
return factoryOfFruit;
}
public Fruit getFruitOf(String sort){
Fruit f=null;
try {
f=(Fruit) Class.forName(pro.getProperty(sort)).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return f;
}
public void init(String filePath){
pro=InitProperty.getProperty(filePath);
}
}
[color=green][b]我们这个工厂到底怎么样呢,能不能生产水果呢。那就让我们测试一下写我们的Test.java[/b][/color]
package factory.test;
import factory.myfactory.FactoryOfFruit;
import factory.myinterface.Fruit;
/**
* @author zoupengfei
*
*/
public class Test {
public static void main(String[] args) {
FactoryOfFruit factory=FactoryOfFruit.getInstance();
factory.init("src/resouse.properties");
Fruit f=factory.getFruitOf("banana");
f.eated();
}
}
[color=green][b]
结果:
***吃香蕉****
[/b][/color]
[color=green][b]
接下来你就可以吃自己想要的水果喽!!
[/b][/color]