Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: The Flyweight Pattern could be extended to support composite objects where each flyweight can contain references to other flyweights. Explain a bit more For example, a complex character (e.g., with styling…
Short answer: GUI frameworks that display multiple similar graphical elements (e.g., buttons, labels, icons) can use the Flyweight Pattern to reuse common elements while only storing the unique aspects (such as position,…
Short answer: When the program requests a Character for each letter in the string "Hello World", the CharacterFactory checks if the character already exists. If it does, the existing object is reused; otherwise…
Short answer: The CharacterFactory is responsible for managing the flyweights. It keeps track of the existing character objects and ensures that no duplicate instances of the same character are created. If a character al…
Short answer: Instead of directly instantiating the factory within the client code, you could use dependency injection to pass the correct factory implementation into the client code. Explain a bit more This would make t…
Short answer: A document generation system might have different types of document formats, such as PDF, Word, or HTML. A Factory Method can be used to create the appropriate document generator based on user input, allowi…
Short answer: The ILogger interface is implemented by the concrete classes FileLogger and ConsoleLogger. These classes define how the log message will be handled, either by writing to a file or outputting to the console.…
Short answer: These classes implement the ILogger interface, defining how the messages are logged (either to a file or the console). FileLogger: public class FileLogger : ILogger Example code { public void Log(string mes…
Short answer: The facade can be enhanced with better exception handling. Explain a bit more For example, if a component fails (e.g., DVD player is missing), the facade could display a user-friendly message or take an app…
Short answer: A Facade Pattern can be used in financial systems where complex operations like credit checks, account updates, and transaction processing are abstracted into a simplified process, allowing users to perform…
Short answer: The Facade makes the system more readable. If the subsystem's complexity changes, the facade can be updated without affecting the client code. Real-world example (ShopNest) Use a GoF pattern in ShopNest onl…
Short answer: The Facade Pattern enables the user to work with a single entry point (i.e., the HomeTheaterFacade), which internally delegates tasks to the complex subsystem classes. This makes the system much easier to u…
Short answer: The Facade class simplifies interactions with the subsystem by providing a unified interface for the user. It wraps the complex subsystem and provides higher-level methods that internally call the appropria…
Short answer: Add functionality to dynamically change the properties of the decorated object. Explain a bit more For instance, you could add a feature to customize the size of the coffee (Small, Medium, Large) which woul…
Short answer: In text editors, the Decorator Pattern could be used for adding formatting options to text (bold, italic, underline) dynamically, without needing separate classes for each combination of formatting. Real-wo…
Short answer: The original SimpleCoffee class remains unchanged, which means you don’t need to touch existing code. The new functionality is added without altering or subclassing the core class. Real-world example (ShopN…
Short answer: The CoffeeDecorator class is abstract, and all concrete decorators (MilkDecorator, SugarDecorator) extend this class. The decorator wraps the SimpleCoffee object (or other decorated objects) and enhances or…
Short answer: The SimpleCoffee class is the basic concrete implementation of the ICoffee interface. It represents a basic coffee order with a cost and description. public class SimpleCoffee : ICoffee Example code { publi…
Short answer: You can extend the file system to support additional metadata for each file or directory, such as size, creation date, and file type. This could be added as properties in the File and Directory classes. Rea…
Short answer: The Composite Pattern can also be used in organizational structures, where departments (composites) contain teams or employees (individuals), and both can be treated as "components" with a common…
Short answer: The recursive structure makes it easy to manage hierarchical data. For example, when displaying the contents of a directory, you don’t need to worry about whether the child is a file or a subdirectory. Say…
Short answer: The File class represents the leaf in the tree structure. It is an individual component (a file) and implements the ShowInfo() method to display its details. public class File : IFileSystemComponent Example…
Short answer: You can combine multiple commands into a composite command. For example, if the user performs a series of actions (like adding text, changing fonts, and changing colors), you can encapsulate all of those ac…
Short answer: The Command Pattern makes it easy to add new commands without modifying existing code. For example, you can add new commands like RemoveTextCommand or ChangeFontCommand by simply implementing the ICommand i…
Short answer: The TextEditor maintains a stack of executed commands. When the Undo() method is called, it pops the most recent command from the stack and calls its Undo() method, which reverts the action performed by the…
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Flyweight Pattern could be extended to support composite objects where each flyweight can contain references to other flyweights.
For example, a complex character (e.g., with styling information) could consist of several flyweight components (like the base character, font style, size, etc.).
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: GUI frameworks that display multiple similar graphical elements (e.g., buttons, labels, icons) can use the Flyweight Pattern to reuse common elements while only storing the unique aspects (such as position, text, or color).
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: When the program requests a Character for each letter in the string "Hello World", the CharacterFactory checks if the character already exists. If it does, the existing object is reused; otherwise, a new Character object is created. As a result, memory is saved, and performance is improved by reusing common objects. Key Benefits of the Flyweight Pattern:
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 CharacterFactory is responsible for managing the flyweights. It keeps track of the existing character objects and ensures that no duplicate instances of the same character are created. If a character already exists in the dictionary, it is reused. Otherwise, a new Character object is created and stored for future use. public class CharacterFactory
{
private readonly Dictionary<char, Character> _characters = new
Dictionary<char, Character>();
public Character GetCharacter(char symbol)
{
if (!_characters.ContainsKey(symbol))
{
_characters[symbol] = new Character(symbol);
}
return _characters[symbol];
}
}
ShopNest’s NotificationFactory creates Email or SMS senders based on user preference.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: Instead of directly instantiating the factory within the client code, you could use dependency injection to pass the correct factory implementation into the client code.
This would make the code even more flexible and testable.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: A document generation system might have different types of document formats, such as PDF, Word, or HTML. A Factory Method can be used to create the appropriate document generator based on user input, allowing for flexible document creation without hardcoding the specific document format classes.
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 ILogger interface is implemented by the concrete classes FileLogger and ConsoleLogger. These classes define how the log message will be handled, either by writing to a file or outputting to the console.
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 implement the ILogger interface, defining how the messages are logged (either to a file or the console). FileLogger: public class FileLogger : ILogger
{
public void Log(string message) => Console.WriteLine($"Logging to file: {message}"); } ConsoleLogger: public class ConsoleLogger : ILogger
{
public void Log(string message) => Console.WriteLine($"Logging to console: {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.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The facade can be enhanced with better exception handling.
For example, if a component fails (e.g., DVD player is missing), the facade could display a user-friendly message or take an appropriate action.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: A Facade Pattern can be used in financial systems where complex operations like credit checks, account updates, and transaction processing are abstracted into a simplified process, allowing users to perform transactions without understanding the underlying complexities.
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 Facade makes the system more readable. If the subsystem's complexity changes, the facade can be updated without affecting the client code.
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 Facade Pattern enables the user to work with a single entry point (i.e., the HomeTheaterFacade), which internally delegates tasks to the complex subsystem classes. This makes the system much easier to use while hiding unnecessary complexity. Key Benefits of the Facade Pattern:
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 Facade class simplifies interactions with the subsystem by providing a unified interface for the user. It wraps the complex subsystem and provides higher-level methods that internally call the appropriate subsystem methods. public class HomeTheaterFacade
{
private readonly Amplifier _amplifier;
private readonly DVDPlayer _dvdPlayer;
public HomeTheaterFacade(Amplifier amplifier, DVDPlayer dvdPlayer) {
_amplifier = amplifier;
_dvdPlayer = dvdPlayer;
}
public void WatchMovie(string movie)
{ _amplifier.On(); _dvdPlayer.Play(movie); }
public void EndMovie()
{ _amplifier.Off(); }
}
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: Add functionality to dynamically change the properties of the decorated object.
For instance, you could add a feature to customize the size of the coffee (Small, Medium, Large) which would change both the cost and description.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: In text editors, the Decorator Pattern could be used for adding formatting options to text (bold, italic, underline) dynamically, without needing separate classes for each combination of formatting.
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 original SimpleCoffee class remains unchanged, which means you don’t need to touch existing code. The new functionality is added without altering or subclassing the core class.
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 CoffeeDecorator class is abstract, and all concrete decorators (MilkDecorator, SugarDecorator) extend this class. The decorator wraps the SimpleCoffee object (or other decorated objects) and enhances or alters its behavior, like adding extra cost or modifying the description.
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 SimpleCoffee class is the basic concrete implementation of the ICoffee interface. It represents a basic coffee order with a cost and description. public class SimpleCoffee : ICoffee
{
public double Cost() => 1.00;
public string Description() => "Simple Coffee";
}
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 file system to support additional metadata for each file or directory, such as size, creation date, and file type. This could be added as properties in the File and Directory classes.
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 Composite Pattern can also be used in organizational structures, where departments (composites) contain teams or employees (individuals), and both can be treated as "components" with a common interface for operations like calculating total salary or generating reports. Improvement Suggestions:
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 recursive structure makes it easy to manage hierarchical data. For example, when displaying the contents of a directory, you don’t need to worry about whether the child is a file or a subdirectory.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The File class represents the leaf in the tree structure. It is an individual component (a file) and implements the ShowInfo() method to display its details. public class File : IFileSystemComponent
{
private string _name;
public File(string name) => _name = name;
public void ShowInfo() => Console.WriteLine($"File: {_name}");
}
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 combine multiple commands into a composite command. For example, if the user performs a series of actions (like adding text, changing fonts, and changing colors), you can encapsulate all of those actions into a single composite command that can be undone in one step. Real-Time Use Case
The Command Pattern is often used in: Text Editors: For implementing undo/redo functionality and user interactions. Transaction Systems: Where each action can be encapsulated as a command, and the entire system can be undone or redone. GUI Frameworks: Buttons, sliders, and menu items can be mapped to commands, allowing for consistent handling of actions across the UI. Gaming Applications: Where player actions (e.g., moving, shooting) can be encapsulated as commands and undone when necessary. Visual Diagram: Here's a simple visual diagram to understand the Command Pattern: +-----------------+ +---------------------+ +------------------+ | TextEditor | ---> | AddTextCommand | ---> | Document | | (Invoker) | | (Concrete Command) | | (Receiver) | +-----------------+ +---------------------+ +------------------+ | | +-----------+ +-------------------+ | Undo | | AddText / RemoveText | +-----------+ +-------------------+ The Command Pattern provides a flexible and scalable way to handle requests in object-oriented systems, especially when you need to manage complex workflows, implement undo/redo functionality, or decouple senders from receivers. Composite Pattern: Real-Time Example - Building a File System Scenario: The Composite Pattern is used when you need to treat individual objects and compositions of objects uniformly. This is particularly useful when you have a hierarchical structure, like a file system, where files and directories can be treated in a similar manner. In a file system: Files are the individual objects (leaves). Directories are composite objects that can contain files or other directories (children). This pattern helps to simplify the management of hierarchical structures, making it easier to handle both individual items and collections of items in a unified way. Code Explanation:
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The Command Pattern makes it easy to add new commands without modifying existing code. For example, you can add new commands like RemoveTextCommand or ChangeFontCommand by simply implementing the ICommand interface.
Gang of Four Patterns Design Patterns in C# · GoF Patterns
Short answer: The TextEditor maintains a stack of executed commands. When the Undo() method is called, it pops the most recent command from the stack and calls its Undo() method, which reverts the action performed by the command (removes the last added text). Key Benefits of the Command Pattern:
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.