简体   繁体   English

以下设计是抽象工厂吗?

[英]Is the below design an abstract factory?

I have the following scenario 我有以下场景

  1. A method takes a vehicle type object and returns the following 方法采用车辆类型对象并返回以下内容

    a. 一个。 Road vehicle b. 公路车辆b。 water vehicle c. 水车c。 flying vehicle. 飞行的车辆。

  2. another method takes the object returned from 1 and gives more specific object. 另一种方法从1返回对象并提供更具体的对象。 For ex if "Road vehicle is returned" then this method will return Car, bus etc. 如果“道路车辆退回”,那么此方法将返回汽车,公共汽车等。

Can I say that this is an example of Abstract factory pattern? 我可以说这是抽象工厂模式的一个例子吗? Because in step 1 , I am getting one of possible factory and in step 2, it is more of specific object. 因为在步骤1中,我得到了一个可能的工厂,并且在步骤2中,它更具特定的对象。

Regards 问候

You're not creating a concrete factory first, so I don't see this as being a representation of abstract factory. 你不是先创建一个具体的工厂,所以我不认为这是抽象工厂的代表。 Thus you've got a concrete factory method. 因此,你有一个具体的工厂方法。

在这种情况下,您有一个工厂方法。

No yours is not an abstract factory, it is more of a Factory Method pattern . 不,你不是一个抽象工厂,它更像是一个工厂方法模式

Abstract Factory 抽象工厂

Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes. Abstract Factory提供了用于创建一系列相关对象的接口,而无需显式指定其类。

Something like this would be Abstract Factory 像这样的东西就是抽象工厂

interface CarSparePartFactory{
    public SparkPlug createSparkPlug();
    public Wheel createWheel();

}

interface SparkPlug{

}

interface Wheel{

}

Here you are creating a group of related objects using an interface. 在这里,您将使用界面创建一组相关对象。

For more on Abstract factory: 有关抽象工厂的更多信息:

It isn't. 事实并非如此。 An abstract factory should determine by itself the kind of object to be created. 抽象工厂应该自己确定要创建的对象的类型。 It's not supposed to be a parameter sent by the client. 它不应该是客户端发送的参数。 Otherwise, the main purpose of the abstract factory ("Provide an interface for creating families of related or dependent objects without specifying their concrete classes.") is not satisfied. 否则,抽象工厂的主要目的(“提供用于创建相关或依赖对象族的接口而不指定其具体类。”)不满足。

See more at http://en.wikipedia.org/wiki/Abstract_factory_pattern 有关详情,请访问http://en.wikipedia.org/wiki/Abstract_factory_pattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抽象工厂设计模式使用 - Abstract Factory Design Pattern use 设计模式:工厂 vs 工厂方法 vs 抽象工厂 - Design Patterns: Factory vs Factory method vs Abstract Factory 抽象工厂设计模式和泛型-Java - Abstract factory design pattern and generics - Java 抽象工厂设计:用枚举替换if else - Abstract Factory Design: Replacing if else with enum 设计问题通用/工厂/抽象类 - design issue generics/factory/abstract class Java中的工厂设计模式与抽象工厂设计模式有什么区别? - what is the difference between factory and abstract factory design pattern in java? “工厂”设计模式在工厂中可以有 1 个以上的抽象类型吗? - Can a 'Factory' Design Pattern have more than 1 abstract type in a factory? 以下设计模式如何成为工厂方法。 我相信它更像是一个抽象工厂而不是工厂方法 - How is the following design pattern a Factory method. I believe it is more of a Abstract Factory than factory method 这是一个很好的设计:使用纯抽象类来方便工厂方法的使用 - Is this good design: using a pure abstract class to facilitate the use of factory method Class.newInstance()是否遵循“抽象工厂”设计模式? - Does Class.newInstance() follow the “Abstract factory” design pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM