Design Patterns are often divided into the following three types:
Creation type
When creating an object, we do not directly instantiate the object. Instead, the program determines how to create the object based on the specific scenario. For example, factory method, abstract factory mode, and Singleton mode.
ConstructorIt is used to organize multiple objects into a larger structure. Such as the adapter mode, bridging mode, and bundle mode.
Behavior TypeIt helps communication between objects between systems and controls processes in complex systems. For example, the command mode, interpreter mode, and iterator mode.
I. Singleton Mode
public class Singleton { public static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } public static void main(String[] args) { Singleton instance1 = Singleton.getInstance(); Singleton instance2 = Singleton.getInstance(); System.out.println(instance1 == instance2); }}
(In spring, you only need to specify scope = "Singleton" in the configuration object bean to implement singleton)
Ii. Simple Factory
When instance A needs to call the method of instance B, it is not implemented through the new B instance, but through a factory bfactory that can create instance B.
B = new B (); --- à B = bfactory (). getb ();
It is changed:
When instance A needs to call a method, this method can be implemented by instance B or instance B1. Then we define an interface IB to allow B and B1 to implement this interface. And write an IB factory ibfactory to provide a getib () method.
public class A{private IB ib;public A(IB ib){this.ib=ib;}public void showMessage(){ib.print();}}public interface IB{public abstract void print();} public class B implements IB{public void print(){}} public clsss B1 implements IB{public void print(){}} public IBFactory{public IB getIB(){return new B();}} --------------------------IB ib=IBFactory.getIB();A a=new A(ib);a.showMessage();----------------------------
Abstract factory pattern diagram:
3. factory methods and abstract factories
In the simple factory mode, the system uses the productfactory factory class to produce all product instances, and the factory class is used to determine the instance that produces the class.
3.1 factory MethodIn the factory method, if we do not want to make logical judgments in the factory class, the program can provide different factories for different product classes and different factory classes to produce different products.
After the factory method is used, the client code is successfully separated from the implementation class of the called object.
3.2 Abstract Factory Model: To solve the coupling problem between the client code and different factories, add a factory class that produces outputfactory instances (different factory instances are produced based on input parameters)
public class OutputFactoryFactory {public static OutputFactory getOutputFactory(String type) {if ("better".equalsIgnoreCase(type)) {return new BetterOutputFactory();} else {return new OutputFactory();}}}