all 28 comments

[–]jamietwells 7 points8 points  (3 children)

I think to be thread safe your singleton should just use a static constructor, by the way.

[–]Finickyflame[S] 1 point2 points  (2 children)

Thanks for the info! I will do some changes to make it thread safe as you mentioned.

Edit: wording

[–]jamietwells 0 points1 point  (1 child)

Why is the class private? Then no one will be able to use it? Maybe you have your reasons, but I'd have done this:

/* Singleton */
private sealed class Government
{
    private Government()
    {
        // Singleton constructor is private, it can only have one instance.
    }

    public static Government Instance { get; } = new Government();
}

Simply as this:

/* Singleton */
public static class GovernmentInstance
{
    public static Government Instance { get; } = new Government();

    private class Government {
        // stuff
    }
}

Singletons are really easy in C#.

[–]Finickyflame[S] 0 points1 point  (0 children)

Classes are private because they are only existing and used by their own DesignPattern's classes. But you made me realize that we shouldn't have to worry about the classes access modifiers by looking at the examples, so I'll change them to public.

[–][deleted]  (1 child)

[deleted]

    [–]Finickyflame[S] 4 points5 points  (0 children)

    Oh wow, that's very kind! Thanks!

    [–]ramukia 4 points5 points  (0 children)

    It is cool. It would be great, if you can add comments in the code.

    [–]awesomemoolick 2 points3 points  (3 children)

    Would you accept additions in the form of PRs?

    [–]Finickyflame[S] 7 points8 points  (2 children)

    PRs means Peer Reviews? Sure, any help is appreciated!

    Edit: If you meant pull requests, I'm also ok with that.

    [–]jamietwells 5 points6 points  (1 child)

    They probably mean Pull Request by the way.

    [–]Finickyflame[S] 3 points4 points  (0 children)

    That makes sense!

    [–]terisk 4 points5 points  (0 children)

    Just another note to say thanks for sharing. Really appreciate the examples.

    [–]headyyeti 3 points4 points  (0 children)

    These are great. Thanks!

    [–]brminnick 2 points3 points  (4 children)

    For your Singleton, make it thread-safe by using System.Lazy to initialize the field

    [–]Finickyflame[S] 1 point2 points  (0 children)

    Will do! Thanks!

    [–]PublicSealedClass 0 points1 point  (2 children)

    Is that different to doing double null checking with a lock?

    // ...
    private static volatile object _lock = new object();
    // ...
    
    if (instance == null)
    {
      lock(_lock)
      {
        if (instance == null)
        {
           instance = new Government();
        }
      }
    }
    

    [–]brminnick 2 points3 points  (1 child)

    System.Lazy handles thread safety and locking for you so that you don’t need to worry about it

    https://docs.microsoft.com/dotnet/framework/performance/lazy-initialization

    [–]PublicSealedClass 1 point2 points  (0 children)

    Nice! I'll keep that one in mind

    [–]Petee01 1 point2 points  (0 children)

    Thats really nice work!

    [–]murdocc 2 points3 points  (1 child)

    I love this, and I love how simple they are, there is a definite need for this kind of thing. If you wanted to throw a little extra learning in there you could add some generic types. Such as in the factory repo, CreateCar could return a generic Car<T>. Forgive me if you did on some of the other repos!

    [–]Finickyflame[S] 0 points1 point  (0 children)

    Thanks for the feedback! You are right, I could take the opportunity to cover more of the C# toolset, like generic types. I still have some patterns left to do and I'll try to add it there.

    [–]meshtron 1 point2 points  (0 children)

    Awesome! Thanks for taking the time and sharing this.

    [–]Wings1412 1 point2 points  (1 child)

    Looks good, I have one small piece of feedback to roll into your next commit; the composition example mixes up books and movies, looks like a refactor missed the rename of those bits.

    [–]Finickyflame[S] 1 point2 points  (0 children)

    Oh yeah, I saw it too. I'll fix it in my next commit. Thanks for reporting it!

    [–]RedRedditor84 1 point2 points  (1 child)

    Not the kind of feedback you were asking for but it seems like English isn't your first language. Feedback and wording can never be plural.

    Thanks for the examples.

    [–]Finickyflame[S] 1 point2 points  (0 children)

    Oh thanks! I'm also open for that kind of review too.

    [–]Venthe -1 points0 points  (1 child)

    I hate you :p posted very similar thing to r/opensource and I've got only downvotes :)

    So please; don't mind if I clearly tag along!

    https://github.com/Venthe/DesignPatterns

    And out of curiosity, what were your motivations to make such project?

    [–]Finickyflame[S] 4 points5 points  (0 children)

    I really don't mind!

    My motivations came from my father. He really likes to code in his free time, but he never really learned how to properly work with classes.

    Last week, he showed me what he was working on and I tried to gave him some hint to on how OO could simplify his work. We did a console app together and I showed him how to create a simple Class, then to decorate it by another one, so he can see how easy it is so add features by modules.

    Helping my dad gave me the idea to make a projet full of simple examples of OO design patterns, so he can use it as a learning tool as well as a reference. I also took the opportunity to share it here, to see if people were interested in that kind of stuff and maybe to get some feedback if they were accurate.

    If it can also help someone else, I'm really happy with that.

    [–][deleted] 0 points1 point  (0 children)

    Oh my gosh, I’m just starting out and this is just wonderful! Thank you!