java设计模式---简单工厂模式/工厂模式/抽象工厂模式
简单工厂模式
一 简介
二 代码示例
工厂
public class FruitFactory {
// /*
// * 获得Apple类的实例
// */
// public static Fruit getApple() {
// return new Apple();
// }
//
// /*
// * 获得Banana类实例
// */
// public static Fruit getBanana() {
// return new Banana();
// }
/*
* get方法,获得所有产品对象
*/
}
角色
public class Apple implements Fruit{
/*
* 采集
*/
public void get(){
System.out.println("采集苹果");
}
}
public class Banana implements Fruit{
/*
* 采集
*/
public void get(){
System.out.println("采集香蕉");
}
}
抽象角色
public interface Fruit {
/*
* 采集
*/
public void get();
}
主函数
// //实例化一个Apple
// Fruit apple = FruitFactory.getApple();
// Fruit banana = FruitFactory.getBanana();
// apple.get();
// banana.get();
三 改进
工厂
/*
* get方法,获得所有产品对象
*/
public static Fruit getFruit(String type) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// if(type.equalsIgnoreCase("apple")) {
// return Apple.class.newInstance();
//
// } else if(type.equalsIgnoreCase("banana")) {
// return Banana.class.newInstance();
// } else {
// System.out.println("找不到相应的实例化类");
// return null;
// }
Class fruit = Class.forName(type);
return (Fruit) fruit.newInstance();
}
主函数
Fruit apple = FruitFactory.getFruit("Apple");
Fruit banana = FruitFactory.getFruit("Banana");
apple.get();
banana.get();
其它不变
简单工厂优缺点
工厂模式
概述
适合单类产品的排列组合
简单工厂的话 不符合封闭原则 (所以增加了一个角色工厂,这样再增加的时候不会修改工厂)
代码示例
工厂
public interface FruitFactory {
public Fruit getFruit();
}
角色
public class Apple implements Fruit{
/*
* 采集
*/
public void get(){
System.out.println("采集苹果");
}
}
public class Pear implements Fruit {
public void get() {
System.out.println("采集梨子");
}
}
角色工厂
public class AppleFactory implements FruitFactory {
public Fruit getFruit() {
return new Apple();
}
}
public class PearFactory implements FruitFactory {
public Fruit getFruit() {
return new Pear();
}
}
抽象角色
public interface Fruit {
/*
* 采集
*/
public void get();
}
主函数
public static void main(String[] args) {
//获得AppleFactory
FruitFactory ff = new AppleFactory();
//通过AppleFactory来获得Apple实例对象
Fruit apple = ff.getFruit();
apple.get();
//获得BananaFactory
FruitFactory ff2 = new BananaFactory();
Fruit banana = ff2.getFruit();
banana.get();
//获得PearFactory
FruitFactory ff3 = new PearFactory();
Fruit pear = ff3.getFruit();
pear.get();
}
与简单工厂对比
抽象工厂模式
概述
相比工厂方法模式来说有归类的思想,不适合排列组合
抽象工厂模式 对产品又进行了抽象 就拿苹果来说,不只是苹果。还分温室的,南方的,北方的。
代码示例
抽象角色
public interface Fruit {
/*
* 采集
*/
public void get();
}
工厂
public interface FruitFactory {
//实例化Apple
public Fruit getApple();
//实例化Banana
public Fruit getBanana();
}
角色
public abstract class Apple implements Fruit{
/*
* 采集
*/
public abstract void get();
}
public abstract class Banana implements Fruit{
/*
* 采集
*/
public abstract void get();
}
角色种类
public class NorthApple extends Apple {
public void get() {
System.out.println("采集北方苹果");
}
}
public class SouthApple extends Apple {
public void get() {
System.out.println("采集南方苹果");
}
}
public class WenshiApple extends Apple {
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 SouthFruitFactory implements FruitFactory {
public Fruit getApple() {
return new SouthApple();
}
public Fruit getBanana() {
return new SouthBanana();
}
}
public class WenshiFruitFactory implements FruitFactory {
public Fruit getApple() {
return new WenshiApple();
}
public Fruit getBanana() {
return new WenshiBanana();
}
}
主函数
public static void main(String[] args) {
FruitFactory ff = new NorthFruitFactory();
Fruit apple = ff.getApple();
apple.get();
Fruit banana = ff.getBanana();
banana.get();
FruitFactory ff2= new SouthFruitFactory();
Fruit apple2 = ff2.getApple();
apple2.get();
Fruit banana2 = ff2.getBanana();
banana2.get();
FruitFactory ff3 = new WenshiFruitFactory();
Fruit apple3 = ff3.getApple();
apple3.get();
Fruit banana3 = ff3.getBanana();
banana3.get();
}