What is the Decorator Design Pattern? The Decorator design pattern is a structural pattern that lets you add new behavior to a class by “wrapping” it inside another class. This pattern is sometimes called the Wrapper Design Pattern. You may use this pattern in Domain-Driven Design, where something like an Order class may have a different meaning to the different departments of a company – and require different properties. Sample code description This code will be for a pizza restaurant. People can order a pizza to eat in the dining room, for picking up, or for delivery. For each of…
Leave a CommentCategory: C# Design Patterns
What is the Adapter Design Pattern? The Adapter design pattern is a structural pattern, used to allow objects of one type to be used in an application or function that expects objects of a different type. You may use this pattern when your program needs to submit data to a web service or API, which is expecting the data as a different datatype. In this example, we’ll simulate a program that creates an Order object (with Customer and OrderItem objects in its properties) and needs to submit a shipping request to a different program, that expects objects of a different…
Leave a CommentWhat is the Publish/Subscribe Design Pattern? The Publish/Subscribe Design Pattern is a technique for one or more objects to know when something happens to another object – without them needing to continually inspect the object. In this example, I’ll show a non-pattern way to monitor an object, a way using the publish/subscribe pattern, and an enhanced way to pass additional information when “publishing”. The sample code will be for a game. If the player moves to a location that is poisonous, they take damage. If the player’s hit points drop to zero or less, the game wants to know, so…
Leave a CommentWhat is the Data Mapper Design Pattern? The Data Mapper design pattern is a technique to save data from in-memory objects to the database, and load data from the database into in-memory objects. This pattern has the code for saving and loading inside a separate class from the model class. The Active Record Design Pattern is a similar pattern but moves the code for database saving and loading inside the Model class. Example C# source code for the Data Mapper design pattern For this example, we’ll use this simple Customer class as the Model. It only has properties. It could…
2 CommentsWhat is the Active Record Design Pattern? The Active Record design pattern is a technique to save data from in-memory objects to the database, and load data from the database into in-memory objects. This pattern includes the code for saving and loading inside the model class. The Data Mapper Design Pattern is a similar pattern but moves the saving and loading code out of the model, into a separate class. Example C# source code for the Active Record design pattern Here, we have a typical Customer class. On lines 8 and 9, we have the connection string for the database.…
Leave a CommentWhat is the Memento Design Pattern? The Memento Design Pattern is a behavioral pattern, used to save an object’s state (its property values) to allow for restoring the object’s properties to their previous values (rollback or undo changes). There are three components for the Memento Design Pattern: the Originator (class to maintain state history for), the Memento (holds a “snapshot” of the state), and the Caretaker (manages saving and restoring mementos). Code Sample for Memento Design Pattern in C# Here’s how to use a memento to track multiple snapshots of a Customer object’s property values. In this example, I’ll have the…
Leave a CommentWhat is the Command Design Pattern? The Command Design Pattern is a behavioral pattern that puts the logic to execute a function into a separate class/object, so it can be executed independently of the model class/object. It’s especially useful for build enterprise-level programs, as it can improve reliability and scalability. Parts of the Command Design Pattern There are four parts to the Command pattern. Receiver: Business object that “receives” the action from the Command. Command: Classes that execute an action on a receiver object. Invoker: Class that tells the Command objects to execute their actions. Client: The main program that uses the other parts.…
Leave a CommentWhat is the Builder Design Pattern? The Builder Design Pattern is a creational design pattern. It’s used to create instances of objects whose constructors have many parameters, or when many properties need to be set before the object can be used. The Builder pattern provides a simpler way to pass all those parameters or set all those values. This example will be instantiating a Report object that would be used to create a report on orders at a company. In the sample code, the constructor only needs five parameters. But you can see how it could potentially need many more.…
Leave a CommentWhat is the Strategy Design Pattern? The Strategy design pattern is a behavioral pattern used to allow changing the algorithm used to perform a function. This is done by having a separate class for each method of implementing the algorithm. For a real-world example, imagine the function of “get lunch”. This could be implemented by “cook from scratch”, “microwave frozen meal”, “order food to be delivered”, or “go to a restaurant”. I’ll demonstrate the strategy pattern with a calculator class that calculates the average of a list of numbers. It will use either the mean or median algorithm, with each…
Leave a CommentWhat is the Singleton Design Pattern? The Singleton Design Pattern is used when you want every place in your program to use the same instance of a class. Singletons are often used as a place to maintain shared data or cached values, or for objects that take a long time to initialize. Why would you use a singleton? In this example, we’ll use the Singleton design pattern for a class that would write log messages to a log file. There might be file-locking problems if multiple objects are trying to write to the same file. So, we want all logging…
Leave a Comment