本文是学习java与模式后的总结!
一、简介
工厂模式专门负责将大量有共同接口的类实例化。工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类。
下面是使用工厂模式的两种情况:
1.在编码时不能预见需要创建哪种类的实例。
2.系统不应依赖于产品类实例如何被创建、组合和表达的细节
工厂模式的几种形态:
(1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory MethodPattern)。
(2)工厂方法(Factory Method)模式,又称多态性工厂(Polymorphic Factory)模式或虚拟构造子(Virtual Constructor)模式;
(3)抽象工厂(Abstract Factory)模式,又称工具箱(Kit 或Toolkit)模式。
二、简单工厂模式
来个例子,有一个农场公司,专门向市场销售各类水果。在这个系统里需要描述下列的水果:
//水果接口
public interface Fruit{
//生长
void grow();
//收获
void harvest();
//种植
void plant();
}
//苹果类
public class Apple implements Fruit{
private int treeAge;
public void grow(){
log("Apple is growing...");
}
public void harvest(){
log("Apple has been harvested.");
}
public void plant(){
log("Apple has been planted.");
}
public static void log(String msg){
System.out.println(msg);
}
public int getTreeAge(){
return treeAge;
}
public void setTreeAge(int treeAge){
this.treeAge = treeAge;
}
}
其他的水果类与其类似,略了。。。
//果园园丁类
public class FruitGardener
{
//静态工厂方法,返回值类型为水果接口类型
public static Fruit factory(String which){
if (which.equalsIgnoreCase("apple"))
{
return new Apple();
}
else if (which.equalsIgnoreCase("strawberry"))
{
return new Strawberry();
}
else if (which.equalsIgnoreCase("grape"))
{
return new Grape();
}
//这里如果没有对应的应该跑出异常,省略不写了。麻烦的,返回个空吧。嘿嘿
return null;
}
}
好了,现在客户端只要调用园丁的静态工厂方法,水果就出来了。
//客户类
public class client{
public static void main(String[] args) {
Fruit a=FruitGardener.factory("grape");
。。。。
}
}
简单工厂模式的结构
简单工厂模式在某种程度上支持“开-闭”原则,但是支持的还不够,因为如果有新的产品加入到系统中,就要去修改工厂类,将必要的逻辑加入到工厂类中。由此引入了工厂方法模式。
三、工厂方法模式
工厂方法模式的结构:
来个小例子。
public interface Moveable {
//产品抽象类角色
public void run();
}
public class Car implements Moveable{
//具体产品类角色1
public void run(){
System.out.println("冒着烟奔跑run........");
}
}
public class Plane implements Moveable {
//具体产品类角色2
public void run() {
System.out.println("扇着翅膀奔跑plane");
}
}
public interface VehicleFactory {
//抽象工厂类角色
Moveable creat();
}
public class CarFactory implements VehicleFactory{
//具体工厂类角色1
public Moveable creat() {
return new Car();
}
}
public class PlaneFactory implements VehicleFactory{
//具体工厂类角色2
public Moveable creat() {
return new Plane();
}
}
//客户类
public class client{
public static void main(String[] args) {
//VehicleFactory v=new CarFactory();
VehicleFactory v=new PlaneFactory();
v.creat().run();
}
}
在工厂方法模式中,如果系统需要加入一个新产品,那么所需要的就是向系统中加入一个这个产品类以及它对应的工厂类,没必要修改抽象工厂角色或其他具体工厂角色,对于新增产品类而言,这个系统完全支持“开-闭”原则。
四、抽象工厂模式
工厂方法模式与抽象工厂方法的最大区别在于工厂方法是针对一个产品的,而抽象工厂针对的是一系列的产品。
例子如下:
//抽象工厂
public abstract class AbstractFactory {
//生产车辆
public abstract Vehicle createVehicle();
//生产武器
public abstract Weapon createWeapon();
//生产实物
public abstract Food createFood();
}
//具体工厂类角色(魔法工厂)
public class MagicFactory extends AbstractFactory {
public Food createFood() {
return new MushRoom();
}
public Vehicle createVehicle() {
return new Broom();
}
public Weapon createWeapon() {
return new MagicStick();
}
}
//具体工厂类角色(普通工厂)
public class DefaultFactory extends AbstractFactory{
public Food createFood() {
return new Apple();
}
public Vehicle createVehicle() {
return new Car();
}
public Weapon createWeapon() {
return new AK47();
}
}
//抽象产品类1
public abstract class Vehicle {
public abstract void run();
}
//抽象产品类2
public abstract class Weapon {
public abstract void shoot();
}
//抽象产品类3
public abstract class Food {
public abstract void printName();
}
//具体产品类1
public class Car extends Vehicle {
public void run() {
System.out.println("冒着烟奔跑中car.......");
}
}
//具体产品类2
public class AK47 extends Weapon{
public void shoot() {
System.out.println("哒哒哒...");
}
}
//具体产品类3
public class Apple extends Food {
public void printName() {
System.out.println("apple");
}
}
//客户类
public class client{
public static void main(String[] args) {
AbstractFactory f = new DefaultFactory();
Vehicle v = f.createVehicle();
v.run();
Weapon w = f.createWeapon();
w.shoot();
Food a = f.createFood();
a.printName();
}
}
一个抽象工厂对应的是一系列的抽象产品。