Fortsätt till huvudinnehåll

Inlägg

Visar inlägg från april, 2017

Design Patterns Overview - Observer

The observer pattern is a behavioral pattern that lets several objects, observers, subscribe to state changes at another object, the Subject. As with the Iterator, the .NET framework contains interfaces that are recommended when implementing the design pattern. These are named IObservable and IObserver. The subject should implement the IObservable interface while the observers implement IObserver. Of course, you do not absolutely need to use these interfaces in order to implement the observer pattern but it is good to know that they exist.

Design Patters Overview - Iterator

The iterator design pattern is an integrated part in many languages and frameworks. In C# you can implement the IEnumerable interface (defining the GetEnumerator method) to make a class iterable. Using the yield keyword makes the method return an iterator. This is so simple in the .NET framework that I did not bother to create a UML diagram this time. Check the code in my GitHub repository  for an example on how to implement IEnumerable.

Design Patterns Overview - Factory

No software design patterns overview can be complete without describing the Factory pattern. I would say this is one of the most commonly used patterns. Let's get right to the UML diagram. As can be seen an interface, Creator, is defined for creating a Product, but it is the ConcreteCreator that decides which subclass of Product to instantiate. From the client you instantiate the ConcreteCreator and use that to create the products. It is the resonsibility of the ConcreteCreator to see to it that the product is created correctly. This adds a nice level of abstraction since any changes to how the ConcreteProduct is created only needs to be reflected in the ConcreteCreator. The users of the product does not need to change their implementation.

Design Patterns Overview - Decorator

Ok, time for a structural pattern. I really like the Decorator pattern idea. Basically you create an abstract base class, Component. This you subclass to create concrete components, AND, to create Decorators. The decorators add functionality to the concrete components, and since they all subclass the abstract components you can add several decorators, add just the functionality you need. As with the previous patterns I have added example code in my  repository on GitHub .

Design Patterns Overview - Builder

The Builder Pattern is another creational pattern. It is used for creating different setups of complex aggregated objects. This sounds a lot more complex than what it really is. One easy to understand parable I like is when you go to a fast food restaurant and order a hamburger meal. In order to create the meal, different parts need to be combined. There might be 5-10 different burgers, fries or salad, different soft drinks etc. Ok, so create (build) a meal, start off by creating a new HamburgerMealBuilder, then using that you define the type of burger, fries or salad, Coke or Fanta, and so on until you have the complete meal. There you have it, a perfect use of the builder pattern. To understand the diagram better, take a look in my repository on  github . And by the way, do not use the C# code from Wikipedia, it is incorrect (the [NotNull] attribute does not exist any more, and far worse, the CarBuilder does not Build new cars, it only hands of references to the same single o

Design Patterns Overview - Prototype

Second pattern out is the Prototype pattern. This is a simple creational pattern that you can use when you want to create an object that you later on wish to create many copies, clones, of. The clones will get the same properties as the prototype. In my sample code  on github  I use the  MemberwiseClone  from the .NET framework. Take note that this creates a shallow copy of the object. Any references that the object holds will not be cloned, hence both the original and the clone will reference the same object. If you do need a deep copy, there is information available on the  MemberwiseClone  page on different options to achieve this.

Design Patterns Overview - Abstract Factory

I am currently involved in designing a new feature for a product at work. When discussing with one of the architects I realized that I need to refresh my memory on Software Design Patterns. First out is 'Abstract Factory'. I created a UML diagram using PlantUML. The name of the interfaces and classes are heavily inspired by the information available on Wikipedia. Sample code (C#) that demonstrates this, and other patterns, are available in my repository on github . The source for the PlantUML diagrams are included.