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?
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.
the abstract factory pattern,the static factory method, the simple factory (also called 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With