This is the 9th day of my participation in the More text Challenge. For details, see more text Challenge
define
Define an excuse to create an object and let the subclass decide which class to instantiate
Usage scenarios
Where you need to create complex objects, “for example, you need to create different objects with more features, but they all have a lot in common, and you can use the factory method module.” Complex objects that fit into the factory pattern can be created with new, but you don’t need to use the factory pattern. For a class that does not know what object it needs; A class uses its subclasses to specify which objects to create; Delegating object creation to one of multiple factory subclasses allows the client to use it without caring which factory subclass is creating the product subclass and dynamically specify it as needed.
code
Abstract Products:
/** * Abstract product class */
public abstract class Product {
// The abstract method of the product class is implemented by the concrete product class
public abstract void method(a);
}
Copy the code
Specific products:
/*** * Specific product category A */
public class ProductA extends Product{
@Override
public void method(a) {
System.out.println("I am specific product A"); }}/*** * Specific products B */
public class ProductB extends Product{
@Override
public void method(a) {
System.out.println("I'm specific product B."); }}Copy the code
Specific product classes can be created in different ways depending on requirements.
Abstract method class:
/** * abstract factory class * to get a sense of factory method pattern, write two abstract methods to get product class */
public abstract class Factory {
public abstract <T extends Product> T createProduct(Class<T> cla); // Create dynamically by reflection
public abstract Product createProduct(String type); // Create the product object class with the type
}
Copy the code
Concrete factory class
/*** * concrete abstract factory */
public class ConcreateFactory extends Factory {
@Override
public <T extends Product> T createProduct(Class<T> cla) {
Product product = null;
try {
product = (Product) Class.forName(cla.getName()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return (T) product;
}
@Override
public Product createProduct(String type) {
switch (type) {
case "A":
return new ProductA();
case "B":
return new ProductB();
}
return null; }}Copy the code
Let’s look at the test class:
public class ClientTest {
public static void main(String[] args) {
Factory factory = new ConcreateFactory();
Product productA = factory.createProduct("A"); Product productB = factory.createProduct(ProductB.class); productA.method(); productB.method(); }}Copy the code
The output
As you can see from the code, we just need to pass in the object or type that we want to generate, and we can get it, and we can execute the corresponding method, and if we need to extend it later, we don’t have to worry about a lot of judgment, and that’s how the six principles of object orientation correspond to the six principles of object orientation
conclusion
Parent in factory method pattern, the factory is responsible for defining public interfaces, objects created products and factories subclass object is responsible for generating the specific products, the purpose is to delay product class instantiation operations to complete, factory subclasses by factory subclasses to determine exactly which a specific product class to instantiate.
advantages
- Rapidly expanding
- Perfectly embodies the open closed principle, which is open for extension but closed for modification.
disadvantages
- There will be many product classes, increasing the amount of code