设计模式之三种工厂方法
设计模式之工厂模式
简单工厂:(计算器、画图)
1.创建基本类
2.创建计算类(各种方式,每种一个类,继承基本类)
3.创建操作工厂(根据不同操作实例化)
4.调用方法。
abstract class Product
{
public void MethName()
{
//公共方法的实现
}
public abstract void MethodDiff();
//声明抽象业务方法
}
class ConcreteProductA extends Product
{
public override void MethodDiff()
{
//业务方法的实现
}
}
class Factory
{
public static Product GetProduct(string arg)
{
Product product = null;
if(arg.Equals("A")
{
product = new ConcreteProductA();
//init
}
else if(arg.Equals("B"))
{
product = new ConcreteProductB();
//init
}
else
{
....//其他情况
}
return product;
}
}
class Program
{
static void Main(string[] args)
{
Product product;
product = Factory.GetProduct("A");//工厂类创建对象
Product.MethName();
product.MethodDiff();
}
}
工厂方法:(造车、雷锋事件(大学生帮助老人))
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
┌─────────────┐ ┌─────────────┐
│ Product │ │ Factory │
└─────────────┘ └─────────────┘
▲ ▲
│ │
┌─────────────┐ ┌─────────────┐
│ ProductImpl │<─ ─ ─│ FactoryImpl │
└─────────────┘ └─────────────┘
工厂方法克服了简单工厂违背对修改关闭、对扩展开放的原则,保持了封装对象的创建过程。
interface Car {
public void getCar();
}
public class BMW implements Car{
@Override
public void getCar() {
System.out.printf("来一辆BMW!");
}
}
public class BYD implements Car {
@Override
public void getCar() {
System.out.printf("来一辆BYD!");
}
}
//抽象工厂
interface CarFactory{
public Car getCarByFactory();
}
// BMW 具体工厂类
class BMWfactory implements CarFactory{
@Override
public Car getCarByFactory() {
return new BMW();
}
}
// BYD 具体工厂类
class BYD factory implements CarFactory{
@Override
public Car getCarByFactory() {
return new BYD ();
}
}
//主方法调用
public class TestFactory {
public static void main(String[] args) {
Car bmw= new BMWfactory().getCarByFactory();
bmw.getCar();
}
}
抽象工厂(不同数据库访问案例,代码实现随时改变数据库从mysql->oracle)
为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。
抽象工厂模式是工厂方法模式的升级版本,他用来创建一组相关或者相互依赖的对象。他与工厂方法模式的区别就在于,工厂方法模式针对的是一个产品结构;而抽象工厂模式则是针对的多个产品结构。这种模式有点类似于多个供应商负责提供一系列类型的产品。
┌────────┐
─ >│ProductA│
┌────────┐ ┌─────────┐ │ └────────┘
│ Client │─ ─>│ Factory │─ ─
└────────┘ └─────────┘ │ ┌────────┐
▲ ─ >│ProductB│
┌───────┴───────┐ └────────┘
│ │
┌─────────┐ ┌─────────┐
│Factory1 │ │Factory2 │
└─────────┘ └─────────┘
│ ┌─────────┐ │ ┌─────────┐
─ >│ProductA1│ ─ >│ProductA2│
│ └─────────┘ │ └─────────┘
┌─────────┐ ┌─────────┐
└ ─>│ProductB1│ └ ─>│ProductB2│
└─────────┘ └─────────┘
在工厂方法的基础上升级
interface Plane{
public void getPlane();
}
public class AirPlane implements Plane{
@Override
public void getPlane() {
System.out.printf("来一架客机!");
}
}
public class BattlePlane implements Plane{
@Override
public void getPlane() {
System.out.printf("来一架战斗机!");
}
}
//抽象工厂
interface Factory{
public Car getCarByFactory();<br> public Plane getPlaneByFactory();
}
// 具体工厂类
class FactoryOne implements Factory{
@Override
public Car getCarByFactory() {
return new BMW();
}
@Override
public Plane getPlaneByFactory() {
return new AirPlane();
}
}
// 具体工厂类
class FactoryTwo implements Factory{
@Override
public Car getCarByFactory() {
return new BYD();
}
@Override
public Plane getPlaneByFactory() {
return new BattlePlane ();
}
}
public class TestFactory {
public static void main(String[] args) {
Car byd = new FactoryTwo().getCarByFactory();
byd.getCar();
Plane airPlane = new FactoryOne().getPlaneByFactory();
airPlane.getPlane();
}
}
一般通过 反射+工厂、反射+配置文件实现,例如Spring中Bean配置。
三种工厂的区别:
简单工厂:一个接口、一个工厂
工厂方法:一个接口、多个工厂
抽象工厂:多个接口、多个工厂
posted on 2021-11-25 23:29 Chase_Hanky 阅读(101) 评论(0) 收藏 举报
浙公网安备 33010602011771号