Pages: Prev 1 2 3
– page 3 Skip to content

.NET Architecture & Development

The Zen and Art of Technical Leadership and Digesting Spaghetti Code

Archive

Category: Design Patterns

The Decorator pattern is intended to give you a way to extend the behavior of an object or to dynamically compose an object’s behavior at runtime.

image

The Decorator Pattern is used for adding additional functionality to a particular object as opposed to a class of objects. It is easy to add functionality to an entire class of objects by subclassing an object, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.

Decorators provide a flexible alternative to subclassing for extending functionality.

Code examples (click to enlarge/collapse):

  • This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.

    The Command pattern is intended to encapsulate a request as an object. A Command object encapsulates an action and its parameters. It is useful for implementing transactional behavior or a simple workflow.

    image

    Code examples (click to enlarge/collapse):

    This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.

    1 Comment Read more

    C# Design Patterns – Adapter Pattern

    Feb 6

    The Adapter pattern is intended to provide a way to use an object whose interface is different from the on expected by the client, without having to modify either.

    image

    There are two ways to create an adapter: by inheritance or by object composition. In the first case, which generally is considered best practice, we derive a class from the adaptee and add the expected methods to match the desired interface. In the other case, we include the adaptee inside the adapter and create methods to to translate the calls within the adapter.

    Code examples (click to enlarge/collapse):

    The Adapter pattern thus is really simple, so both examples are pretty straightforward. The examples above use object composition, since object composition is more often used than by inheritance. Any idea why?

    This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.

    Leave a Comment Read more

    C# Design Patterns – Factory Method Pattern

    Feb 2

    The factory method pattern  deals with the problem of creating objects without specifying the exact class of object that will be created.

    image

    The factory method design pattern handles this problem by defining a separate method for creating the objects, whose subclasses can then override to specify the derived type  that will be created.

    More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.

    Code examples (click to enlarge/collapse):

    The real world example is my favourite example using a Namer factory class. Depending on how you provide your full name (First-Name-First or Last-Name-First) your first name and last name are extracted from your full name. Using this pattern you can easily add other name patterns, for example how to handle middle names.

    With this foundation you should be able to successfully implement the Factory Method Pattern.

    This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.

    2 Comments Read more

    C# Design Patterns – Proxy Pattern

    Jan 30

    The Proxy pattern is intended to provide you with a simple placeholder that takes the place of a more complex object  that may be invoked later. It  is a kind of "wrapper" that delegates to the real object.

    image

    The Proxy pattern is especially useful for situations where object creation is a time consuming process, and you do not want to instantiate such an object unless and until it is actually requested by the client. This is also known as the "lazy loading" principle.

    Code examples (click to enlarge/collapse):


    In the simplified real world example you have two related entities Employee and Manager. Imagine both entities should be retrieved from the database. Image also that the manager has a manager, which has a manager, which has a … Before you know it, you have retreived the whole company from the database. That is not what you want, you only want to access some of the attributes of the employee and not the manager. If you want to access the employee’s manager, you have to load it explicitly.

    This post is part of my series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern and look at simplified real-world C# code that implements the pattern.

    Leave a Comment Read more

    C# Design Patterns – Overview of GoF Patterns

    Jan 30

    Anyone serious about learning object-oriented design and development should study design patterns. In fact, if you didn’t use design patterns before in your software designs or in your code, you should study them right here and right now! And start using them, of course. Use them a lot.

    The problem is that you can find information about design patterns almost everywhere on the internet. Examples can be found in almost any language, including Java and C#. Instead of looking everywhere, from now on you can find all the information here. To be honest: this is a memory dump.

    In short, design patterns describe solutions to common object oriented design problems. The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. You can call them the mother of all patterns.

    The GoF patterns are generally categorized in three groups: Creational, Structural, and Behavioral Patterns. Other categorizations are possible also, but for now we stick to the original categorization.

    Creational Patterns

    Structural Patterns

    Behavioral Patterns

    These are the classic pattern(s). If you want to acquire deep understanding of  design patterns,  you not only have to study design patterns thoroughly but you also have to implement them one-by-one in your favorite coding language.

    22 Comments Read more
    Pages: Prev 1 2 3