Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

Provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

Usage scenarios

Now the user needs more, in addition to the requirements of different shapes, we also require the creation of different color shapes, such as the circle, we require a red circle, red orange yellow green blue purple and other various colors of the circle. According to the design scheme of our abstract method pattern, we need to implement the shapes of various colors and implement the factory classes corresponding to the shapes of these colors, which will increase the number of factory classes in our system.

implementation

Step 1

Create an interface:

// Shape public interface
public interface Shape {
    void draw(a);
}
Copy the code

Step 2

Create an abstract class.

// The circular abstract class Circle
public abstract class Circle implements Shape {}Copy the code
// Rectange
public abstract class Rectangle implements Shape {}Copy the code

Step 3

The concrete implementation class inherits from the corresponding abstract class. Inheriting from different abstract classes is equivalent to classifying the class into different levels, for example:

// The Circle implementation of the specific color
public class BlueCircle extends Circle {
    @Override
    public void draw(a) {
        System.out.println("Draw a blue circle."); }}public class RedCircle extends Circle {
    @Override
    public void draw(a) {
        System.out.println("Draw a red circle."); }}// Concrete colour Rectange is implemented
public class RedRectange extends Rectange{
    @Override
    public void draw(a) {
        System.out.println("Draw a red rectangle"); }}public class BlueRectange extends Rectange {
    @Override
    public void draw(a) {
        System.out.println("Draw a blue rectangle"); }}Copy the code

Step 4

The division of concrete class family is based on color, and objects of different classes are created by corresponding concrete factories. So first we need to define an abstract factory. The concrete factory (family) implements the methods of the abstract factory to generate a set of concrete objects.

// Abstract factory ShapeFactory
public interface ShapeFactory {
    Shape getCircle(a);
    Shape getRectange(a);
}

//RedShapeFactory (it represents the group of red shapes)
public class RedShapeFactory implements ShapeFactory {
    @Override
    public Shape getCircle(a) {
        return new RedCircle();
    }

    @Override
    public Shape getRectange(a) {
        return newRedRectange(); }}//BlueShapeFactory (it represents the group of blue shapes)
public class BlueShapeFactory implements ShapeFactory {
    @Override
    public Shape getCircle(a) {
        return new BlueCircle();
    }

    @Override
    public Shape getRectange(a) {
        return newBlueRectange(); }}/ /... Other family...

Copy the code

Step 5

public class TestDemo {
    public static void main(String[] args) {
        ShapeFactory redShapeFactory = new RedShapeFactory();
        Shape circle = redShapeFactory.getCircle();
        circle.draw();
        Shape rectangle = redShapeFactory.getRectangle();
        rectangle.draw();

        ShapeFactory blueShapeFactory = newBlueShapeFactory(); Shape blueCircle = blueShapeFactory.getCircle(); blueCircle.draw(); }}Copy the code

Step 6

Execute the program and output the result:

Draw a red circle Draw a red rectangle draw a blue circleCopy the code

As we can see from the above example, the abstract factory pattern provides a solution for creating a set of objects. In contrast to the factory method pattern, a concrete factory in the abstract factory pattern does not just create a concrete object; it is responsible for creating a set of concrete objects. Code Address:

  • Github:github.com/leifu1107/a…