任何可以产生对象的方法或者类,都可以称之为工厂。单例就是所谓的静态工厂。
为什么jdk中有了new,还需要工厂呢?
a、灵活的控制生产过程
b、给对象加修饰、或者给对象加访问权限,或者能够在对象生产过程中添加一些日志信息,再或者根据应用场景添加一些实际的业务处理等等。
1、静态工厂
单例模式:一种特殊的静态工厂,请参考单例模式
2、简单工厂
/** * 可移动的物体抽象接口
*/
public interface Moveble { }
/**
* 食品抽象类
*/
public abstract class Food{
}
/**
* 面包类
*/
public class bread extends Food {
public Bike(){ System.out.println("T am bread"); } }
/** * bike类 */ public class Bike implements Moveble{ public Bike(){ System.out.println("T am bike"); } }
/** * car类 */ public class Car implements Moveble{ public Car(){ System.out.println("I am car"); } }
/** * 简单工厂 只要是类都可以生产 */ public class SimplerFactory { public Moveble createCar(){ return new Car(); } public Moveble createBike(){ return new Bike(); }
public Food createBread(){
return new Bread(); }
}
/** * 测试类 */ public class T { public static void main(String[] args) { //生产car Moveble car = new SimplerFactory().createCar(); //生产bike Moveble bike = new SimplerFactory().createBike();
//生产bike
Food bread = new SimplerFactory().createBread();
}
}
3、工厂方法
/** * 可移动的物体抽象接口 */ public abstract interface Moveble { } /** * 食品抽象类 */ public abstract class Food { } /** * bike类 */ public class Bike implements Moveble{ public Bike(){ System.out.println("T am bike"); } } /** * car类 */ public class Car implements Moveble{ public Car(){ System.out.println("I am car"); } } /** * bread类 */ public class Bread extends Food{ public Bread(){ System.out.println("T am bread"); } } /** * bike工厂 */ public class BikeFactory { public Moveble create(){ return new Bike(); } } /** * car工厂 */ public class CarFactory { public Moveble create(){ return new Car(); } } /** * bread工厂 */ public class BreadFactory { public Food create(){ return new Bread(); } } /** * 测试类 ,工厂方法 一种类一个工厂 */ public class T { public static void main(String[] args) { //生产car Moveble car = new BikeFactory().create(); //生产bike Moveble bike = new CarFactory().create(); //生产bread Food bread = new BreadFactory().create(); } }
4、抽象工厂
/** * 食品抽象类 */ public abstract class Food { } /** * 交通工具抽象类 */ public abstract class Transportation { } /** * 电子设备抽象类 */ public abstract class Electronic { } /** * bread类 */ public class Bread extends Food { public Bread(){ System.out.println("T am bread"); } } /** * plane类 */ public class Plane extends Transportation{ public Plane(){ System.out.println("I am plane"); } } /** * phone类 */ public class Phone extends Electronic{ public Phone(){ System.out.println("I am phone"); } } /** * 具体的工厂 可以生产一组类 */ public class HaierFactory extends AbstractFactory{ Food createFood() { return new Bread(); } Electronic createElectronic() { return new Phone(); } Transportation createTransportation() { return new Plane(); } } /** * cake类 */ public class Cake extends Food { public Cake(){ System.out.println("T am cake"); } } /** * Tank类 */ public class Tank extends Transportation{ public Tank(){ System.out.println("I am tank"); } } /** * Computer类 */ public class Computer extends Electronic{ public Computer(){ System.out.println("T am pc"); } } /** * 具体的工厂,可以生产一组类 */ public class SuningFactory extends AbstractFactory{ Food createFood() { return new Cake(); } Electronic createElectronic() { return new Computer(); } Transportation createTransportation() { return new Tank(); } } /** * 测试类,抽象工厂可以生产一组类,并且可以很灵活的扩展一组类的创建过程,但是不能做到灵活扩展一种类 */ public class T { public static void main(String[] args) { //海尔工厂可以生产面包 飞机 手机 AbstractFactory haierFactory = new HaierFactory(); Food bread = haierFactory.createFood(); Transportation plane = haierFactory.createTransportation(); Electronic phone = haierFactory.createElectronic(); //苏宁工厂可以生产 蛋糕 坦克 pc电脑 AbstractFactory suningFactory = new SuningFactory(); Food cate = haierFactory.createFood(); Transportation tank = haierFactory.createTransportation(); Electronic computer = haierFactory.createElectronic(); } }
5、Spring的bean工厂
既可以灵活控制一组类的创建过程也可以灵活控制一种类的创建过程,具体请去阅读Spring源码中的BeanFactory。