@@ -1698,71 +1698,84 @@ First of all we have our memento object that will be able to hold the editor sta
16981698``` C#
16991699class EditorMemento
17001700{
1701- protected $content ;
1701+ private string mContent ;
17021702
1703- public function __construct (string $ content)
1704- {
1705- $ this -> content = $ content ;
1706- }
1703+ public EditorMemento (string content )
1704+ {
1705+ mContent = content ;
1706+ }
17071707
1708- public function getContent ()
1708+ public string Content
1709+ {
1710+ get
17091711 {
1710- return $ this -> content ;
1712+ return mContent ;
17111713 }
1714+ }
17121715}
17131716```
17141717
17151718Then we have our editor i.e. originator that is going to use memento object
17161719
17171720``` C#
1718- class Editor
1719- {
1720- protected $content = '' ;
1721+ class Editor {
17211722
1722- public function type (string $words)
1723- {
1724- $this ->content = $this ->content . ' ' . $words ;
1725- }
1723+ private string mContent = string .Empty ;
1724+ private EditorMemento memento ;
17261725
1727- public function getContent ()
1728- {
1729- return $ this -> content ;
1730- }
1726+ public Editor ()
1727+ {
1728+ memento = new EditorMemento ( string . Empty ) ;
1729+ }
17311730
1732- public function save ( )
1733- {
1734- return new EditorMemento ($ this -> content );
1735- }
1731+ public void Type ( string words )
1732+ {
1733+ mContent = String . Concat ( mContent , " " , words );
1734+ }
17361735
1737- public function restore (EditorMemento $memento)
1736+ public string Content
1737+ {
1738+ get
17381739 {
1739- $ this -> content = $ memento -> getContent () ;
1740+ return mContent ;
17401741 }
1742+ }
1743+
1744+ public void Save ()
1745+ {
1746+ memento = new EditorMemento (mContent );
1747+ }
1748+
1749+ public void Restore ()
1750+ {
1751+ mContent = memento .Content ;
1752+ }
17411753}
17421754```
17431755
17441756And then it can be used as
17451757
17461758``` C#
1747- $ editor = new Editor ();
1759+ var editor = new Editor ();
17481760
1749- // Type some stuff
1750- $ editor -> type ( ' This is the first sentence.' );
1751- $ editor -> type ( ' This is second.' );
1761+ // Type some stuff
1762+ editor . Type ( " This is the first sentence." );
1763+ editor . Type ( " This is second." );
17521764
17531765// Save the state to restore to : This is the first sentence. This is second.
1754- $saved = $editor ->save ();
1766+ editor .Save ();
1767+
1768+ // Type some more
1769+ editor .Type (" This is thrid." );
17551770
1756- // Type some more
1757- $ editor -> type ( 'And this is third.' );
1771+ // Output the content
1772+ Console . WriteLine ( editor . Content ); // This is the first sentence. This is second. This is third.
17581773
1759- // Output: Content before Saving
1760- echo $ editor -> getContent (); // This is the first sentence. This is second. And this is third.
1774+ // Restoring to last saved state
1775+ editor . Restore ();
17611776
1762- // Restoring to last saved state
1763- $editor ->restore ($saved );
1777+ Console .Write (editor .Content ); // This is the first sentence. This is second
17641778
1765- $editor ->getContent (); // This is the first sentence. This is second.
17661779```
17671780
17681781😎 Observer
0 commit comments