2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
“The military has no fixed position, and water has no fixed shape. Those who can win by adapting to the enemy’s changes are called gods.”
On the battlefield of the Three Kingdoms, military tactics were like chess and battle formations were like paintings. Isn't Cao Cao's five-color chess formation a perfect portrayal of the factory method pattern in today's software design? Let's unveil the mystery of the factory method pattern from this magical battle formation method.
The five-colored flags were deployed in battle formation.
The eight golden locks are at your disposal.
Factory method creates corps,
Be flexible and adaptable before things happen.
In the 13th year of Jian'an, Cao Cao and Ma Chao were in a stalemate in Weinan. Ma Chao was brave and good at fighting, and Cao's army was repeatedly defeated. One day, Cao Cao sat alone in his tent, facing the sand table and thinking hard.
"My lord, do you have any good plan?" asked military advisor Xun You.
Cao Cao raised his head slowly, his eyes gleaming, "I thought of a formation called 'Five-Color Chess Formation'. This formation is divided into five parts, arranged according to the theory of the mutual generation and mutual restraint of the five elements."
Xun You asked curiously, "My Lord, please tell me clearly."
Cao Cao took out the five-colored chess pieces and arranged them on the sand table: "Gold, wood, water, fire, and earth, the five elements complement and restrain each other. Each color of chess piece represents a type of soldier, and the position can be changed at any time. The enemy will always see the changing formation, but it is difficult to perceive the mystery."
Xun You suddenly realized: "Great! These five types of troops are like five factories, which can produce the troops we need at any time."
Cao Cao nodded, "That's right. Gold is for killing, wood is for growth, water is for wisdom, fire is for power, and earth is for defense. When facing a battle, you can adapt to the situation and make decisions according to the enemy."
The next day, Cao Cao set up a five-color chess formation. Ma Chao led his army to attack, but found that Cao's army's formation was unpredictable, and he could neither attack nor defend, and finally returned in defeat.
The factory method pattern defines an interface for creating objects, but it is up to the subclass to decide which class to instantiate. The factory method lets the class defer instantiation to the subclass.
Cao Cao's five-color chess formation is similar to the factory method pattern:
Let's use a class diagram to understand the structure of the Factory Method pattern:
Let's use Java to implement this five-color chess system:
// 军队单位接口
interface Unit {
void attack();
void defend();
}
// 军队工厂接口
interface ArmyFactory {
Unit createUnit();
}
// 具体军队单位
class Cavalry implements Unit {
public void attack() { System.out.println("骑兵发起冲锋!"); }
public void defend() { System.out.println("骑兵列阵防守!"); }
}
class Archer implements Unit {
public void attack() { System.out.println("弓箭手放箭!"); }
public void defend() { System.out.println("弓箭手设防!"); }
}
class Scout implements Unit {
public void attack() { System.out.println("斥候发动偷袭!"); }
public void defend() { System.out.println("斥候隐蔽!"); }
}
class Infantry implements Unit {
public void attack() { System.out.println("步兵发起进攻!"); }
public void defend() { System.out.println("步兵筑起盾墙!"); }
}
class Guardian implements Unit {
public void attack() { System.out.println("守卫进行反击!"); }
public void defend() { System.out.println("守卫加强防御!"); }
}
// 具体军队工厂
class GoldArmyFactory implements ArmyFactory {
public Unit createUnit() { return new Cavalry(); }
}
class WoodArmyFactory implements ArmyFactory {
public Unit createUnit() { return new Archer(); }
}
class WaterArmyFactory implements ArmyFactory {
public Unit createUnit() { return new Scout(); }
}
class FireArmyFactory implements ArmyFactory {
public Unit createUnit() { return new Infantry(); }
}
class EarthArmyFactory implements ArmyFactory {
public Unit createUnit() { return new Guardian(); }
}
// 曹操的五色棋布阵
class FiveColorChessFormation {
private ArmyFactory[] factories;
public FiveColorChessFormation() {
factories = new ArmyFactory[] {
new GoldArmyFactory(),
new WoodArmyFactory(),
new WaterArmyFactory(),
new FireArmyFactory(),
new EarthArmyFactory()
};
}
public void deployTroops() {
System.out.println("曹操:部署五色棋布阵!");
for (ArmyFactory factory : factories) {
Unit unit = factory.createUnit();
unit.attack();
unit.defend();
}
}
}
// 主类
public class FactoryMethodPatternDemo {
public static void main(String[] args) {
FiveColorChessFormation caoCaoFormation = new FiveColorChessFormation();
caoCaoFormation.deployTroops();
}
}
曹操:部署五色棋布阵!
骑兵发起冲锋!
骑兵列阵防守!
弓箭手放箭!
弓箭手设防!
斥候发动偷袭!
斥候隐蔽!
步兵发起进攻!
步兵筑起盾墙!
守卫进行反击!
守卫加强防御!
Through Cao Cao's five-color chess formation, we can see several key advantages of the factory method pattern:
Cao Cao's five-color chess formation not only demonstrates his superb military talent, but also reveals the essence of the factory method pattern. In software development, the factory method pattern is widely used in many fields such as framework design, plug-in systems, and configurable applications.
Just as Cao Cao could flexibly dispatch different types of troops according to the battle situation, our software system can also achieve flexibility and scalability in object creation through the factory method pattern. By mastering this design pattern, we can remain invincible in the battlefield of software architecture.
Reference:https://offernow.cn
AI Learning Assistant:https://aistar.cool