2

I know about the Gang of Four and some of their patterns such as the Façade, the Factory, the Singleton and some others.

Besides, it happened that I see multiple patterns which I don't know of, neither for what are they good, or their purpose.

I would like to have your cues about the design patterns that you know. It would be much appreciated for you to provide a code sample explaning its use, and when it would be accurate to use it over another pattern.

Please, only one pattern per answer, and one example per pattern.

See if the pattern you would like to share is already discussed. If not, please feel free to share with the community! =)

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
  • To any one who can edit my post, please make it Community Wiki. I don't seem to have the checkbox to make it so though I already have made other questions community wiki. – Will Marcouiller Dec 08 '10 at 21:12
  • 2
    Wouldn't it be better to have one question per design pattern? That way the best example could be voted to the top and/or accepted. – Thomas Langston Dec 08 '10 at 21:19
  • @Thomas Langston: Absolutely! And although this would be very, I mean it, very interesting, I do not know all the design patterns, so I wish to be introduced to them first. Then I shall consider your proposition as well afterwards to get a better idea, I think. Is this an acceptable approach that makes sense to you? Thanks for your comment! I had not thought of it that way. =) – Will Marcouiller Dec 08 '10 at 21:23
  • 1
    http://en.wikipedia.org/wiki/Design_pattern_(computer_science) can get you introduced to design patterns. You can learn more from the resources linked at http://stackoverflow.com/questions/244706/learning-implementing-design-patterns-for-newbies. – Thomas Langston Dec 08 '10 at 21:28
  • Thanks for these important links which, I must admit, will be pretty useful. Despite, though I know about the `Gang of Four` and other design-patterns related websites and resources, I can't get to understand them. One in while comes this scenario when you better understand when a colleague explains it in his own words or the like. I know what D&P are, but for instance, consider @JB King's answer, I didn't know about it. – Will Marcouiller Dec 08 '10 at 21:36

4 Answers4

2

Adapter pattern

Wikipedia link

A real example of this pattern: C# ADO.NET DataAdapter

Both the first and last link have code samples for those that want them.

JB King
  • 11,860
  • 4
  • 38
  • 49
2

Singleton Pattern

Definition: If a system only needs one instance of a class, and that instance needs to be accessible in many different parts of a system, you control both instantiation and access by making that class a singleton.

Example: A concrete example would be the ISessionFactory API while using NHibernate. Because of the load in memory the ISessionFactory costs to instantiate, due that it involves all of the object-relation mappings and database connection to be loaded in memory all at once, it is advisable to make only one instance, and create the ISession API instances using this unique instance of the ISessionFactory API, that is, the Singleton Pattern.

using NHibernate;

internal class NhConnectionFactory {
    private static readonly ISessionFactory _sessionFactory = config.BuildSessionFactory();

    internal NhSystemFactory() {
    }

    internal ISession OpenSession(string connectionString) {
        return _sessionFactory.OpenSession();
    }

    // Other methods here...
}

public static class NhSystemFacade {
    private static readonly NhConnectionFactory _nhConnectionFactory = new NhConnectionFactory();

    public static string DefaultConnectionString {
        get {
            return Properties.Settings.Default.DefaultConnectionString;
        }
    }

    public static ISession OpenSession() {
        return _nhConnectionFactory.OpenSession(DefaultConnectionString);
    }
}

Links:

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
1

Lazy Initialisation

Useful if some objects may not be used during the running of an application. Thus, saving memory if not required as the object is only created when needed.

Winston
  • 500
  • 3
  • 3
1

State Pattern - http://en.wikipedia.org/wiki/State_pattern

Sujay Ghosh
  • 2,828
  • 8
  • 30
  • 47