Simple factory pattern: 1 abstract product class,N concrete product class; 1. Factory; 1 Client.

Factory mode: 1 Abstract product class,N concrete product class; 1 Factory interface,N Specific product factory; 1 Client.

Simple Factory model

  1. The factory role: This is the core of the pattern and contains the business logic and judgment logic used to create the product
  2. Abstract product roles: These are typically inherited superclasses or interfaces implemented by the concrete product.
  3. Concrete product roles: The objects created by the factory class are instances of this role. Implemented in Java by a concrete class.
Public abstract class operation {private double numberA =0; private double numberB =0; public double getNumberA() { return numberA; } public void setNumberA(double numberA) { this.numberA = numberA; } public double getNumberB() { return numberB; } public void setNumberB(double numberB) { this.numberB = numberB; } public abstract double getResult(); } public class Add extends Operation {public double getResult(){double result = getNumberA()+getNumberB(); return result; } } public class Mul extends operation { public double getResult(){ return getNumberA()*getNumberB(); }}Copy the code
Public class OperationFactory {public static operation createOperate(String operta){switch (operta){case "+":  return new Add(); case "*": return new Mul(); } return null; }}Copy the code
Public class Client {public static void main(String[] args){operation op = OperationFactory.createOperate("+"); op.setNumberA(12); op.setNumberB(10); System.out.println(op.getResult()); }}Copy the code

The factory method

The problem with the simple factory pattern: It removes the dependency between the client and the concrete product, but also violates the open-closed principle (to add a subtraction, add a class and modify the factory class).

The factory pattern addresses the above approach by defining the interface for creating objects and letting the class decide to instantiate the class (instantiated in the concrete class). This is essentially an abstract factory class that creates individual factories for concrete products. In use, specific new products can be made in our own factory. The code is as follows:

Public abstract class operate {private double numA =0; private double numB =0; Get,set omit public abstract double getResult(); } public class Add extends operate {public double getResult(){return getNumA()+getNumB(); } } public class Mul extends operate { public double getResult(){ return getNumA()*getNumB(); }}Copy the code
Public interface operateFacory {public operate createOperate(); } public class addFactory implements operateFacory {public createOperate(){return new Add(); } } public class MulFactory implements operateFacory { public operate createOperate(){ return new Mul(); }}Copy the code
Public static void main(String[] args){public static void main(String[] args){//operateFacory of = new AddFactory(); operateFacory of = new MulFactory(); operate op = of.createOperate(); op.setNumA(3); op.setNumB(10); System.out.println(op.getResult()); }}Copy the code

Abstract factory method

Abstract factory can make the product and specific factory separation, will not appear in the customer Diamagnetic. For example, the increase, deletion, change and check of employees and departments are only known, but the specific implementation process is not known. DataAccess class: Select the interface to instantiate in this class. Such as new SqlServerUser() IUser interface, specific implementation of SqlServerUser class,AcessUSer class IDepartment interface, specific implementation of SQLServerDepartment class, AccessDepartment class

Call User User = new User(); Iuser iu =DataAccess.CreateUser(); iu.Insert(user); IDepatment id =DataAccess.CreateDepartment(); id.Insert(dept); id.GetDepartment(1);

The proxy pattern

Provide a proxy for other objects to control access to that object. The client instantiates the proxy: instantiates the proxied and calls its methods in the proxy. The proxy simply instantiates and calls the proxied class: write methods (actual operations)

The strategy pattern

Define policy classes (abstract classes, or interfaces): Multiple concrete policy classes Context classes: construct methods that accept policy objects, context homes, and invoke concrete algorithms based on policy objects. Client, instantiates the context class (passing in the concrete policy object), and invokes the context interface

Template pattern

Immutable behavior moves to the superclass, removing duplicate code in the like

A template method subclass (the same method is extracted into the parent class, and some different methods are abstracted) implements the template method.

The prototype pattern

In one method, you define the copy method, and then you only new it once, and then you define the object as the first new copy (note deep copy and shallow copy).

The singleton pattern

Ensure that a class has only one instance and provide a global access point to access it. 1. Singleton class constructor,private(deny the possibility of creating an instance using new) 2. In this method, the instance does not exist,new; If exists, return existing. Note that when multithreading singletons, to double lock (instantiation to create, then lock.

Problems in the writing process,

Static member methods can only be called directly by classes.

2.” is a char,”” is a String