lundi 9 septembre 2019

why we need abstract factory when we already have concrete factories

this is the article

https://www.codeproject.com/Articles/328373/Understanding-and-Implementing-Abstract-Factory-Pa

I don't know why we need to have an abstract factory like IPhoneFactory, in the client's end, can't we do sth like

class PhoneTypeChecker
{
   ISmart sam;
   IDumb htc;
   //IPhoneFactory factory;    get rid of this abstract factory
   MANUFACTURERS manu;

   ...
   public void CheckProducts()
    {
        switch (manu)
        {
            case MANUFACTURERS.SAMSUNG:
                Console.WriteLine(new SamsungFactory().GetSmart().Name()); 
                Console.WriteLine(new SamsungFactory().GetDumb().Name());               
                break;
            case MANUFACTURERS.HTC:
                Console.WriteLine(new HTCFactory().GetSmart().Name());  
                Console.WriteLine(new HTCFactory().GetDumb().Name());               
                break;
            case MANUFACTURERS.NOKIA:
                Console.WriteLine(new NokiaFactory().GetSmart().Name()); 
                Console.WriteLine(new NokiaFactory().GetDumb().Name());                
                break;
        }

        Console.WriteLine(manu.ToString() + ":\nSmart Phone: " + 
        factory.GetSmart().Name() + "\nDumb Phone: " + factory.GetDumb().Name());
    }
}

the only thing I can think of to use IPhoneFactory is that we needs to get two different types(Smart and Dumb) from concrete factory classes, but it only save some keystrokes, isn't it?

Aucun commentaire:

Enregistrer un commentaire