Interview: C# Design Patterns — Why and How

vishal gupta
3 min readNov 25, 2020

--

Creational Design Patterns

Creational patterns provide various object creation mechanisms

  1. Factory Method —

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Problem

Imagine that you’re creating a logistics management application. The first version of your app can only handle transportation by trucks, so the bulk of your code lives inside the Truck class.

After a while, your app becomes pretty popular. Each day you receive dozens of requests from sea transportation companies to incorporate sea logistics into the app.

Solution

The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method. Don’t worry: the objects are still created via the new operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as products.

For example, to add a new product type to the app, you’ll only need to create a new creator subclass and override the factory method in it.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.

Problem

Imagine that you’re creating a furniture shop simulator. Your code consists of classes that represent:

  1. A family of related products, say: Chair + Sofa + CoffeeTable.
  2. Several variants of this family. For example, products Chair + Sofa + CoffeeTable are available in these variants: Modern, Victorian, ArtDeco.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Builder is a creational design pattern, which allows constructing complex objects step by step.

Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.

Structural Design Patterns

Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.

Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

Behavioral Design Patterns

Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.

Chain of Responsibility is a behavioral design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

State is a behavioral design pattern that allows an object to change the behavior when its internal state changes.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

vishal gupta
vishal gupta

Written by vishal gupta

Software Architect, Author, Trainer

No responses yet

Write a response