Technology Sharing

Beacon tower of the Battle of Red Cliffs - Observer mode

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

“When war rages for three months, a letter from home is worth a ton of gold; when the design pattern is right, thousands of troops can be united.”

In the magnificent history of the Three Kingdoms, the Battle of Red Cliffs was undoubtedly an important battle that changed the world. In this battle, a seemingly simple but crucial system played a huge role - the beacon tower. Does this ancient communication system remind you of the observer pattern in modern software design? Today, let us travel through thousands of years and unveil the mystery of the observer pattern from the smoke of the beacon tower.

The battle clouds rose on the banks of the Red Cliff River.
Once the beacon fire is lit, the whole world will know about it.
Observe the changes and respond accordingly.
Defeat the enemy in one move and save the country.

War

In 208 AD, Cao Cao led his army southward, intending to conquer Jiangdong and unify the world in one fell swoop. Facing the aggressive Cao army, the Sun-Liu coalition had to make careful plans to defeat the strong with the weak.

"If we want to respond to Cao's army's movements in a timely manner, we must build an efficient early warning system." Zhou Yu frowned in thought.

Zhuge Liang waved his feather fan and said confidently: "In my opinion, we can set up beacon towers along the river. Once we discover the enemy, we can light the beacon fire. Wherever there is smoke, there must be enemy troops."

Zhou Yu's eyes lit up: "Great! This way, our army can keep track of the enemy's movements at any time and be prepared for any eventuality."

In this way, beacon towers stood like sentinels along the Yangtze River, and each tower was manned by a dedicated person to guard it day and night, and to light the beacon fire immediately if Cao's army was spotted.

One day, the beacon tower guards on the front line suddenly discovered traces of Cao's vanguard troops.

"Quick! Light the beacon!" the guard shouted.

Instantly, thick smoke billowed into the sky. Then, the second and third beacon towers were lit one after another, and the smoke signal spread quickly like dominoes.

In the army tent by the river, Zhou Yu was discussing military situation with Zhuge Liang. Suddenly, a soldier came running over:

"Report! The front line is ablaze with fire, Cao's army has arrived!"

Zhou Yu and Zhuge Liang smiled at each other and said confidently, "Pass the order down and act according to the plan!"

Observer Pattern Analysis

The observer pattern defines a one-to-many dependency relationship, allowing multiple observer objects to monitor a subject object at the same time. When the state of this subject object changes, it will notify all observer objects so that they can automatically update themselves.

In the beacon tower system of the Battle of Red Cliff, we can clearly see the shadow of the observer mode:

  • Beacon is the subject being observed (Subject)
  • Each military unit (such as Zhou Yu and Zhuge Liang's headquarters) is an observer.
  • The lighting of the beacon is a change of state
  • The transmission of smoke signals is a notification mechanism

Let's use a class diagram to understand the structure of the observer pattern:

image-20240707223121510

Code

Let's implement this beacon system in Java:

import java.util.ArrayList;
import java.util.List;

// 观察者接口
interface MilitaryUnit {
    void update(String message);
}

// 主题(烽火台)
class BeaconTower {
    private List<MilitaryUnit> observers = new ArrayList<>();
    private String message;

    public void attach(MilitaryUnit observer) {
        observers.add(observer);
    }

    public void detach(MilitaryUnit observer) {
        observers.remove(observer);
    }

    public void notify(String message) {
        this.message = message;
        for (MilitaryUnit observer : observers) {
            observer.update(message);
        }
    }

    public void signalEnemyMovement(String message) {
        System.out.println("烽火台: " + message);
        notify(message);
    }
}

// 具体观察者
class CommandCenter implements MilitaryUnit {
    private String name;

    public CommandCenter(String name) {
        this.name = name;
    }

    @Override
    public void update(String message) {
        System.out.println(name + " 收到消息: " + message);
    }
}

// 主类
public class ObserverPatternDemo {
    public static void main(String[] args) {
        BeaconTower beaconTower = new BeaconTower();

        CommandCenter zhouYu = new CommandCenter("周瑜");
        CommandCenter zhuGeLiang = new CommandCenter("诸葛亮");

        beaconTower.attach(zhouYu);
        beaconTower.attach(zhuGeLiang);

        beaconTower.signalEnemyMovement("曹军前锋已到达赤壁!");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

operation result

烽火台: 曹军前锋已到达赤壁!
周瑜 收到消息: 曹军前锋已到达赤壁!
诸葛亮 收到消息: 曹军前锋已到达赤壁!
  • 1
  • 2
  • 3

The magic of the observer pattern

Through the beacon tower system of the Battle of Chibi, we can see several key advantages of the observer mode:

  1. Decoupling:The beacon tower (subject) does not need to know which specific military units (observers) are listening to it, and the military units do not need to understand the internal implementation of the beacon tower. This loosely coupled design improves the flexibility of the system.
  2. Scalability: If you need to add a new response unit (such as a logistics department), you only need to create a new observer and register it to Parllay without modifying the existing code.
  3. Broadcast Communications: A message can be quickly conveyed to multiple recipients, just like a beacon fire, so that the entire defense line can quickly learn about the enemy situation.
  4. Responsive design: The observer pattern is very suitable for implementing responsive system design. When the state changes, all related parts can be updated in time.

Conclusion

Zhuge Liang's beacon tower system not only helped win the Battle of Red Cliff, but also revealed the essence of the observer pattern. In modern software development, the observer pattern is widely used in many fields such as graphical user interfaces, event processing systems, and message push services.

As the saying goes, "Planning in the tent, winning the battle thousands of miles away", by mastering the observer pattern, we can build responsive and well-structured software systems to remain invincible in the ever-changing digital battlefield.

In the next chapter, we will continue our journey of design patterns and explore more subtle combinations of the wisdom of the Three Kingdoms and modern software design. Stay tuned!

Reference:https://offernow.cn
AI Learning Assistant:https://aistar.cool