Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: This class represents the Flyweight object. It contains the intrinsic state that is shared across multiple instances (the character symbol, in this case), and it provides a Display method to show the charac…
Short answer: In a more advanced system, you could dynamically choose which factory to use based on external configurations, like settings or environment variables. This would enable the system to switch between differen…
Short answer: As mentioned, logging systems often use the Factory Method to allow different log outputs. For example, a logging framework can provide loggers that write to the console, files, databases, or cloud services…
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Short answer: This interface defines the common method Log that will be implemented by all types of loggers (e.g., file, console). public interface ILogger Example code { void Log(string message); } Say this in the inter…
Short answer: If the home theater system grows, you can easily add more components (e.g., projector, lights, sound system) to the facade without modifying the client code. This extends the flexibility of the facade as th…
Short answer: In real-life home theaters, turning on multiple devices like an amplifier, DVD player, and projector can be tedious. A Facade Pattern can simplify this into a single button or command (like WatchMovie()), w…
Short answer: By providing a unified interface, the Facade Pattern simplifies the interaction with complex subsystems. The user only needs to interact with a few high-level methods, making the system easier to use. Real-…
Short answer: These classes represent the components of the complex subsystem. They have specific functionalities but are complicated to interact with individually. Amplifier: public class Amplifier Example code { public…
Short answer: You could introduce more decorators, such as WhippedCreamDecorator, ChocolateDecorator, or CaramelDecorator, for a richer coffee experience. Real-world example (ShopNest) Logging and caching decorators wrap…
Short answer: The Decorator Pattern is ideal for situations like coffee shops, where customers can customize their coffee with various add-ons (milk, sugar, whipped cream, flavor syrups, etc.). Each add-on is a decorator…
Short answer: ICoffee defines the basic methods that any coffee type must implement. This allows the decorators to work with any class that implements this interface, providing flexibility to decorate any coffee object.…
Short answer: This is the base interface that defines the common methods for the Cost() and Description() that every coffee component will implement. public interface ICoffee Example code { double Cost(); string Descript…
Short answer: The Directory class currently only has an Add() method for adding components. It could be improved by adding a Remove() method to allow for the dynamic removal of files or subdirectories. Real-world example…
Short answer: The most common application of the Composite Pattern is in representing file systems. A file system is inherently hierarchical: directories contain files and subdirectories, and those subdirectories can con…
Short answer: Both files and directories are treated the same, as they implement the IFileSystemComponent interface. This makes it easier to manage a mixed structure of individual and composite objects. Real-world exampl…
Short answer: Both File and Directory implement the IFileSystemComponent interface, which defines the common method ShowInfo(). This allows us to treat both files and directories uniformly. Real-world example (ShopNest)…
Short answer: This is the base interface that defines a common method ShowInfo() that will be implemented by both leaf and composite components. public interface IFileSystemComponent Example code { void ShowInfo(); } Rea…
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Short answer: The sender (the TextEditor) is decoupled from the receiver (Document). The TextEditor only knows how to invoke commands but does not need to understand the details of the operations (e.g., how the text is a…
Short answer: The command (in this case, AddTextCommand) encapsulates the request to add text to the document as an object. This allows for parameterization of the command with different requests (e.g., adding different…
Short answer: The ICommand interface defines two methods: ■ Execute(): Executes the command. ■ Undo(): Reverts the command if the user wishes to undo the action. public interface ICommand Example code { void Execute(); v…
Short answer: The Chain of Responsibility allows handlers to be decoupled from the client code. The client doesn’t need to know which handler will process the request, only that the request will eventually be processed b…
Short answer: In the Program class, we set up a chain of responsibility by calling SetNext() on the infoLogger and passing it the errorLogger. This ensures that the InfoLogger will process log messages with the Info leve…
Short answer: The Logger class is the handler interface that defines the contract for handling log messages. It includes a reference to the next logger in the chain (NextLogger) and a method (LogMessage) to process a mes…
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: This class represents the Flyweight object. It contains the intrinsic state that is shared across multiple instances (the character symbol, in this case), and it provides a Display method to show the character's symbol at a particular coordinate. public class Character
{
private readonly char _symbol;
public Character(char symbol) => _symbol = symbol;
public void Display(int x, int y) => Console.WriteLine($"Character: {_symbol} at ({x}, {y})"); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: In a more advanced system, you could dynamically choose which factory to use based on external configurations, like settings or environment variables. This would enable the system to switch between different logging mechanisms or database connections without recompiling the application.
ShopNest’s NotificationFactory creates Email or SMS senders based on user preference.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: As mentioned, logging systems often use the Factory Method to allow different log outputs. For example, a logging framework can provide loggers that write to the console, files, databases, or cloud services, with the user choosing the appropriate logger type via a factory.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly.
ShopNest’s NotificationFactory creates Email or SMS senders based on user preference.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: This interface defines the common method Log that will be implemented by all types of loggers (e.g., file, console). public interface ILogger
{ void Log(string message); }
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: If the home theater system grows, you can easily add more components (e.g., projector, lights, sound system) to the facade without modifying the client code. This extends the flexibility of the facade as the system becomes more complex.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: In real-life home theaters, turning on multiple devices like an amplifier, DVD player, and projector can be tedious. A Facade Pattern can simplify this into a single button or command (like WatchMovie()), where the user only needs to press one button to turn everything on.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: By providing a unified interface, the Facade Pattern simplifies the interaction with complex subsystems. The user only needs to interact with a few high-level methods, making the system easier to use.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: These classes represent the components of the complex subsystem. They have specific functionalities but are complicated to interact with individually. Amplifier: public class Amplifier
{
public void On() => Console.WriteLine("Amplifier is on.");
public void Off() => Console.WriteLine("Amplifier is off.");
} DVDPlayer: public class DVDPlayer
{
public void Play(string movie) => Console.WriteLine($"Playing {movie}."); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: You could introduce more decorators, such as WhippedCreamDecorator, ChocolateDecorator, or CaramelDecorator, for a richer coffee experience.
Logging and caching decorators wrap IProductRepository so core SQL code stays clean.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Decorator Pattern is ideal for situations like coffee shops, where customers can customize their coffee with various add-ons (milk, sugar, whipped cream, flavor syrups, etc.). Each add-on is a decorator that adds a cost and modifies the description of the order without modifying the core coffee object.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: ICoffee defines the basic methods that any coffee type must implement. This allows the decorators to work with any class that implements this interface, providing flexibility to decorate any coffee object.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: This is the base interface that defines the common methods for the Cost() and Description() that every coffee component will implement. public interface ICoffee
{
double Cost();
string Description();
}
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Directory class currently only has an Add() method for adding components. It could be improved by adding a Remove() method to allow for the dynamic removal of files or subdirectories.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The most common application of the Composite Pattern is in representing file systems. A file system is inherently hierarchical: directories contain files and subdirectories, and those subdirectories can contain further files or subdirectories. The Composite Pattern allows for easy traversal and management of this hierarchical structure.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: Both files and directories are treated the same, as they implement the IFileSystemComponent interface. This makes it easier to manage a mixed structure of individual and composite objects.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: Both File and Directory implement the IFileSystemComponent interface, which defines the common method ShowInfo(). This allows us to treat both files and directories uniformly.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: This is the base interface that defines a common method ShowInfo() that will be implemented by both leaf and composite components. public interface IFileSystemComponent
{ void ShowInfo(); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The sender (the TextEditor) is decoupled from the receiver (Document). The TextEditor only knows how to invoke commands but does not need to understand the details of the operations (e.g., how the text is added or removed).
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The command (in this case, AddTextCommand) encapsulates the request to add text to the document as an object. This allows for parameterization of the command with different requests (e.g., adding different text) while decoupling the sender (the TextEditor) from the receiver (Document).
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The ICommand interface defines two methods: ■ Execute(): Executes the command. ■ Undo(): Reverts the command if the user wishes to undo the action. public interface ICommand
{ void Execute(); void Undo(); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Chain of Responsibility allows handlers to be decoupled from the client code. The client doesn’t need to know which handler will process the request, only that the request will eventually be processed by some handler in the chain.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: In the Program class, we set up a chain of responsibility by calling SetNext() on the infoLogger and passing it the errorLogger. This ensures that the InfoLogger will process log messages with the Info level, while the ErrorLogger will handle Error level messages.
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Logger class is the handler interface that defines the contract for handling log messages. It includes a reference to the next logger in the chain (NextLogger) and a method (LogMessage) to process a message. If the current handler cannot handle the message, it passes the request along to the next handler in the chain. public abstract class Logger
{ protected Logger NextLogger; public void SetNext(Logger nextLogger) => NextLogger = nextLogger; public void LogMessage(string message, LogLevel level)
{
if (CanHandle(level))
{ Handle(message); } else { NextLogger?.LogMessage(message, level); }
} protected abstract bool CanHandle(LogLevel level); protected abstract void Handle(string message); }
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.