一.简单工厂模式
//接口
package Factory;
public interface Fruit {
public void eat(); //吃
}
//实现接口类
package Factory;
public class Apple implements Fruit{
public Apple() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃苹果!");
}
}
package Factory;
public class Banana implements Fruit{
public Banana() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃香蕉!");
}
}
package Factory;
public class Orange implements Fruit{
public Orange() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃橘子");
}
}
//工厂类
package Factory;
public class Factory {
public Factory() {
// TODO Auto-generated constructor stub
}
public static Fruit getInstance(String className){
if("apple".equals(className))
return new Apple(); //返回一个对象
if("banana".equals(className))
return new Banana();
if("orange".equals(className))
return new Orange();
return null;
}
}
//主方法
package Factory;
import java.util.Scanner;
public class FactoryTest {
public FactoryTest() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
String className=scanner.nextLine();//输入想要创建对象的名字
Fruit f=Factory.getInstance( className); //调用并接收
f.eat();
}
}
运行结果:
这个工厂类是最简单的工厂类,可以体会到工厂模式的设计思想,但是每次想添加一个类都需要更改大量的代码,所以后来演变了一个更方便使用的工厂模式,利用反射机制创建对象,从而减少对代码的修改。
如果想再添加一个葡萄类
package Factory;
public class Grape implements Fruit{
public Grape() {
// TODO Auto-generated constructor stub
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃葡萄!");
}
}
//要在返回对象那多加一个创建葡萄类
package Factory;
public class Factory {
public Factory() {
// TODO Auto-generated constructor stub
}
public static Fruit getInstance(String className){
if("apple".equals(className))
return new Apple();
if("banana".equals(className))
return new Banana();
if("orange".equals(className))
return new Orange();
if("grape".equals(className))
return new Grape(); //这里还要修改
return null;
}
}
运行结果:
这样对代码的修改量会增大,这只是一个简单的工厂模式,若果放到一个项目当中那修改的代码量远远比这个要多很多。
二.反射之工厂模式
//接口
package fanshe;
public interface Fruit {
public void eat(); //吃
}
//实现接口的类
package fanshe;
public class Apple implements Fruit {
public Apple() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃苹果~!");
}
}
package fanshe;
public class Banana implements Fruit{
public Banana() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃香蕉~!");
}
}
package fanshe;
public class Orange implements Fruit {
public Orange() {
// TODO Auto-generated constructor stub
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃橘子~!");
}
}
//工厂类
package fanshe;
public class Factory {
public Factory() {
// TODO Auto-generated constructor stub
}
public static Fruit getInstance(String className)
{
/*if("orange".equals(className))
return new Orange();
return null;*/
Fruit fruit=null;
try {
fruit=(Fruit) Class.forName(className).newInstance();//创建对象
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fruit;
}
}
//主方法
package fanshe;
import java.util.Scanner;
public class FactortDemo {
public FactortDemo() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
String className=scanner.nextLine(); //输入想要创建类的名字
Fruit fruit=Factory.getInstance("fanshe."+className);
fruit.eat();
}
}
运行结果:
如果再想添加一个葡萄类直接添加类就可以了,不用再修改其他代码,减少了代码的修改量
package fanshe;
public class Grape implements Fruit{
public Grape() {
// TODO Auto-generated constructor stub
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃葡萄~!");
}
}
运行结果: