We are advancing through our journey of building various real-world applications. We have explored the Command, Observer, and Strategy patterns in the previous units. In this unit, we will integrate all three behavioral patterns into different scenarios to solve real-world problems.
In this unit, we will use different combinations of behavioral patterns to solve real-world problems. Let's have a quick recap on what each pattern does:
- Command Pattern: Encapsulates a request as an object, allowing clients to parameterize and queue requests.
- Observer Pattern: Defines a one-to-many dependency between objects, ensuring that when one object changes state, all its dependents are notified and updated automatically.
- Strategy Pattern: Defines a family of algorithms, encapsulates each one and makes them interchangeable. Clients can choose the algorithm to use at runtime.
Here is one scenario of using Command
and Observer
patterns together to build a chat application.
First, we define a base ICommand
interface with an Execute
method. This interface will serve as the blueprint for all command objects.
C#1// Command Pattern Interface for Chat Application 2interface ICommand { 3 // Method to execute the command 4 void Execute(); 5}
We will also define a User
class where that can receive and print messages. This class represents the observer in the Observer pattern.
C#1class User { 2 private string name; 3 4 // Constructor to initialize the user's name 5 public User(string name) { 6 this.name = name; 7 } 8 9 // Method to receive a message, which gets printed to the console 10 public void ReceiveMessage(string message) { 11 Console.WriteLine($"{name} received message: {message}"); 12 } 13}
Next, we define a ChatRoom
class with methods to display messages and manage the list of users in the chat room. This class will act as the subject in the Observer pattern.
C#1class ChatRoom { 2 // List to keep track of users in the chat room 3 private List<User> users = new List<User>(); 4 5 // Method to display message in chat room 6 public void ShowMessage(string message) { 7 Console.WriteLine($"Message: {message}"); 8 } 9 10 // Method to add a user to the chat room 11 public void AddUser(User user) { 12 users.Add(user); 13 } 14 15 // Method to send message to all users in chat room 16 public void SendMessage(string message) { 17 foreach (var user in users) { 18 user.ReceiveMessage(message); 19 } 20 } 21}
Finally, the ChatCommand
class implements the ICommand
interface and encapsulates the action of showing a message. When creating a ChatCommand
, you set the message and the chat room where the message will be displayed, then execute this command to display and broadcast the message.
C#1class ChatCommand : ICommand { 2 private ChatRoom chatRoom; 3 private string message; 4 5 // Constructor to initialize chat room and message 6 public ChatCommand(ChatRoom chatRoom, string message) { 7 this.chatRoom = chatRoom; 8 this.message = message; 9 } 10 11 // Execute method to show and send message in chat room 12 public void Execute() { 13 chatRoom.ShowMessage(message); 14 chatRoom.SendMessage(message); 15 } 16}
Below is the combined structure of our chat application. This demonstrates how the Command and Observer patterns work together:
C#1class Program { 2 static void Main() { 3 // Creating chat room 4 ChatRoom chatRoom = new ChatRoom(); 5 6 // Creating users 7 User user1 = new User("Alice"); 8 User user2 = new User("Bob"); 9 10 // Adding users to chat room 11 chatRoom.AddUser(user1); 12 chatRoom.AddUser(user2); 13 14 // Creating and executing chat command 15 ICommand chatCommand = new ChatCommand(chatRoom, "Hello, everyone!"); 16 chatCommand.Execute(); 17 // Output: 18 // Message: Hello, everyone! 19 // Alice received message: Hello, everyone! 20 // Bob received message: Hello, everyone! 21 } 22}
In this structure:
- The
ChatRoom
acts as a central hub. - Commands encapsulate user actions such as sending messages.
- Users observe the chat room and receive messages when they are sent.
By the end of this unit, you will have a functional chat application in which messages can be sent and received efficiently. You will also have a deeper understanding of how combining multiple behavioral patterns can solve complex real-world problems effectively. Let’s get started with the practice section and see these concepts come to life in our chat application!