Design Pattern One (Creative)

1. Singleton pattern

/**
 * The singleton pattern is a commonly used software design pattern
 * It can ensure that there is only one instance of a class in the system, that is, a class has only one instance object
 * To meet the above conditions 1. The constructor should be privatized, 2. Create a private static reference within yourself 3. Provide a common static method to the outside world
 */
public class Singleton {
    // lazy mode
    private Singleton(){}
    private static Singleton singleton;
    public static Singleton getInstance(){
        if (singleton==null){
            //In the case of high concurrency, if the lazy mode is used, the best choice is the method of double judgment and synchronization.
            synchronized (Singleton.class){ //In the case of multi-threading, it is not necessary to synchronize every time the object is acquired, only the first time to synchronize, which improves the efficiency.
                if (singleton==null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
public class Singleton {
    // hungry mode
    private Singleton(){}
    private static Singleton singleton = new Singleton();
    public static Singleton getInstance(){
        return singleton;
    }
}

2. Factory mode

In the factory pattern, we do not expose the creation logic to the client when creating objects, and we use a common interface to point to the newly created objects. For database access in actual projects , when the user does not know which type of database the system uses in the end, and when the database may change, using the factory mode only needs to change the dialect and driver.

/**
 * Shape interface, there is a drawing method
 */
public interface Shape {
    void draw();
}

There are three shape implementation classes

public class Circular implements Shape {
    public void draw() {
        System.out.println("Draw a circle");
    }
}
public class Square implements Shape{
    public void draw() {
        System.out.println("Draw a square");
    }
}
public class Triangle implements Shape {
    public void draw() {
        System.out.println("Draw a triangle");
    }
}
/**
 * Create a factory that generates objects of entity classes based on given information
 */
public class ShapeFactory {
    public Shape getShape(String shape){
        if (shape==null){
            return null;
        }
        if (shape.equalsIgnoreCase("CIRCULAR")){
            return new Circular();
        }
        if (shape.equalsIgnoreCase("SQUARE")){
            return new Square();
        }
        if (shape.equalsIgnoreCase("TRIANGLE")){
            return new Triangle();
        }
        return null;
    }
}

test class

public class Main {
    public static void main(String[] args){
        ShapeFactory factory = new ShapeFactory();
        Shape shape1 = factory.getShape("CIRCULAR");
        Shape shape2 = factory.getShape("SQUARE");
        Shape shape3 = factory.getShape("TRIANGLE");
        shape1.draw();
        shape2.draw();
        shape3.draw();
    }
}

The console prints the result:


3. Abstract Factory Pattern

Abstract Factory Pattern (Abstract Factory Pattern) is to create other factories around a super factory. The Gigafactory is also known as the factory of other factories.

There are two interfaces: shape and color

public interface Color {
    // fill color abstract method
    void fill();
}
public interface Shape {
    void draw();
}

their implementation class

public class Circular implements Shape {
    public void draw() {
        System.out.println("Draw a circle");
    }
}
public class Square implements Shape{
    public void draw() {
        System.out.println("Draw a square");
    }
}
public class Triangle implements Shape {
    public void draw() {
        System.out.println("Draw a triangle");
    }
}
public class Red implements Color {
    public void fill() {
        System.out.println("fill red");
    }
}
public class Blue implements Color {
    public void fill() {
        System.out.println("fill blue");
    }
}
public class Yellow implements Color {
    public void fill() {
        System.out.println("Fill with yellow");
    }
}

abstract factory class

public abstract class BaseFactory {
    public abstract Shape getShape(String shapeType);
    public abstract Color getColor(String colorType);
}

Factory class implementation class

public class ShapeFactory extends BaseFactory{
    public Shape getShape(String shapeType){
        if (shapeType.equalsIgnoreCase("CIRCULAR")){
            return new Circular();
        }
        if (shapeType.equalsIgnoreCase("SQUARE")){
            return new Square();
        }
        if (shapeType.equalsIgnoreCase("TRIANGLE")){
            return new Triangle();
        }
        return null;
    }

    public Color getColor(String colorType) {
        return null;
    }
}
public class ColorFactory extends BaseFactory{
    public Shape getShape(String shapeType) {
        return null;
    }
    public Color getColor(String colorType) {
        if (colorType.equalsIgnoreCase("RED")){
            return new Red();
        }
        if (colorType.equalsIgnoreCase("YELLOW")){
            return new Yellow();
        }
        if (colorType.equalsIgnoreCase("BLUE")){
            return new Blue();
        }
        return null;
    }
}

Create a factory generator to get concrete factories.

public class FactoryProducer {
    public static BaseFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("SHAPE")){
            return new ShapeFactory();
        } else if(choice.equalsIgnoreCase("COLOR")){
            return new ColorFactory();
        }
        return null;
    }
}

test

public class Main {
    public static void main(String[] args){
        BaseFactory factory = FactoryProducer.getFactory("SHAPE");
        BaseFactory factory1 = FactoryProducer.getFactory("COLOR");
        Shape shape1 = factory.getShape("CIRCULAR");
        Shape shape2 = factory.getShape("SQUARE");
        Shape shape3 = factory.getShape("TRIANGLE");
        Color color1 = factory1.getColor("RED");
        Color color2 = factory1.getColor("BLUE");
        Color color3 = factory1.getColor("YELLOW");
        shape1.draw();
        color1.fill();
        shape2.draw();
        color2.fill();
        shape3.draw();
        color3.fill();
    }
}

The console prints the result:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855686&siteId=291194637