What have we learned from the epidemic

preface

It started on December 8, 2019, and the official reported the medical record of the first patient with unexplained pneumonia.

Nearly two years have passed since now. We have experienced City closure, isolation and home office. We have had panic and moved,

We have been moved by selfless doctors, nurses, soldiers, volunteers and so on. Their "illness does not retreat and oath does not return" makes us cry again and again.

We have experienced the miracle of the completion of Vulcan hospital in ten days. We have experienced too much.

Whenever there is a difficult time, you can realize that your country is so strong that we are no inferior to some developed countries in the face of the epidemic.

I have no regrets to enter China in this life and be a Chinese in the afterlife!

1, Study

It is October 31, 2021, and the epidemic has achieved phased victory.

As a programmer, what I can do is not to add trouble to the country.

So I usually don't go out to wave. It's good to study at home and improve myself.

When I looked at the source code of ReentrantLock, AQS was involved, and the template method pattern was used, so I intend to output an article on template method pattern today.

During the epidemic period, all were isolated at home, so some were learning to cook, some were losing weight, and some were making babies.

Let's take this as an example to talk about the template method.

Code implementation:

AbstractRegulations

package test;


/**
 * @author Day and night programming of Muzi
 */
public abstract class AbstractRegulations {

    // Everyone has his own name
    String name;
    public AbstractRegulations(){}
    public AbstractRegulations(String name) {
        this.name = name;
    }
    // The state specifies your scope of action and all approximate activities
    // But whatever you do at home
    public void doWhat(){
        System.out.println(name+"At 9 o'clock in the morning, the community should wear a mask and get food downstairs.");
        stayAtHome();
        System.out.println(name+"At 8 p.m., wear a mask at your door and wait for volunteers to measure their temperature.");
    }

    protected void stayAtHome() {
        throw new UnsupportedOperationException();
    }
}

XiaoMingRegulations

package test;

/**
 * @author Day and night programming of Muzi
 */
public class XiaoMingRegulations extends AbstractRegulations{
    public XiaoMingRegulations(){}
    public XiaoMingRegulations(String name){
        super(name);
    }
    // Xiao Ming defines what he wants to do
    @Override
    protected void stayAtHome() {
        System.out.println("I'm Xiao Ming. I eat at home and eat fat.");
    }
}

XiaoQiangRegulations

package test;

/**
 * @author Day and night programming of Muzi
 */
public class XiaoQiangRegulations extends AbstractRegulations{
    public XiaoQiangRegulations(){}
    public XiaoQiangRegulations(String name){
        super(name);
    }
    // Xiaoqiang customizes what he does
    @Override
    protected void stayAtHome() {
        System.out.println("I'm Xiaoqiang. I practice my waist at home. I can practice bar.");
    }
}

Test

package test;

/**
 * @author Day and night programming of Muzi
 */
public class Test {
    public static void main(String[] args) {
        new XiaoMingRegulations("Xiao Ming").doWhat();
        System.out.println("---------------Gorgeous dividing line-------------------");
        new XiaoQiangRegulations("cockroach").doWhat();
    }
}

Output:

Xiao Ming wears a mask in the community at 9 a.m. and leads dishes downstairs.
I'm Xiao Ming. I eat at home and eat fat.
Xiao Ming wears a mask at his door at 8 p.m. and waits for volunteers to take their temperature.
---------------Gorgeous dividing line-------------------
Xiaoqiang wears a mask in the community at 9 a.m. and leads the dishes downstairs.
I'm Xiaoqiang. I practice my waist at home. I can practice bar.
Xiaoqiang wears a mask at his door at 8 p.m. and waits for volunteers to take their temperature.

2, Nagging

People who have read my previous articles know that AQS also uses the template method mode.

Let's review the AQS code a little

public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {
    
    // Trying to get credentials
    public final void acquire(int arg) {
        // Call tryAcquire to try to get it once
        if (!tryAcquire(arg) &&
            // If the acquisition fails, it will be put in the queue
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
    // You can see that tryAcquire is not implemented, and you need subclasses to implement it yourself
    protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }
}

So when we see this, don't be confused. This kind of implementation is generally a subclass implementation. Just look at its corresponding implementation class.

3, More

More? Today, I found a simple knowledge point to output the article.

There is still some work unfinished. I have to work overtime at home.

Working people, working souls, working people are people. If you don't move bricks today, your position will not be stable tomorrow.

Goodbye, workers.

Tags: Java Design Pattern Interview

Posted on Sat, 30 Oct 2021 23:07:00 -0400 by dsds1121