Iterator Design Pattern

There are some exceptions in the Design Patterns world when design patterns are explained pretty well. Iterator is one of such patterns. However, there is no surprise in that, since this pattern is very easy to use.

So, what is the purpose of this pattern? Basically, this is used when you want to have some custom collection that might have custom way of sequencing your elements. When you have such custom container, then you can use iterator design pattern.

How to implement this pattern? Well, it is very easy. First of all, you provide the implementation for Iterator:

interface Iterator<T> {
    bool HasNext();
    T Next();

Then, you implement some container that is able to return the iterator of elements specified:

class Container<T> {
    static Iterator<T> GetIterator() {
       return // Iterator<T> is instantiated as a new object

And that is it. It is both easy and simple design pattern. I do not think there is something more to tell about it.

When would you use it directly? Probably, never, since all modern languages have interfaces in place that already have Iterator implemented somehow (e.g. C# has IEnumerable). And these interfaces are being implemented in case you actually need custom way of sequencing your elements in the container.

Adapter Design Pattern

This design pattern is pretty easy to use and understand, so this article is going to be short. First of all, why do we need it at all? Well, the main reason is the following – there are some classes that might implement useful algorithms, but we expect a bit different output out of them. For instance:
1) Legacy shape uses top left coordinates, and then height and width for rectangle. We would like to use top left and bottom left coordinates knowing we are talking about rectangle initially.
2) We have old customer authentication interface which expects different type of Customer class that we use.

In other words, there are lots of reasons why Adapter can and should be used. I will give a sketch of real-world example for the customer authentication. Let us say that old authentication API uses the following class for Customer:

public class APICustomer {
    public Email { get; set; }
    public Password { get; set; }

And we use the following class:

public class Customer {
    public UserName { get; set; }
    public Password { get; set; }

And now we need some adapter that adapts our Customer to APICustomer. We can implement it in the following way:

public class CustomerAdapter {
    public static APICustomer Adapt(Customer c) {
        // Given the circumstances that all users use as email domain.
        // In the Gmail users' database it might actually be so. However, in a more real-world adoption of APICustomer would be much more difficult or even impossible.
        // Still, this is not a bad example to illustrate use of it.
        return new APICustomer { Email = c.UserName + "", Password = c.Password };

And this is it. Basically, I have introduced a pretty simple class that converts object of Customer to APICustomer object. In other words, it means that Adapter does not have any sort of strict obligations regarding use of it, so Adapter might be almost everything that you use everyday in your programming life. Just think about it.

Object Pool Design Pattern

Object Pool Design Pattern is pretty simple one. However, even here tutorials might fail. Let us take a look into a couple of examples:


Both of these examples talk about performance like it should be the most important thing for the Object Pool while performance here is really important, but it is taken into consideration from a bit different angle. For instance, let us take a look into examples that Object Pool might actually be useful: Parallelization of jobs being performed. This is probably one and only class where Object Pool is actually useful. You take some tasks, you parallelize them across different threads, and you use Object Pool as a Factory or a provider for these objects. Creation and providing of these objects have nothing to do with performance. If you need something to do with performance in the object level, use Prototype for that, not an Object Pool.

In other words, it is correct to say that Object Pool might give some performance benefits. But it is retrieved through the pattern itself instead of through object creation. And finally, we should treat design patterns as an abstract way of designing our algorithms and systems. So, what actually does Object Pool give us? Well, it gives as a pretty centralized and easy to use API to get the objects we might want to get.

Another problem with the Object Pool is the example itself in the first tutorial. Why do we need always to compare design patterns with something which is completely unrelated to the Software Development? So we would be able to pretend that we understand what is this design pattern, yes? Why not trying to ACTUALLY understand and USE it instead of PRETEND? It would be much better for everyone.

So, let us take a look into the following code:

// First, we define the interface for generic Object Pool.
public interface IObjectPool<T>
    T Acquire();
    void Release(T node);
    void SetMaximumPoolSize(int size);
// Then, we define some Resource. Let us say it will be some node analysing website URL.
public interface IWebsiteURLAnalysisNode
    string Analyse(string url);
// And finally, we have the Object Pool itself:
public class AnalysisNodePool : IObjectPool<IWebsiteURLAnalysisNode>
    private static Semaphore _semaphore = new Semaphore(3, 3);
    private static AnalysisNodePool _instance = new AnalysisNodePool();
    private readonly Stack<IWebsiteURLAnalysisNode> _nodes = new Stack<IWebsiteURLAnalysisNode>();
    private readonly IWebsiteURLAnalysisNodeCreator _creator; // This would be initialized somehow, but for simplicity I am not doing this here.
    private AnalysisNodePool()
    // Would use some IoC Container (like Autofac), but for simplicity this is not bad. Just make sure not to include this into your real projects. Statics are not good if they can be replaced with non-statics.
    static AnalysisNodePool GetInstance()
        return _instance;
    public IWebsiteURLAnalysisNode Acquire()
        if (_nodes.Count == 0)
            return _creator.Create();
        return _nodes.Pop();
    public void Release(IWebsiteURLAnalysisNode node)
    public void SetMaximumPoolSize(int size)
        _semaphore = new Semaphore(size, size);

As you can see, example is pretty real, since semaphore is used (to ensure maximum number of resources would get used at the same time). So, this is the example of how to actually use Object Pool design pattern. But why use it for crawler? Well, the reason here is that we know pretty well that number of sockets can be limited. Theoretically, it can be unlimited, but this would give no real benefit, thus adding abstraction to limit number of them in an easy manner is pretty nice way of thinking here. Still, not sure if this is very necessary for simple website crawler projects.

In other words, if you know that you have limited amount of resources that need to be shared among some other classes, then Object Pool Design pattern can be used. The main reason to use Object Pool is to provide some API of acquiring objects representing limited amount of resources, not efficiency as some examples like to say. Efficiency gains might be here, but this is not the reason.

Factory Method Design Pattern

Actually, this is quite simple pattern. However, there are so many inappropriate examples (toy code, non-understandable and useless ones) and even more cryptic explanations that you can easily get confused and think what is the point in using this design pattern? It contains lots of awful examples where using Factory Method does not show its’ possible and practical purpose in no way. For example, let us take a look into a Book Factory!

Seriously, am I supposed to use this for some Books? Why? What is so special about the book that the book having different title, publisher or any other attribute that can be represented in a primitive data types, i.e. so special that I am supposed to create a Book Factory? Builder for books? Maybe, since there might be massive amount of attributes I would like to set comfortably and abstract setting them. Using Prototype for the Book itself? For the Book itself no way, but if the source is some remote data source, then Prototype can be useful, so Book can really implement ICloneable. But Factory Method for the Book? Maybe there are some ways, but I tend to simplify most difficult processes in most simplistic way possible, so it would be easier to manage them later. Thus, I think Book should not be used as an example for Factory Method. It is just too misleading from actual purpose of this awesome design pattern.

In order to find a proper example for usage of Factory Method, first we should take a couple of points why it is used. Some of the points mentioned in various resources are the following:

  1. Abstraction of new object creation. Yes, this is correct, and it is extremely useful in the IoC Containers where Factory Method is used in this way. However, IoC Containers for this article is a bit too complex example to get real understanding of Factory Method, thus I will leave it out here.
  2. Some say that way how objects are instantiated (created newly in memory or returned from some cache) should not matter for the client. Well, I am not so sure. Even if client will know that new object gets put somewhere and somehow in the memory, I think no client will know exactly the internals of that. If the “new” operator would be really harmful as some people tend to say, then no Object Oriented Programming Language would have operator “new”. As a client, I might want to write code in a way that does not restrict me in any ways. Or at least I would like to have intuitive enough API to write the code. In any case, it is wrong to assume what other developers want from me as an API developer. I can only assume the most standard needs: intuitive and easy to use API, understandable documentation, practical and useful examples (not some too primitive examples like “Hello, World”). However, I really agree that “new” should be avoided if it is possible to get the result without in a more elegant way. But still it should be used somewhere. We just should find proper place for it, not avoid it.
  3. Abstract logic that would come out of direct composition of two actual objects. And this can be quite a real reason to use Factory Method. For instance, you might have some main class for doing something, then you might have another class which also does something where another class has something that does something, and so on. Providing an interface as a proper closure for such type of logic really helps, since it becomes unit testable.
  4. Another thing that is suggested is to use some static class for the main factory. Actually, it is one of the antipatterns that you can think of. If possible, you should avoid this at any costs. What if I intend to use the factory in some model class that creates different objects based on the input provided? With static classes, I would be unable to write unit tests in isolated manner.
  5. And so many more explanations.

There are so many explanations due to the following reasons:

  1. Some people confuse Factory Method with Factory. They are different. Factory is used to create objects whose classes resolve to the same top level definition (parent/grandparent) based on some input parameters. Some even say it is not a pattern, and just a programming technique. Well, we have in this world lots of grammar purifiers. Probably, there is no exception in the developers’ world. So, what actually Factory is? Yes, it is a software development technique. And what a Design Pattern is? Also, it is some sort of software development technique. Thus, simple Factory is also a proper design pattern, since it is a common way to solve the problem specified. Factory is just not the part of GoG patterns. Maybe because it is so obvious and simple that any developer would think about it out of intuition. Still, I think Design Patterns are much more than GoG classics in today’s world.
  2. Some even confuse Factory Method with Abstract Factory. It is a rare case, but I have seen this in code working on the project a couple of years ago.
  3. And most probably just have not enough knowledge in Mathematics, verbal communication is not good enough regarding extremely important things. In other words, they do not actually understand what the Factory Method is, and just pretend to understand. However, they use it all the time. Which is strange, but it happens so.

Thus, I am going to explain this pattern once and for all not to make any confusions regarding it. It is pretty sad to see so many toy examples that are never going to help. So, what example for the Factory Method can be useful? Document factory method INTEGRATED into some document storage medium might be useful example where there is IDocument interface that gives as FileName (e.g. example.json) and its’ JSON representation. For instance, we might have some byte stream and we want to be able to store it in different document storage formats: DOC, DOCX, PDF, HTML, and so on. Currently, we have no idea how to convert that byte[] stream to PDF, DOC, DOCX, and so on. Maybe we do not even know what to expect in that byte[] stream. So, let us create a base class for storage medium based on these unknowns:

// Product participant.
public interface IDocument
    string AsJSON { get; }
    string FileName { get; }
// This has not to be a factory or something. It is just a Creator participant, and that is it.
public abstract class DocumentStorageBase
    // This is the factory method we are going to use.
    // This is the main point of Factory Method. You can not know exactly what type of document will get created here, but
    // you know that there will be some fixed interface (a.k.a. Product participant).
    protected abstract IDocument Create(byte[] content);
    // Just a simple storage method. We have the interface, the data in it, thus we know how to save it in some file.
    public void Store(byte[] content)
        var document = Create(content);
        var fileName = document.FileName;
        var json = document.AsJSON;
        // Store the document somewhere.
        // Not implementing here, since I want to show the use for pattern, not implementation. You can think of any implementation you like.

Then, let us say we know we will have to be able to store PDF documents. So, we introduce two new classes.

// Concrete Product participant.
public class PDFDocument : IDocument
    public string AsJSON { get; set; }
    public string FileName { get; set; }
// Concrete Creator Participant.
public class PDFDocumentStorage : DocumentStorageBase
    protected override IDocument Create(byte[] content)
        // Some difficult conversion logic that is able to get PDFDocument object from byte[].
        // This class would definitely include some dependency injection.
        return new PDFDocument();

And that is it. This is everything about Factory Method. We covered all of the participants, and I have shown a pretty useful example which can really get benefits from the Factory Method. As you can see, Factory Method is extremely powerful pattern, and can be used in such cases where you know just a resulting interface/base class (a.k.a. Product participant), and you want someone else to create Concrete Product participants (i.e. they are created by Concrete Creator participants). And Creator itself is just an abstract class exposing an API method to override Factory Method which is used to create Concrete Product participants.

To sum up everything, Factory Method can be defined in the following way:

  1. It is a Creational Pattern allowing us to defer responsibility of object creation to the child-level through parent-level abstract method definition.
  2. There are four participants: Product (base interface both Client and API developer agree on), Creator (base class which introduced Product creation Factory Method which is abstract and should be implemented by child classes), Concrete Creator (implements the Factory method), Concrete Product (instance of Product interface created by Concrete Creator).
  3. It is useful when you know nothing about concrete products that might be instantiated, but you have a pretty good idea of the interfaces, and you exactly know what needs to be done. In other words, you know WHAT, but you do not know HOW, yet. Or you just want to leave HOW part for later.

Finally, there are some pretty nice explanations regarding Factory Method, too. At least the following one is the single one I was able to find where at least explanation is nice: . Explanation is short, clear, and from this you can really get the idea HOW to use Factory Method. But not sure about WHY. Example is original maybe, but not very useful from the real-world perspective, since it does not show WHY Factory Method should be used. I can code all of the sandwiches without any of the Design Patterns, and I would not lose anything. However, I would lose massive amount of nerves, time, and lots of other stuff if I would try to code smart Website Crawler by trying to include all of the rules in a single class. Or to create God of Documents (a.k.a. God Object that is able to create and store PDF, DOC, DOCX, HTML, XML, and so many more document formats in a single class). By using Factory Method, I can concentrate on the things I know right now, and do not bother that I might not know right now something, since I will always be able to extend the class without violation of SOLID (especially Single Responsibility and Open/Closed principles), and do not worry too much of some unknowns. That is why Factory Method can be used. But for sandwiches, books, colors, shapes… These examples are inappropriate. No one codes different classes for books if title is different. And if one does, then software design in that project should be very messy.

Prototype Design Pattern

When learning design patterns, it is really important to see real-world examples to understand actual use for patterns instead of trying to decrypt learning resources and pretend understanding design pattern just because you have read some examples which would never be thought to be used in the real-world. This problem is extremely visible regarding Prototype Design Pattern. Let us look into the examples below:


What could possibly go wrong? Basically, everything with these sources. What are these guys modeling? Some color, some shape? Even the class for different color is created. Have you ever seen someone that creates a class for the color? How many colors are there? Well, modern displays are able to understand at least 16777215 colors or more. So, are you going to create 16777215 or more classes for each color, so you would be able to use prototype? Then, how many shapes are there. Well, there are circle, square, rectangle, triangle. Then, there are lots of 2D irregular shapes. Then, there are extremely massive amount of 3D shapes. Number of colors in the computers can be finite (but this number is still very huge…), but number of shapes is actually infinite.

So, my point is that these two tutorial about Prototype is inappropriate for people who want to learn how to use Prototype in the real-world. They show no real use for the prototype. The one good thing here from these tutorials is that at least Prototype’s goal is correctly defined – create duplicate objects from already existing object in order to save some performance. Yes, this is the correct approach. But do we really need saving performance in order to create some really simple classes? Through prototype, we could even lose performance, since Prototype adds more abstract layer on top of the simple class. Thus, it might even become an asymptotic problem for us if we are going to use just for simple cases like the examples in the examples mentioned.

So, what would be the real-world example where Prototype can actually save the day? Well, all of the calculations, data extractions which are performance-hungry are good candidates for Prototype abstraction. For instance, one of the real-world example can be Website Crawler where there is a class (let’s call it WebsitePage) that contains both URL and HTML Content. In order to get HTML content, it might take quite a long amount of time (up to 1-2 seconds which can be treated as a long amount of time if you need multiple of such classes), since you have to open some remote connection, read some data. After that, you want just to process the page, and be as much as network independent as possible when you already have the HTML page for the URL you want to have. So, here you can use the Prototype. But how?

In order to use Prototype, you first have to implement ICloneable interface (example below is given in C# dialect):

public interface ICloneable<T> {
    T clone();

Method clone() will just create the instance of class ICloneable using class’es already existing data. Let us see an example for the WebsitePage class:

public class WebsitePage : ICloneable<WebsitePage> {
    public Url { get; set; }
    public HtmlContent { get; set; }
    WebsitePage clone() {
        // Note. If you have pretty complex data class here, you can combine Prototype with Builder just right here.
        // But for simplicity, I am not going to do that here.
        return new WebsitePage
            Url = Url,
            HtmlContent = HtmlContent

So, where is the performance bottleneck solved? Nowhere. For now at least. Performance bottlenecks are solved in something that most tutorial creators call “Prototype Registry”. However, this naming is really confusing and completely not understandable. Why do we need some registry? We just want to be able to solve the context-related problem and that is why we would like to use the pattern. For Website Crawler case, it would be just a simple Repository being able to create WebsitePage based on URL (this URL can be treated as ID for our so-called “Prototype Registry”). If you like, you can call it like WebsitePagePrototypeRegistry. Just make sure that it is clear enough whose register/repository it is. So, let us implement it:

public class WebsitePageRepository {
    private readonly Dictionary<string, WebsitePage> _pages = new Dictionary<string, WebsitePage>();
    public WebsitePage GetPage(string url) {
        // We can even process url and use some finalized form.
        // E.g. we can assume that "" is the same as "".
        // In case we have URL already in cache, we just return existing object from cache, and use it.
        if (_pages.ContainsKey(url))
            return _pages[url].Clone();
        // This is the actual operation we want to encapsulate, and which can really be slow.
        // Operations such as color creation and other stuff mentioned in examples explaining Prototype are really worthless.
        using (var webClient = new WebClient())
            string htmlCode = webClient.DownloadString(url);
            var websitePage = new WebsitePage
                Url = url,
                HtmlContent = htmlCode
            _pages[url] = websitePage;
            return websitePage;

And that is it. We have real-world example showing usage of Prototype. As we can see, “Prototype Registry” is the class that is the most important, since it allows us to achieve actual goal we want to achieve. And we really see here real-world use of Prototype to solve real-world problem.

Generally, there are extremely many inappropriate Design Patterns tutorials with trivial and worthless examples that makes extremely confusing to learn Design Patterns. However, my goal is to fill in this gap, and to make sure that people wanting to learn Design Patterns would be able to learn them in a really efficient and non-confusing way through real-world examples.