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.
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!"
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:
Let's use a class diagram to understand the structure of the observer pattern:
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("曹军前锋已到达赤壁!");
}
}
烽火台: 曹军前锋已到达赤壁!
周瑜 收到消息: 曹军前锋已到达赤壁!
诸葛亮 收到消息: 曹军前锋已到达赤壁!
Through the beacon tower system of the Battle of Chibi, we can see several key advantages of the observer mode:
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