简体   繁体   中英

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. water vehicle c. flying vehicle.

  2. another method takes the object returned from 1 and gives more specific object. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM