一、简介
工厂模式主要是通过工厂来创建对象,屏蔽掉创建对象的过程。分为:
- 简单工厂模式 -- 对具体要生产的对象抽象
- 工厂方法模式 --对工厂抽象
- 抽象工厂方法模式
二、场景
- SqlSessionFactoryBean、
- BeanFactory
三、举例
1、简单工厂模式
简单工厂模式是通过对具体获取的对象进行抽象。在工厂中,通过创建子类对象指向父类来返回。
缺点:当需要生产多个汽车时。需要修改工厂的if,违背开闭原则
1)、抽象父类
public abstract class AbstractCar {
String engine;
public abstract void run();
}
2、具体子类
public class MiniCar extends AbstractCar{
public MiniCar(){
this.engine = "V8发动机";
}
@Override
public void run() {
System.out.println("跑起来了---> 嗡嗡嗡");
}
}
public class VanCar extends AbstractCar{
public VanCar(){
this.engine = "单杠柴油发动机";
}
@Override
public void run() {
System.out.println("跑起来了-> 哒哒哒");
}
}
3)、工厂
import java.util.Objects;
public class WuLinFactory {
public AbstractCar newCar(String type){
if(Objects.equals(type, "mini")){
return new MiniCar();
}else if(Objects.equals(type,"van")){
return new VanCar();
}
return null;
}
}
4)、测试方法
@Test
public void simpleFactory(){
WuLinFactory wuLinFactory = new WuLinFactory();
AbstractCar miniCar = wuLinFactory.newCar("mini");
miniCar.run();
AbstractCar vanCar = wuLinFactory.newCar("van");
vanCar.run();
}
5)、结果
跑起来了---> 嗡嗡�?
跑起来了-> 哒哒�?
2、工厂方法
工厂方法是在对具体需要生产的对象抽象,然后再对工厂抽象
缺点:对工厂的抽象不够,对于多种品类任然违背开闭原则。需要对工厂的抽象再进一步抽象
1)、汽车抽象父类
public abstract class AbstractCar {
String engine;
public abstract void run();
}
2)、具体汽车类
public class MiniCar extends AbstractCar {
public MiniCar(){
this.engine = "V8发动机";
}
@Override
public void run() {
System.out.println("跑起来了---> 嗡嗡嗡");
}
}
public class RacingCar extends AbstractCar{
public RacingCar(){
this.engine = "V99 发动机";
}
@Override
public void run() {
System.out.println("赛车跑起来了---> 呜呜呜");
}
}
public class VanCar extends AbstractCar {
public VanCar(){
this.engine = "单杠柴油发动机";
}
@Override
public void run() {
System.out.println("跑起来了-> 哒哒哒");
}
}
3)、工厂抽象类
public abstract class AbstractCarFactory {
public abstract AbstractCar newCar();
}
4)、具体工厂类
public class WuLinMiniCarFactory extends AbstractCarFactory{
@Override
public AbstractCar newCar() {
return new MiniCar();
}
}
public class WuLinRacingCarFactory extends AbstractCarFactory{
@Override
public AbstractCar newCar() {
return new RacingCar();
}
}
public class WuLinVanCarFactory extends AbstractCarFactory{
@Override
public AbstractCar newCar() {
return new VanCar();
}
}
5)、测试类
@Test
public void factoryMethod(){
AbstractCarFactory miniCarFactory = new WuLinMiniCarFactory();
miniCarFactory.newCar().run();
AbstractCarFactory racingCarFactory = new WuLinRacingCarFactory();
racingCarFactory.newCar().run();
AbstractCarFactory vanCarFactory = new WuLinVanCarFactory();
vanCarFactory.newCar().run();
}
3、抽象工厂
对工厂的抽象进一步抽象
1)、汽车抽象类
public abstract class AbstractCar {
String engine;
public abstract void run();
}
2)、口罩抽象类
public abstract class AbstractMask {
Integer price;
public abstract void protectedMe();
}
3)、汽车和口罩具体对象
public class CommonMask extends AbstractMask{
public CommonMask(){
this.price = 1;
}
@Override
public void protectedMe() {
System.out.println("common mask protected me");
}
}
public class N95Maskl extends AbstractMask{
public N95Maskl(){
this.price = 3;
}
@Override
public void protectedMe() {
System.out.println("n95 protected Me");
}
}
public class RacingCar extends AbstractCar {
public RacingCar(){
this.engine = "V99 发动机";
}
@Override
public void run() {
System.out.println("赛车跑起来了---> 呜呜呜");
}
}
public class VanCar extends AbstractCar {
public VanCar(){
this.engine = "单杠柴油发动机";
}
@Override
public void run() {
System.out.println("跑起来了-> 哒哒哒");
}
}
4)、企业工厂接口
public interface WulinFactory {
AbstractCar newCar();
AbstractMask newMask();
}
5)、汽车和口罩工厂接口
public interface WuLinCarFactory extends WulinFactory{
AbstractCar newCar();
}
public interface WuLinMaskFactory extends WulinFactory{
AbstractMask newMask();
}
6)、汽车和口罩具体工厂
public class WuLinCommonMaskFactory implements WuLinMaskFactory{
@Override
public AbstractCar newCar() {
return null;
}
@Override
public AbstractMask newMask() {
return new CommonMask();
}
}
public class WuLinN95MaskFactory implements WuLinMaskFactory{
@Override
public AbstractCar newCar() {
return null;
}
@Override
public AbstractMask newMask() {
return new N95Maskl();
}
}
public class WuLinRacingCarFactory implements WuLinCarFactory{
@Override
public AbstractCar newCar() {
return new RacingCar();
}
@Override
public AbstractMask newMask() {
return null;
}
}
public class WuLinVanCarFactory implements WuLinCarFactory{
@Override
public AbstractCar newCar() {
return new VanCar();
}
@Override
public AbstractMask newMask() {
return null;
}
}
7)、测试方法
@Test
public void abstractFactory(){
WulinFactory commonMaskFactory = new WuLinCommonMaskFactory();
AbstractMask commonMask = commonMaskFactory.newMask();
commonMask.protectedMe();
WulinFactory n95MaskFactory = new WuLinN95MaskFactory();
AbstractMask n95Mask = n95MaskFactory.newMask();
n95Mask.protectedMe();
WulinFactory racingCarFactory = new com.example.designpatterns.creatation.factory.abstractfactory.WuLinRacingCarFactory();
com.example.designpatterns.creatation.factory.abstractfactory.AbstractCar racingCar = racingCarFactory.newCar();
racingCar.run();
WulinFactory vanCarFactory = new com.example.designpatterns.creatation.factory.abstractfactory.WuLinVanCarFactory();
com.example.designpatterns.creatation.factory.abstractfactory.AbstractCar vanCar = vanCarFactory.newCar();
racingCar.run();
}