Introduction

Design Patterns were created by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, known as the "Gang of Four" or simply "GoF". They are standard solutions to common problems in software design. Instead of focusing on how individual components work, design patterns take a systematic approach, which focuses on the patterns of interaction. Design patterns describe abstract systems of interaction between classes, objects, and communication flow.

Background

This program is a demonstration of the Model-View-Controller (MVC) design pattern, allowing the separation of the user interface, the data model (temperature), and the code which is used to connect the two. The program was translated to C# from Joseph Bergin's Building Graphical User Interfaces with the MVC Pattern.

Observer Pattern

The basic pattern used is the Observer pattern, which is implemented as a combination of a Delegate and an Event. This is the best way for updating an object's state on all observers.

Strategy Pattern

Being required to present both Fahrenheit degrees and Celsius degrees in the same application, thus making a form for each, is just a waste of time. Applying a Strategy design pattern to factor out the small changes is best suited. The Strategy is implemented as an abstract class, and both CelsiusStrategy and FarenheitStrategy implement it:

Decorator Pattern

Decorators are used to provide additional functionality to an object of some kind. The key to a decorator is that a decorator "wraps" the object decorated and looks to a client exactly the same as the object wrapped. This means that the decorator implements the same interface as the object it decorates.

You can think of a decorator as a shell around the object decorated. Any message that a client sends to the object is caught by the decorator instead. The decorator may apply some action and then pass the message it received on to the decorated object. That object probably returns a value (to the decorator) which may again apply an action to that result, finally sending the (perhaps modified) result to the original client. To the client, the decorator is invisible. It just sent a message and got a result. However, the decorator has two chances to enhance the result returned.

In this program, the Decorator allows setting minimum and maximum temperatures:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

What's in a name? IMHO it doesn't matter how things look like as long as it's working for you and flexible in the context you have defined. Patterns are there to help you implement things, if you have to force your code in such a way that it fits a certain pattern there's something wrong. There's also a quite a bit of overlap in the different patterns, I tend to forget what a particular pattern is but I do remember how to play with various approaches (i.e. classes, interfaces and all the C# constructs).