一、简单工厂模式
提供一个工厂,负责对象的创建。当需要使用对象时,直接从工厂获取。
public interface Fruit {
void get();
}
public class Apple implements Fruit{
/*
采集
*/
public void get(){
System.out.println("采集苹果");
}
}
public class Banana implements Fruit{
/*
采集香蕉
*/
public void get(){
System.out.println("采集香蕉");
}
}
public class FruitFactory {
/*
第一种写法
// 获得Apple实例
public static Fruit getApple(){
return new Apple();
}
// 获得Banana实例
public static Fruit getBanana(){
return new Banana();
}
*/
public static Fruit getFruit(String type) throws IllegalAccessException, InstantiationException {
/* 第二种写法
if(type.equals("apple")){
return Apple.class.newInstance();
}else if(type.equals("banana")){
return Banana.class.newInstance();
}else {
System.out.println("找不到实例");
return null;
}
*/
/* 第三种写法
try {
Class fruit = Class.forName(type);
return (Fruit) fruit.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
*/
}
}
public class MainClass {
public static void main(String args[]) throws InstantiationException, IllegalAccessException {
// Fruit apple = new Apple();
// apple.get();
// Fruit banana = new Banana();
// banana.get();
// Fruit apple = FruitFactory.getApple();
// Fruit banana = FruitFactory.getBanana();
// apple.get();
// banana.get();
Fruit apple = FruitFactory.getFruit("Apple");
Fruit banana = FruitFactory.getFruit("Banana");
apple.get();
banana.get();
}
}
优点:工厂可以隐藏创建的对象的 “加工”细节 。当我们需要获取对象的时候,不需要自己实现对象的创建过程,避免了麻烦和难以维护的问题。
缺点:当对象类型很多时,简单工厂类很庞大臃肿、耦合性高。而且增加、删除某个子类对象的创建都需要修改简单工厂类代码,,不符合开闭原则。
二、工厂方法模式
public interface FruitFactory {
/*
*/
Fruit getFruit();
}
public class AppleFactory implements FruitFactory {
public Fruit getFruit(){
return new Apple();
}
}
public class BananaFactory implements FruitFactory {
public Fruit getFruit(){
return new Banana();
}
}
public class MainClass {
public static void main(String args[]) throws InstantiationException, IllegalAccessException {
// 获得AppleFactory
FruitFactory applefactory = new AppleFactory();
// 通过AppleFactory来获得Apple实例对象
Fruit apple = applefactory.getFruit();
apple.get();
// 获得BananaFactory
FruitFactory bananafactory = new BananaFactory();
// 通过BananaFactory来获得Banana实例对象
Fruit banana = bananafactory.getFruit();
banana.get();
}
}
工厂方法模式中一个子类对应一个工厂类。它把原来臃肿庞大的简单工厂类,拆分成了一个个的工厂类,对简单工厂模式进行了解耦。而这些工厂类都实现于一个抽象接口。
优点:增加功能只需新增类和相应工厂类,不需要修改代码。符合开闭原则。
缺点:增加较多的代码量。
三、抽象工厂模式
使用场景:提供一个工厂,创建一系列相关的或相互依赖的对象,而无需指定指定具体的类。
比如GreenHouseApple创建所有温室生产的水果,NorthFruitFactory 创建所有北方生产的水果…
public class GreenHouseApple extends Apple {
public void get(){
System.out.println("采集温室苹果");
}
}
public class GreenHouseBanana extends Banana {
public void get(){
System.out.println("采集温室香蕉");
}
}
public class GreenHouseFruitFactory implements FruitFactory {
public Fruit getApple(){
return new GreenHouseApple();
}
public Fruit getBanana(){
return new GreenHouseBanana();
}
}
public class NorthApple extends Apple {
public void get(){
System.out.println("采集北方苹果");
}
}
public class NorthBanana extends Banana {
public void get(){
System.out.println("采集北方香蕉");
}
}
public class NorthFruitFactory implements FruitFactory {
public Fruit getApple(){
return new NorthApple();
}
public Fruit getBanana(){
return new NorthBanana();
}
}
public class SouthApple extends Apple {
public void get(){
System.out.println("采集南方苹果");
}
}
public class SouthBanana extends Banana {
public void get(){
System.out.println("采集南方香蕉");
}
}
public class SouthFruitFactory implements FruitFactory {
public Fruit getApple(){
return new SouthApple();
}
public Fruit getBanana(){
return new SouthBanana();
}
}
public class MainClass {
public static void main(String args[]){
FruitFactory northFactory = new NorthFruitFactory();
Fruit northApple = northFactory.getApple();
northApple.get();
Fruit northBanana = northFactory.getBanana();
northBanana.get();
FruitFactory southFactory = new SouthFruitFactory();
Fruit southApple = southFactory.getApple();
southApple.get();
Fruit southBanana = southFactory.getBanana();
southBanana.get();
FruitFactory greenFactory = new GreenHouseFruitFactory();
Fruit greenHouseApple = greenFactory.getApple();
greenHouseApple.get();
Fruit greenHouseBanana = greenFactory.getBanana();
greenHouseBanana.get();
}
}