1.写一个简单的工厂模式
package org.sh.factoryDemo01;
interface Fruit{
public void eat();
}
class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果");
}
}
class Orange implements Fruit{
@Override
public void eat() {
System.out.println("吃橘子");
}
}
class Factory{
public static Fruit getInstance(String className){
Fruit f = null;
if(className.equals("apple")){
f = new Apple();
}
if(className.equals("orange")){
f = new Orange();
}
return f;
}
}
public class FactoryDemo01{
public static void main(String[] args) {
Fruit f = Factory.getInstance("apple");
f.eat();
}
}
这样的模式正确 但是存在缺点,在工厂操作中因为每次只要以增加子类,则必须修改工厂,那么这个时候可以根据反射机制完成
通过发射机制来实现:
package org.sh.factoryDemo01;
interface Fruit{
public void eat();
}
class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果");
}
}
class Orange implements Fruit{
@Override
public void eat() {
System.out.println("吃橘子");
}
}
class Factory{
public static Fruit getInstance(String className){
Fruit f = null;
try {
f = (Fruit) Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return f;
}
}
public class FactoryDemo01{
public static void main(String[] args) {
Fruit f = Factory.getInstance("org.sh.factoryDemo01.Apple");
f.eat();
}
}
以上实现 工厂可以完全不用改变,但是每次都要输入很长的包.类名称
改进:使用属性配置文件
package org.sh.factoryDemo02.copy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
interface Fruit{
public void eat();
}
class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果");
}
}
class Orange implements Fruit{
@Override
public void eat() {
System.out.println("吃橘子");
}
}
class PropertiesOperate{
private Properties pro = null;
private File file = new File("D:"+File.separator+"fruit.properties");
public PropertiesOperate(){
pro = new Properties();
if(file.exists()){
try {
pro.loadFromXML(new FileInputStream(file));
} catch (InvalidPropertiesFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
this.save();
}
}
public void save(){
this.pro.setProperty("apple", "org.sh.factoryDemo02.copy.Apple");
this.pro.setProperty("orange", "org.sh.factoryDemo02.copy.Orange");
try {
this.pro.storeToXML(new FileOutputStream(this.file), "Fruit");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Properties getProperties(){
return this.pro;
}
}
class Factory{
public static Fruit getInstance(String className){
Fruit f = null;
try {
f = (Fruit) Class.forName(className).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return f;
}
}
public class FactoryDemo01{
public static void main(String[] args) {
Properties pro = new PropertiesOperate().getProperties();
System.out.println("**********"+pro.getProperty("apple"));
Fruit f = Factory.getInstance(pro.getProperty("apple"));
f.eat();
}
}