Pages: Prev 1 2 3 4 5 6 7 Next
– page 6 Skip to content

.NET Architecture & Development

The Zen and Art of Technical Leadership and Digesting Spaghetti Code

Archive

Author Archive

The Composite pattern is intended to allow you to compose tree structures to represent whole-part hierarchies so that clients can treat individual objects and compositions of objects uniformly.

image

When I hear "composite pattern", immediately XML pops up in my mind. A composite is an element and a leaf is an attribute.

The client creates the root of the tree and adds composites and leafs. Next an operation can be performed on the complete tree by calling the method on the root. Thereafter the operation is ‘automagically’ called recursively.

The key of this pattern is the abstract class Component, which is the parent of the composite object and leaf. It defines a property and an operation. It also has methods for adding and removing objects from the tree. These methods are not necessary, but – indeed – very convenient.

The composite is the concrete class which implements all methods of the abstract parent class, thus can contain another composite or leaf.

The leaf, on the other hand, is a crippled implementation of the abstract parent class. It cannot contain other parts, it is an endpoint.

In this example, a xml-structure is created and the method Operation is use to display itself. Therefore, Display() would be a better name. Maybe we should refactor it.

This post is part of a 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 by looking at a minimalistic example to reveal its structure and then look at more concrete and useful real-world C# code that implements the pattern.

The Chain of Responsibility is intended to promote loose coupling between the sender of a request and its receiver by giving more than one object an opportunity to handle the request. The receiving objects are chained and pass the request along the chain until one of the objects handles it.

image

What I like about this pattern is the chaining part. If the first handler cannot handle the request, the second one will. Or the third, fourth, or … And you only have to call the first handler of the chain.

You must be aware that if you want to use this pattern you should be very keen on exception handling, because the major drawback of this pattern is the possibility that the chain breaks.

And you probably heard this before:

A chain is only as strong as its weakest link

This also holds true for the chain of responsibility pattern. If one handler doesn’t do its work right the working of the following handlers are possibly compromised, which is especially important if the handler is one of the first components in the chain.

The client creates the chain by linking the handlers and does its request to the first handler in the chain, that’s it.


As is the case in most design patterns, there is an abstract class. The abstract class Handler defines SetSuccessor, which is responsible for the actual chaining.


The concrete class implements the actual handling of the request.

I also have included a real world example, a message handler which can handle different versions of requests. The first link in the chain handles version 1, the second version 2, and so forth…

Click here for a simplified

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 Template Design Pattern is used to set up the outline or skeleton of an algorithm, leaving the details to specific implementations later. This way, subclasses can override parts of the algorithm without changing its overall structure.

image

The template pattern is a relatively simple pattern. You can use it when you have some similar classes with only minor differences and it is possible to isolated the differences into one method while the shared similarities are provided by the base class. Using the template pattern the client only has to instantiate the concrete class and call the desired template method.


The abstract class contains a template method which calls an abstract operation which is defined in the base class and implemented in the concrete class.

The concrete class overrides the abstract method of the base class.

Thus, the client calls a template method of the base class which calls a method of a concrete class.

Click here for a simplified

This post is part of a 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 by looking at a minimalistic example to reveal its structure and then look at more concrete and useful real-world C# code that implements the pattern.

The Interpreter pattern is intended to provide you with a way to define representation of the grammar of a language whith an interpreter that uses the representation to interpret sentences in the language.

image

Some applications provide support for built-in scripting and macro languages so users can describe operations they can perform in the application. If this ability is what you want, then I am sorry to inform you: please use one of the many scripting engines available, for example CS-Script – The C# Script Engine.

The interpreter pattern is especially useful for parsing algorithmic expressions, but it is also very useful for interpretation of small strings containing foreign code. On the net you can find an abundance of examples that use the interpreter pattern for conversion of a Roman numeral to a decimal.

Nice as it is, but instead of ruminating the Roman example we will apply the interpreter pattern for conversion of binary code to decimal.

All you have to do in the client is to construct an interpretation tree, and later on to walk the tree with the provided context. The context contains all necessary information that is global to the interpreter.

In our case, the context contains a binary input and a decimal output. I know, these should be properties instead of global variables. So you should refactor them.

The base class AbstractExpression is the mother of all expressions. Maybe you should refactor it to an interface. In this example it contains one abstract method which receives the context.

As always the concrete class does all the work, conversion of binary code to decimal. It’s no rocket science, but nicely shows how this pattern works.

 

At this point you probably are thinking: Hey, where is the NonTerminalExpression? Honestly, I tried to use it in the example, but the example became a little bit to complex. So I purged it. On the other hand, you probably would not use it very often in real world situations, except for building a calculator.

The expression "5*(3+2)" contains the nonterminal expression "(3+2)". You could rewrite the expression like this:

If you want to try this yourself and incorporate a nonterminal expression, be my guest. I will wait right here for you. See you later, then.

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 Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme.

image

In normal usage, the client software would create a concrete implementation of the abstract factory and then use the generic interfaces to create the concrete objects that are part of the theme. The client does not know (or care) about which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from its general usage.

It sounds all a bit complex to you, don’t worry. In fact, it is really simple. Think about database access in general. There you have a whole bunch of database specific commands, for example SqlConnection, SqlCommand, etc. for SQL Server. For Oracle you have OracleConnection and OracleCommand. And surrise, for ODBC you have OdbcConnection and OdbcCommand. These all have the same parent, DbConnection and DbCommand, respectively.

This can be summarized as follows:

Base class SQL Server Oracle Odbc
DbConnection SqlConnection OracleConnection OdbcConnection
DbCommand SqlCommand OracleCommand OdbcCommand
DbDataReader SqlDataReader OracleDataReader OdbcDataReader

Mostly, I use a SQL Database. This corresponds with the following code:

Need I say more?

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 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
    Pages: Prev 1 2 3 4 5 6 7 Next