jeudi 24 octobre 2019

c# which desgin pattern is used

I got this simple task to figur out what design patterns are being used on each class and to explain pros and cons. I'm quite weak on this subject and wish someone would help me with this and eventually tell me where to read more about this subject and when to use each pattern or which one is mostly used out there in the real world?

Here is the code: Pattern A:

class PatternA
{
    private static PatternA _instance;
    private PatternA()
    {
    }

    public static PatternA GetInstance()
    {
        if (_instance == null)
            _instance = new PatternA();

        return _instance;
    }

    public void DoWork()
    {
        throw new NotImplementedException();
    }
}

Pattern B

 public abstract class SomeObject
{
}

public class ConcreteSomeObject : SomeObject
{
}

public abstract class PatternB
{
    public abstract SomeObject Create();
}

public class ConcretePatternB : PatternB
{
    public override SomeObject Create()
    {
        return new ConcreteSomeObject();
    }
}

Pattern C:

public abstract class Foo
{
    private readonly List<Bar> _list = new List<Bar>();

    public void Attach(Bar bar)
    {
        _list.Add(bar);
    }

    public void Detach(Bar bar)
    {
        _list.Remove(bar);
    }

    public void Notify()
    {
        foreach (var o in _list)
        {
            o.Update();
        }
    }
}

public abstract class Bar
{
    protected readonly Foo Foo;

    protected Bar(Foo foo)
    {
        Foo = foo;
    }

    public abstract void Update();
}

Aucun commentaire:

Enregistrer un commentaire