工厂模式的定义
工厂模式使用的频率非常高,我们在开发中总能见到它们的身影。其定义为:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
即定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类
其相对于简单工厂模式,更加符合开闭原则!!!
工厂模式包括如下角色:
- 抽象产品
- 具体产品
- 抽象工厂
- 具体工厂
以上角色之间的关系如下图所示:
UML图如下:
java代码:
抽象产品(Abstract Product)角色
package com.gof.entity;
/**
*
* @ClassName: Audi
* @Description: 奥迪车的图纸
* @author cheng
* @date 2017-8-8 下午05:06:23
*/
public interface Audi {
/**
*
* @Title: desc
* @Description:用来描述汽车信息
*/
void desc();
}
具体产品(Concrete Product)角色
package com.gof.entity;
/**
*
* @ClassName: AudiA4
* @Description: 奥迪A4系列
* @author cheng
* @date 2017-8-8 下午05:08:28
*/
public class AudiA4 implements Audi {
/**
* 重写
*/
@Override
public void desc() {
System.out.println("我是一辆奥迪A4");
}
}
package com.gof.entity;
/**
*
* @ClassName: AudiA6
* @Description: 奥迪A6系列
* @author cheng
* @date 2017-8-8 下午05:16:17
*/
public class AudiA6 implements Audi {
/**
* 重写
*/
@Override
public void desc() {
System.out.println("我是一辆奥迪A6");
}
}
package com.gof.entity;
/**
*
* @ClassName: AudiA8
* @Description: 奥迪A8系列
* @author cheng
* @date 2017-8-8 下午05:17:16
*/
public class AudiA8 implements Audi {
/**
* 重写
*/
@Override
public void desc() {
System.out.println("我是一辆奥迪A8");
}
}
抽象工厂(Abstract Factory)角色
package com.gof.factory1;
import com.gof.entity.Audi;
/**
*
* @ClassName: AudiFactory
* @Description: 生产奥迪的抽象工厂
* @author cheng
* @date 2017-8-9 下午03:01:59
*/
public interface AudiFactory {
/**
*
* @Title: createAudi
* @Description:生产奥迪
* @return
*/
Audi createAudi();
}
具体工厂(Concrete Factory)角色
package com.gof.factory1;
import com.gof.entity.Audi;
import com.gof.entity.AudiA4;
/**
*
* @ClassName: AudiA4Factory
* @Description: 奥迪A4生产工厂
* @author cheng
* @date 2017-8-9 下午03:04:26
*/
public class AudiA4Factory implements AudiFactory{
/**
* 生产奥迪A4
*/
@Override
public Audi createAudi() {
// TODO Auto-generated method stub
return new AudiA4();
}
}
package com.gof.factory1;
import com.gof.entity.Audi;
import com.gof.entity.AudiA6;
/**
*
* @ClassName: AudiA6Factory
* @Description: 奥迪A6生产工厂
* @author cheng
* @date 2017-8-9 下午03:05:46
*/
public class AudiA6Factory implements AudiFactory {
/**
* 生产奥迪A6
*/
@Override
public Audi createAudi() {
// TODO Auto-generated method stub
return new AudiA6();
}
}
package com.gof.factory1;
import com.gof.entity.Audi;
import com.gof.entity.AudiA8;
/**
*
* @ClassName: AudiA8Factory
* @Description: 奥迪A8生产工厂
* @author cheng
* @date 2017-8-9 下午03:07:25
*/
public class AudiA8Factory implements AudiFactory {
/**
* 生产奥迪A8
*/
@Override
public Audi createAudi() {
// TODO Auto-generated method stub
return new AudiA8();
}
}
测试
package com.gof.factory1;
import com.gof.entity.Audi;
/**
*
* @ClassName: AudiFactoryTest
* @Description: 测试
* @author cheng
* @date 2017-8-9 下午03:10:01
*/
public class AudiFactoryTest {
public static void main(String[] args){
System.out.println("====================================");
AudiFactory audiFactory1 = new AudiA4Factory();
Audi audi1 = audiFactory1.createAudi();
audi1.desc();
System.out.println("====================================");
AudiFactory audiFactory2 = new AudiA6Factory();
Audi audi2 = audiFactory2.createAudi();
audi2.desc();
System.out.println("====================================");
AudiFactory audiFactory3 = new AudiA8Factory();
Audi audi3 = audiFactory3.createAudi();
audi3.desc();
}
}
运行结果