工厂模式
一、工厂模式的背景
1、工厂的概念
任何可以产生对象的类或者是方法,都可以称为一个工厂。
2、为什么要有工厂?
我们知道,得到对象直接new一个就可以了,为什么要有通过类或者方法去得到呢?主要是为了能更灵活的修改与维护,试想:如果都通过new去得到,当new的次数较多时,与代码耦合度较高,后期改动会非常麻烦。
二、工厂模式的实现
1)工厂方法
- 灵活扩展产品类型
可以定义一个接口或者父类,然后子类去实现接口或者继承父类,就可以很灵活的拓展产品类型,符合开闭原则。
注意:形容词用接口,名词用抽象类
例子:以交通工具为例子
package Factory_Pattern;
public interface Moveable {
void go();
}
package Factory_Pattern;
public class Plane implements Moveable{
@Override
public void go() {
// TODO Auto-generated method stub
System.out.println("Plane fly ------");
}
}
package Factory_Pattern;
public class Car implements Moveable{
public void go() {
System.out.println("Car go wuwuwuuwuwuw----");
}
}
package Factory_Pattern;
public class Train implements Moveable{
@Override
public void go() {
// TODO Auto-generated method stub
System.out.println("Train is moving hooooo------");
}
}
- 灵活修改生产过程
灵活修改生产过程肯定要用到工厂类,为了符合开闭原则,每一个子类都需要有相应的工厂类,例如Car类需要有CarFactory去生产该类。
提示:工厂类应该用到单例模式
,称为:单例工厂
例子:
package Factory_Pattern;
public class PlaneFactory {
private PlaneFactory() {
}
private static volatile PlaneFactory INSTANCE;
PlaneFactory getInstance() {
if(INSTANCE==null)
synchronized(PlaneFactory.class) {
if(INSTANCE==null)
INSTANCE=new PlaneFactory();
}
return INSTANCE;
}
//注意这里最好返回接口,而不是具体的交通工具子类Plane !!!
public static Moveable create() {
System.out.println("A plane is created");
return new Plane();
}
}
package Factory_Pattern;
public class CarFactory {
private final static CarFactory INSTANCE=new CarFactory();
public static CarFactory getInstance() {
return INSTANCE;
}
private CarFactory() {
}
//更加严格的做法是不返回Car返回Moveable
public Moveable create() {
System.out.println("A car is created");
return new Car();
}
}
package Factory_Pattern;
public class TrainFactory {
private TrainFactory() {}
private static class Iner{
//内部类的私有成员是可以被访问的
private final static TrainFactory INSTANCE=new TrainFactory();
}
static TrainFactory getInstance() {
return Iner.INSTANCE;
}
//方法名不用createTrain,直接用create更好,因为TrainFactory中创建的肯定是Train
public Moveable create() {
System.out.println("A train is crated");
return new Train();
}
}
2)抽象工厂
- 灵活定制产品一族
例如:我们可以定义餐馆族(通过Chicken、Burger、Drink),A族是ChickenOfA、BurgerOfA、DrinkOfA,B族是ChickenOfB、BurgerOfB、DrinkOfB。这时,抽象工厂模式所要求的是有一个抽象工厂类,然后每一个族都需要一个子工厂去继承实现这个抽象工厂类即可,子工厂最好使用单例模式。
下面是例子代码:
Drink抽象类
package Abstract_Factory_Pattern;
public abstract class Drink {
abstract void printDrink();
}
实现Drink的两个子类
package Abstract_Factory_Pattern;
public class DrinkOfA extends Drink{
@Override
void printDrink() {
// TODO Auto-generated method stub
System.out.println("This is Drink of A");
}
}
package Abstract_Factory_Pattern;
public class DrinkOfB extends Drink{
@Override
void printDrink() {
// TODO Auto-generated method stub
System.out.println("This is Drink Of B");
}
}
Burger抽象类
package Abstract_Factory_Pattern;
public abstract class Burger {
abstract void printBurger();
}
实现Burger的两个子类
package Abstract_Factory_Pattern;
public class BurgerOfA extends Burger {
@Override
void printBurger() {
// TODO Auto-generated method stub
System.out.println("This is Burger of A");
}
}
package Abstract_Factory_Pattern;
public class BurgerOfB extends Burger{
@Override
void printBurger() {
// TODO Auto-generated method stub
System.out.println("This is Burger Of B");
}
}
Chicken抽象类
package Abstract_Factory_Pattern;
public abstract class Chicken {
abstract void printChicken();
}
实现Chicken的两个子类
package Abstract_Factory_Pattern;
public class ChickenOfA extends Chicken{
@Override
void printChicken() {
// TODO Auto-generated method stub
System.out.println("This is Chicken of A");
}
}
package Abstract_Factory_Pattern;
public class ChickenOfB extends Chicken{
@Override
void printChicken() {
// TODO Auto-generated method stub
System.out.println("This is chicken Of B");
}
}
抽象工厂
package Abstract_Factory_Pattern;
public abstract class AbstractFactory {
abstract Chicken createChicken();
abstract Drink createDrink();
abstract Burger createBurger();
}
A和B两个族的子工厂
package Abstract_Factory_Pattern;
public class A_Factory extends AbstractFactory{
private A_Factory() {}
private static class Iner{
private static final A_Factory INSTANCE=new A_Factory();
}
public static A_Factory getINSTANCE() {
return Iner.INSTANCE;
}
@Override
Chicken createChicken() {
// TODO Auto-generated method stub
return new ChickenOfA();
}
@Override
Drink createDrink() {
// TODO Auto-generated method stub
return new DrinkOfA();
}
@Override
Burger createBurger() {
// TODO Auto-generated method stub
return new BurgerOfA();
}
}
package Abstract_Factory_Pattern;
public class B_Factory extends AbstractFactory{
private B_Factory() {}
private static volatile B_Factory INSTANCE;
public static B_Factory getINSTANCE() {
if(INSTANCE==null)
synchronized(B_Factory.class) {
if(INSTANCE==null)
INSTANCE=new B_Factory();
}
return INSTANCE;
}
@Override
Chicken createChicken() {
// TODO Auto-generated method stub
return new ChickenOfB();
}
@Override
Drink createDrink() {
// TODO Auto-generated method stub
return new DrinkOfB();
}
@Override
Burger createBurger() {
// TODO Auto-generated method stub
return new BurgerOfB();
}
}
测试:
package Abstract_Factory_Pattern;
public class Main {
public static void main(String[] args) {
Chicken chickenA=A_Factory.getINSTANCE().createChicken();
Burger burgerA=A_Factory.getINSTANCE().createBurger();
Drink drinkA=A_Factory.getINSTANCE().createDrink();
chickenA.printChicken();
burgerA.printBurger();
drinkA.printDrink();
Chicken chickenB=B_Factory.getINSTANCE().createChicken();
Burger burgerB=B_Factory.getINSTANCE().createBurger();
Drink drinkB=B_Factory.getINSTANCE().createDrink();
chickenB.printChicken();
burgerB.printBurger();
drinkB.printDrink();
}
}
结果图: