Factory pattern
1. Simple factory The factory class
that understands
the simple factory pattern generally uses static methods to return different object instances by receiving different parameters.
It cannot be extended without modifying the code.
Example
product interface
//product interface
public interface ProductI {
public void productName();
}
Product entity class
public class ProductA implements ProductI {
@Override
public void productName() {
System.out.println("product A");
}
}
public class ProductB implements ProductI {
@Override
public void productName() {
System.out.println("product B");
}
}
Factory class
// Simple factory pattern
public class Factory {
public ProductI create(String productName) {
switch (productName) {
case "A":
return new ProductA();
case "B":
return new ProductB();
default:
return null;
}
}
}
测试类
public class Client {
public static void main(String[] args) {
Factory factory = new Factory();
ProductI productA = factory.create("A");
productA.productName();
//
ProductI productB = factory.create("B");
productB.productName();
}
}
输出
product A
product B
factory method
Understanding
the factory method is to provide a factory class for each product. Create different product instances through different factory instances.
In the same hierarchical structure, any product can be added.
Example
product interfaces are the same as product entity classes and simple factories.
Factory interface
public interface FactoryI {
// The purpose of the factory is to produce products
public ProductI create();
}
Factory entity class
public class FactoryA implements FactoryI {
@Override
public ProductI create() {
return new ProductA();
}
}
public class FactoryB implements FactoryI {
@Override
public ProductI create() {
return new ProductB();
}
}
Test class
// Factory method pattern
public class Client {
public static void main(String[] args) {
FactoryI factoryA = new FactoryA();
ProductI productA = factoryA.create();
productA.productName();
//
FactoryI factoryB = new FactoryB();
ProductI productB = factoryB.create ();
productB.productName();
}
}
output
product A
product B
abstract factory
understands that
abstract factory is to deal with the concept of product family. For example, each car company may produce cars, vans, and passenger cars at the same time, and then each factory must have a method for creating cars, vans, and passenger cars.
In response to the concept of product family, it is easy to add new product lines, but it is impossible to add new products.
The difference from the factory method is that the abstract factory often has multiple methods that can produce multiple products, that is, product clusters.
Example
abstract factory class
// Draw wire factory pattern
public interface AbstractFactory {
public ProductAI createProductA();
public ProductBI createProductB();
}
工厂实体类
public class Factory1 implements AbstractFactory {
@Override
public ProductAI createProductA() {
return new ProductA1();
}
@Override
public ProductBI createProductB() {
return new ProductB1();
}
}
public class Factory2 implements AbstractFactory {
@Override
public ProductAI createProductA() {
return new ProductA2();
}
@Override
public ProductBI createProductB() {
return new ProductB2();
}
}
产品接口
//产品接口
public interface ProductBI {
public void productName();
}
//产品接口
public interface ProductAI {
public void productName();
}
产品实体类
public class ProductA1 implements ProductAI {
@Override
public void productName() {
System.out.println("product A1");
}
}
Factory pattern of Java design pattern (three kinds)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326525248&siteId=291194637
Recommended
Ranking