質問

I am a dirty c# programmer and today I am going through a senior developer's code, I realized they made every "thing" an object "a class" and followed patterns like "abstract factory" and created interfaces, abstract base classes etc I am finding it bit hard to get it all in my mind to understand the code, and reasons behind relationship of abstracts and interfaces + factory. I looked at videos and understand them individually but all together when I am trying to understand them in this scenario, it winds me up.

Back to question (I changed objects names because of privacy)

Here is the part of class I can't understand as what pattern or principals has been applied to it,

internal sealed class Mango : GenericMango, IMango
    {
        public static readonly IMangoFactory<IMango> Factory;

        static Mango()
        { // this line below specially i don't understand
            Factory = new MangoFactory<IMango>((aBC, disposeMango) => new Mango(aBC, disposeMango));
        }

        private Mango(string aBC, bool disposeMango)
            : base(aBC, disposeMango)
        {
        }

        // more methods and properties in here etc..
役に立ちましたか?

解決

Hard to help you, as I don't understand what you don't understand.

Of course, I don't know why the original developer went to this trouble and yet didn't go to the trouble of using dependency injection.

Zooming in specifically on your problem line:

Factory = new MangoFactory<IMango>((aBC, eatableMango) => new Mango(aBC, eatableMango));

I'm going to assume that you know what an anonymous delegate is, and what a lambda expression is. If you aren't familiar with these, then Igor is your man for the answer.

This is basically the "lambda expression as factory method" subspecies of singleton factory pattern.

I'm also assuming that somewhere in MangoFactory there is a Func<string,bool,IMango> member. This is the function used by the MangoFactory to generate new Mangos.

Ultimately, the pattern being followed is one where (likely) the following line is what it used to instantiate a Mango:

var theMango = Mango.Factory.CreateMango("foo",true);

And the goal was to hide the details of Mango construction.

他のヒント

Factory = new MangoFactory<IMango>((aBC, eatableMango) => new Mango(aBC, eatableMango));

It is using of lambda expression, syntax sugar for delegate

See the article http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top