Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    239
    Thanks
    18
    Thanked 34 Times in 33 Posts

    Factory pattern expert advise needed

    I am trying to understand the core purpose of Factory pattern. The purpose of abstract classes is undeniable. They allow developer be sure that any variation of given abstract class will at least have certain methods. What I do not understand, why I need intermediate factory class to create various instances of product classes instead of creating them right in client app?

    The simplest example: I have Billing abstract class with methods CreateForm, ProcessRequest, RegisterTransaction. Later I can create PayPalBilling and BitcoinBilling child classes from abstract Billing class. In client app I can use any of those classes (PayPalBilling or BitcoinBilling) and treat them as Billing class with certain guaranteed methods.

    Why do I need to use some intermediate class which will handle child classes? Did I miss something in Factory pattern? Thanks in advance

  • #2
    New Coder
    Join Date
    Aug 2013
    Posts
    38
    Thanks
    1
    Thanked 6 Times in 6 Posts
    An abstract factory let's its consumer create an arbitrary number of objects that share a common interface (or abstract base class) while being agnostic on the concrete type of the product.

    As you said your billing client can use PayPalBilling and BitcoinBilling and treat them as Billing but how does your billing client create those objects? Well, you could use a abstract factory for that:

    Code:
    interface IPaymentMethod
    {
        void ProcessTransaction();
    }
    
    class Paypal : IPaymentMethod
    {
        public void ProcessTransaction()
        {
            // use paypal api to process the transaction ...
        }
    }
    
    class Bitcoin : IPaymentMethod
    {
        public void ProcessTransaction()
        {
            // use bitcoin api to process the transaction ...
        }
    }
    
    interface IPaymentMethodFactory
    {
        IPaymentMethod Create();
    }
    
    class PaypalFactory : IPaymentMethodFactory
    {
        IPaymentMethod Create()
        {
            return new Paypal();
        }
    }
    
    class BitcoinFactory : IPaymentMethodFactory
    {
        IPaymentMethod Create()
        {
            return new Bitcoin();
        }
    }
    
    class PaymentSystem
    {
        private IPaymentMethodFactory PaymentMethodFactory;
    
        public BilllingSystem(IPaymentMethodFactory paymentMethodFactory)
        {
            this.PaymentMethodFactory = paymentMethodFactory;
        }
        
        public void BillCustomer()
        {
            IPaymentMethod paymentMethod = this.PaymentMethodFactory.Create();
            paymentMethod.ProcessTransaction();
        }
    }
    Last edited by hBGl; 01-14-2014 at 02:31 AM.

  • #3
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    239
    Thanks
    18
    Thanked 34 Times in 33 Posts
    Thanks for your reply. Your model is exactly how every book describes Factory method but why I cannot have simply:

    Code:
    class PaymentSystem
    {
        private IPaymentMethod PaymentMethod;
    
        public BilllingSystem(IPaymentMethod PaymentMethod)
        {
            this.PaymentMethod = PaymentMethod;
        }
        
        public void BillCustomer()
        {
            IPaymentMethod PaymentMethod = this.PaymentMethod.Create();
            paymentMethod.ProcessTransaction();
        }
    }
    What is the benefit of using intermediate factory class to create object?

  • #4
    New Coder
    Join Date
    Aug 2013
    Posts
    38
    Thanks
    1
    Thanked 6 Times in 6 Posts
    You totally could. The example doesn't really show the capabilities of a factory.

    Here are some benefits:

    1) Lazy evaluation
    If you pass three objects into a constructor then you pay the cost of creating them right there. However passing factories which typically have a small footprint delays the creation until the very moment the objects are needed. This is very useful if the creation is expensive.

    2) Create as many as you need
    A factory let's you create as many instances of a type as you want.

    3) Handle state associated with the creation of a object inside the factory
    For example you could cache stuff inside the factory:

    Code:
    class Paypal : IPaymentMethod
    {
        // ...
    
        public Paypal(PaypalConfig config)
        {
            // use config to set private fields
        }
        
        // ...
    }
    
    class PaypalFactory : IPaymentMethodFactory
    {
        private PaypalConfig Config = null;
        
        public IPaymentMethod Create()
        {
            if (this.Config == null)
            {
                // Read the config only once fron the database
                this.Config = Database.ReadPaypalConfig();
            }
            return new Paypal(this.Config);
        }
    }

  • Users who have thanked hBGl for this post:

    PHP6 (01-30-2014)

  • #5
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    239
    Thanks
    18
    Thanked 34 Times in 33 Posts
    Thanks for your explanation, now it makes sense. I noticed that patterns cannot be used right after you read about them. You need to know how they work and when Right time comes, you will shout: eureka! that's who this pattern should be used...

    p.s. do you know any good book with pattern usage examples?

  • #6
    New Coder
    Join Date
    Aug 2013
    Posts
    38
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Design Patterns: Elements of Reusable Object-Oriented Software is a classic book about oo design patterns. The examples are written in C++ and Smalltalk (mostly C++). I think of it more as a reference for looking up the essence of a pattern.

    Other than that I've read about design patterns on various websites that show up on the first two pages of your search engine of choice.

    If you are using a specific language you should also check if there already is an implementation for a design pattern. For example C# has the observer pattern actually integrated into the language although not in the strict oo way.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •