Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Method VS Factory Object [duplicate]

As I understand Factory Method is Simple Factory and Factory Object is Abstract Factory? And:

-Factory Method (Simple Factory):

public class SimplePizzaFactory {
    public static final int CHEESE = 1;
    public static final int PEPPERONI = 2;
    public static final int VEGGIE = 3;

    public static Pizza createPizza(int type) {
        Pizza pizza = null;

        if (type == CHEESE) {
            pizza = new CheesePizza();
        } else if (type == PEPPERONI ) {
            pizza = new PepperoniPizza();
        } else if (type == VEGGIE ) {
            pizza = new VeggiePizza();
        }

        return pizza;
    }
}

Factory Object(Abstract Factory):

?

Am I right?

How much are there realizations of Factory patterns and what is their difference?

like image 377
drifter Avatar asked Feb 02 '12 18:02

drifter


People also ask

What is difference between factory method and factory pattern?

The factory method is a creational design pattern, i.e., related to object creation. In the Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

Which are the three types of factory method?

the abstract factory pattern,the static factory method, the simple factory (also called factory).

What is the difference between factory method and abstract factory?

The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.

What is the difference between factory method and prototype design pattern?

Factory pattern is used to introduce loose coupling between objects as the factory will take care of all the instantiation logic hiding it from the clients. Prototype pattern on the other hand is used when the cost of creating an object is large and it is ok to copy an existing instance than creating a new instance.


1 Answers

No. A factory-method is a factory that does not require any state. A factory class is a class itself - it has state, and methods that alter that state. In the end you call the .create() method, and it uses its current state to create a new object of a different type.

Abstract factory is a different thing - there you have multiple factory implementations of the same abstract concept. The wikipedia example is about e GUIFactory - this is an abstract factory, which has two implementations: WinFactory and OSXFactory. The client code does not know which implementation it is using - it just knows the factory creates Button instances. Which make it possible to write the same code regardless of the OS.

like image 84
Bozho Avatar answered Sep 24 '22 10:09

Bozho