PHP design mode-Memorandum Mode
The memorandum mode is also called the snapshot mode or Token mode. It captures the internal state of an object without interrupting the closed state and saves the State outside the object. In this way, the object can be restored to the previously saved state.
UML class diagram:
Role:
1. The initiator (GameRole) is responsible for creating a memorandum to record its internal status at the current time and restoring its internal status using the memorandum. The initiator can determine the internal states of the memorandum as needed.
2. Memorandum (RoleStateSaveBox): stores the internal status of the initiator object and prevents other objects other than the initiator from accessing the memorandum. The memorandum has two interfaces: the manager can only see the narrow interface of the Memorandum, and he can only pass the memorandum to other objects. The initiator can see the wide interface of the memo, allowing it to access all data returned to the previous status.
3. GameRoleStateManager: It is responsible for accessing the Memorandum and cannot access or operate on the content.
Core code:
LiveLevel, $ this-> attackLevel, $ this-> defenseLevel);} // restore the public function RecoveryState (RoleStateMemento $ _ memento) {$ this-> liveLevel =$ _ memento-> liveLevel; $ this-> attackLevel =$ _ memento-> attackLevel; $ this-> defenseLevel =$ _ memento-> defenseLevel;} // ------------ other attributes and Operations ------------ // obtain the Initial State public function GetInitState () {$ this-> defenseLevel = 100; $ this-> attackLevel = 100; $ this-> liveLev El = 100;} // The public function StateDisplay () {echo role status:; echo vitality: {$ this-> liveLevel}; echo Attack Strength: {$ this-> attackLevel}; echo defensive strength: {$ this-> defenseLevel};} // public function BeenAttack () {$ this-> liveLevel-= 9.5; if ($ this-> liveLevel <= 0) {$ this-> liveLevel = 0; echo er, this role is killed !; Echo Game Over !; Return;} $ this-> attackLevel-= 1.1; if ($ this-> attackLevel <= 0) {$ this-> attackLevel = 0 ;} $ this-> defenseLevel-= 0.5; if ($ this-> defenseLevel <= 0) {$ this-> defenseLevel = 0 ;}}} // role status storage box class RoleStateMemento {public $ liveLevel; public $ attackLevel; public $ defenseLevel; public function RoleStateMemento ($ _ ll, $ _ al, $ _ dl) {$ this-> liveLevel =$ _ ll; $ this-> attackLevel =$ _ al; $ this-> defenseLevel =$ _ dl ;}} // game role status manager class RoleStateManager {public $ memento ;}
Test code:
GetInitState (); echo ---------------- before the war ---------------; $ ufo-> StateDisplay (); // Save the progress $ roleMan = new RoleStateManager (); $ roleMan-> memento = $ ufo-> SaveState (); echo ---------------- combat -----------------; $ num = 1; // vs Boss5 rounds for ($ I = 0; $ I <13; $ I ++) {echo ------------- round {$ num} -----------; $ ufo-> BeenAttack (); $ ufo-> StateDisplay (); $ num ++; // if ($ ufo-> liveLevel <= 0) {break;} echo ---------------- restore status -----------------; // restore the previous status $ ufo-> RecoveryState ($ roleMan-> memento); $ ufo-> StateDisplay ();
Advantages:
1. Sometimes some internal information of the initiator object must be stored outside the initiator object, but it must be read by the initiator object itself, the memorandum mode can be used to shield the internal information of the complex initiator from other objects, so as to properly maintain the encapsulated boundary.
2. This model simplifies initiation of humans. The initiators no longer need to manage and save versions of their internal statuses. The client can manage the versions of these statuses as needed.
Disadvantages:
1. If the status of the initiator role needs to be completely stored in the memorandum object, the memorandum object will be expensive in terms of resource consumption.
2. When the owner's role stores a memorandum, the owner may not know how much storage space it will occupy, and thus cannot remind the user whether an operation is expensive.
Welcome to my video course. The address is as follows. Thank you.
PHP Object-Oriented Design Model