Spring learning 03 - design patterns in spring

Spring learning 03 - design patterns in spring (2)

1.1 Adapter Pattern

Adapter pattern refers to converting the interface of a class into another interface expected by users, so that classes with incompatible interfaces can work together. It belongs to structural design pattern.

The adapter mode is applicable to the following business scenarios:

(1) A situation where the methods and requirements of an existing class do not match (the method results are the same or similar)

(2) Solutions to problems with similar functions and different interfaces caused by different products and manufacturers


In China, the civil electricity is 220V AC, but the mobile phone lithium battery uses 5V DC, so we need a power adapter to charge the mobile phone.

Let's simulate this scenario. First, create an AC220 class to represent the 220V AC output

public class AC220 {
    public int outputAC220V(){
        int output = 220;
        System.out.println("power supply at generator voltage"+output+"V");
        return  output;

Create a mobile phone manufacturer's standard that outputs DC 5V. Each mobile phone adapter should output DC 5V to realize the interface.

public interface DC5 {
    int outputDC5V();
public class PowerAdapter implements DC5{
    private AC220 ac220;
    public PowerAdapter(AC220 ac220){
        this.ac220 = ac220;

    public int outputDC5V() {
        int adapterInput = ac220.outputAC220V();
        int adapterOutput = transform(adapterInput);
        System.out.println("The adapter converts the voltage from"+adapterInput+"Become"+adapterOutput);
        return adapterOutput;
    private int transform (int intput){
        return 5;

Client code

    public static void main(String[] args) {
        DC5 dc5 = new PowerAdapter(new AC220());
        int output = dc5.outputDC5V(); // 5
//Output AC 220V
//The adapter changes the voltage from 220 to 5

Simply put, the adapter is equivalent to a middleware that connects the two sides.

1.2 Observer Pattern

The observer pattern defines one to many dependencies between objects, allowing multiple observers to monitor a subject object at the same time. When the subject object changes, all its dependents will receive a notification and follow up.


Suppose that in the evening self-study, teachers Zhang San and Li Si need to manage the whole class. When a classmate has questions, the teacher needs to answer the classmate's questions. Therefore, once there is a new change in the class, station 3 and Li Si should receive the news. They are the observers of the class.

First, the teacher interface is defined

public interface Teacher {
    void answer();
public class Classroom {
    private List<Teacher> observers = new ArrayList<Teacher>();

    private boolean ifQuestion = false; //Indicates whether there are students in the class who have problems

    public void setIfQuestion(boolean ifQuestion) { //If a student has a problem, inform all teachers to answer the problem.
        this.ifQuestion = ifQuestion;
        if (ifQuestion) notifyAllObservers();
        ifQuestion = false;

    public void attach(Teacher observer){
    public void notifyAllObservers(){
        for (Teacher observer : observers) {
public class Wangwu implements Teacher{
    public void answer() {
        System.out.println("Wang Wu has come to answer the question");
public class Zhangsan implements Teacher{
    public void answer() {
        System.out.println("Zhang San has come to answer the question");
public class Lisi implements Teacher{
    public void answer() {
        System.out.println("Li Si has come to answer the question");

Three observers are defined here.

public static void main(String[] args) {
    Classroom classroom = new Classroom();
    classroom.attach(new Zhangsan());
    classroom.attach(new Lisi());
    classroom.attach(new Wangwu());
//Zhang San has come to answer the question
//Li Si has come to answer the question
//Wang Wu has come to answer the question

Simply put, it is to increase the dependence of the observer in the observed, and call the methods in the observer whenever the observed changes. There is nothing.

1.3 Prototype Pattern

Prototype Pattern is used to create duplicate objects while ensuring performance. This type of design pattern is a creation pattern, which provides the best way to create objects.

This pattern implements a prototype interface that is used to create a clone of the current object. This pattern is used when the cost of directly creating objects is relatively high. For example, an object needs to be created after a costly database operation. We can cache the object, return its clone on the next request, and update the database when needed to reduce database calls.

To put it bluntly, it is to implement the clonable interface and use clone() to create objects.

There are two ways to copy JAVA objects: reference copy and object copy

Reference copy: different from object copy, reference copy only generates a new object reference address, but the two addresses ultimately point to the same object;

Object copy: this method will regenerate a new object, and the generated new object has no association with the original object.

Shallow copy is different from reference copy. The object to be shallow copied will regenerate a new object. The new object has nothing to do with the original object. However, if a property in the object is of reference type, the object corresponding to the property will not regenerate. Shallow copy will only regenerate the currently copied object, Objects referenced by their properties are not regenerated.

Figure1: https://www.zhihu.com/people/yan-bang-fei/collections

The difference between deep copy and shallow copy is that deep copy will regenerate the copied object and the object referenced by its attribute into a new object.

Figure2: https://www.zhihu.com/people/yan-bang-fei/collections

At this point, the main design patterns of Spring have been talked about, and finally Spring can be officially used.

MVC design pattern will be discussed in spring MVC!


Main materials: core principles of Spring 5 and handwriting practice of 30 classes


Tags: Java Spring

Posted on Tue, 21 Sep 2021 19:30:54 -0400 by ShaunO