Business scenario: develop the electronic invoice module in the work, but there are many electronic invoice platforms in the market. After investigation, it is found that their functions are all invoicing, and there are great differences between the implementation interface and configuration information. So I think of the factory model.
Why do you think of factory mode? You can get your own understanding from the scene. Design pattern itself is more to achieve design principles.
Brief introduction of factory pattern: three kinds of creation pattern of design pattern.
Model establishment:
Factory - (production) - invoice company
Invoice company - (function) - Invoicing
Factory selection: at present, two companies are determined. Other companies will be added if not excluded. Finally, we choose the abstract factory implementation.
First, confirm the function of invoicing company
public interface InvoiceCompany { //draw a bill void makeInvoice(); }
At present, two companies are responsible for invoicing
public class InvoiceCompanyA implements InvoiceCompany { @Override public void makeInvoice() { System.out.println("company A Invoiced"); } } public class InvoiceCompanyB implements InvoiceCompany { @Override public void makeInvoice() { System.out.println("B The company issued an invoice"); } }
The following is an introduction to the types and implementation of factories:
- General Factory
public class SimpleFactory { //Generate company A public InvoiceCompany getInvoiceCompanyA() { return new InvoiceCompanyA(); } //Generate company B public InvoiceCompany getInvoiceCompanyB() { return new InvoiceCompanyB(); } }
test
public class SimpleFactoryTest { public static void main(String[] args) { SimpleFactory simpleFactory = new SimpleFactory(); InvoiceCompany invoiceCompanyA = simpleFactory.getInvoiceCompanyA(); invoiceCompanyA.makeInvoice(); } } Output: company A issued invoice
2. Static factory (static factory method based on simple factory)
public class StaticFactory { //Generate company A public static InvoiceCompany getInvoiceCompanyA() { return new InvoiceCompanyA(); } //Generate company B public static InvoiceCompany getInvoiceCompanyB() { return new InvoiceCompanyB(); } }
test
public class StaticFactoryTest { public static void main(String[] args) { InvoiceCompany invoiceCompanyA = StaticFactory.getInvoiceCompanyA(); invoiceCompanyA.makeInvoice(); } } Output: company A issued invoice
3. Abstract Factory (abstract factory methods)
public interface AbstractFactory { //Production billing company InvoiceCompany getInvoiceCompany(); } //Create A factory that only produces company A public class InvoiceCompanyAFactory implements AbstractFactory { @Override public InvoiceCompany getInvoiceCompany() { return new InvoiceCompanyA(); } } //Create a factory that only produces company B public class InvoiceCompanyBFactory implements AbstractFactory { @Override public InvoiceCompany getInvoiceCompany() { return new InvoiceCompanyB(); } }
test
public class AbstractFactoryTest { public static void main(String[] args) { AbstractFactory abstractFactory = new InvoiceCompanyAFactory(); InvoiceCompany invoiceCompany = abstractFactory.getInvoiceCompany(); invoiceCompany.makeInvoice(); } } Output: company A issued invoice
Conclusion: Although there are three kinds of factories, I don't think there are too many opportunities for ordinary factories to play. From the above, we can see that static is more like a pure reinforcement of ordinary factories. The abstract factory is more powerful. It can well implement the opening and closing principle. If new company C can create another C factory without source code. If it is a static factory, it is inevitable to modify the source code.
——There are many ways to realize a function. Only by learning to think about the optimal solution can we make continuous progress.