Mid GoF Patterns

Spreadsheet Software:?

  • Spreadsheet applications like Excel often use the Memento Pattern to save

different states of a spreadsheet, enabling the user to undo changes like

deleting a cell or modifying a formula.

Visual Diagram:

+----------------+ +--------------------+

| TextEditor | Save() | TextMemento |

| (Originator) |------------>| (Memento) |

+----------------+ +--------------------+

| ^

Write Text Restore

| |

v |

+----------------+ +--------------------+

| Caretaker |<------------| TextMemento |

| (History) | Undo() | (Memento) |

Follow:

+----------------+ +--------------------+

Conclusion:

The Memento Pattern is a powerful design pattern for handling state restoration in software

systems, especially when implementing undo functionality. It helps maintain encapsulation

while allowing objects to restore their previous states. Although it has potential drawbacks in

terms of memory usage and complexity, the Memento Pattern remains invaluable for

applications that require maintaining and reverting state, such as text editors, games, and

form-based applications.

Observer Pattern: Real-Time Example - News Feed System

Definition:

The Observer Pattern defines a one-to-many dependency between objects, where when

one object (the "subject") changes state, all its dependent objects (the "observers") are

notified and updated automatically. This pattern is often used in scenarios where an object’s

state changes frequently and multiple objects need to react to those changes, like a user

interface or event-driven systems.

Use Case:

A common use case of the Observer Pattern is a news feed where users (observers) need

to be notified whenever a new article (news) is published (state change). For example, in a

news publishing system, the publisher notifies all subscribers when a new news article is

published.

Code Breakdown:

More from Design Patterns in C#

All questions for this course