শনিবার, ১৬ নভেম্বর, ২০১৩

Factory Method Pattern



·         Gang of Four design patterns Gi creational group Gi GKwU pattern
·         Object creating Gi mgq exact class name specify bv K‡i object creating issue handle K‡i|

Intent:
·         Object/s creation Gi mgq complexity mg~n hide K‡i|
·         GB †ÿ‡Î object create Gi Rb¨ client normally †Kvb particular class specify  K‡i bv|
·         Client mvavibZ: interface/ abstract class Gi against G code Ki‡e Ges concrete type creating Gi responsibility Factory class Gi Kv‡Q †Q‡o w`‡e|
·         Factory class Gi GKwU static method _vK‡e hvnv abstract class or interface return Ki‡e|
·         G‡ÿ‡Î Client KwZcq information supply Ki‡e (Z‡e Zv memgq bq);
o   Factory Method supplied information mg~n Use K‡i ‡Kvb subclass create I  return Ki‡e Zv determine Ki‡e|
·         Factory Method use Gi gva¨‡g wKfv‡e dependent class mg~n created n‡e client code Zv m¤ú‡K© completely ignore _vK‡e|
·         Factory Method mvavibZ Dependency Inversion princwiple (DIP) ‡K follow K‡i|
·         Factory Method pattern use Gi Av‡iKwU myweav n‡jv objects creating code centralize Kiv hvq|
o   Objects generating G †Kvb mgm¨v n‡j dependent code G †Kvb affecting bv K‡i easily located and updated Kiv hvq| 


  • Dc‡iv³ Figure Factory Method pattern define K‡i| Figure Gi Object mg~‡ni  roles wb¤œiƒc-
·         IProduct GKwU Interface.
·         IProduct Interface ‡K  inherit K‡i `ywU  class ConcreteProductA I ConcreteProductB define Kiv n‡q‡Q|
·         Product Type ‡K Client information wnmv‡e supply K‡i i.e. Parameter wnmv‡e ProductA, ProductB supply K‡i|
·         Factory Method Parameter value Abyhvqx Instance create Ki‡e i.e. Parameter value hw` ProductA nq Z‡e ConcreteProductA class Gi Instance create Ki‡e Ab¨_vq ConcreteProductB Gi Instance create Ki‡e|

Practical Picture:
·         Toy Manufacturing company hvnviv Toy manufacturing Gi mgq Factory method pattern follow K‡i-
·         cÖ_‡g Zviv plastic powder ‰Zix K‡i|
·         Then  plastic molding powder process K‡i
·         Then processed powder ‡K desired shape Gi dice Gi g‡a¨ inject K‡i|
·         D`vnib ¯^iƒc D³ powder ‡K
o   Horse Gi dice Gi g‡a¨ inject Ki‡j horse n‡e
o   Car Gi dice Gi g‡a¨ inject Ki‡j car  n‡e
o   Doll Gi dice Gi g‡a¨ inject Ki‡j doll  n‡e
·         ‡Zgwb Factory method pattern G I GKwU Interface define Kiv nq (plastic molding powder)
·         Interface Øviv wewfbœ concrete class create Kiv nq (inject products e.g.- Horse, Car, Doll etc.)




Object Diagram for Factory Method using Injection Mold Example

Steps for Implement This Pattern
GKwU Factory method pattern wb‡¤§v³ Step mg~‡n m¤úbœ Kiv nq-

 

Step 1: Create an interface.

namespace PatternTest
{
    public interface IShippingCourier
    {
        string GenerateConsignmentLabelFor(Address address);
    }
}

 

Step 2: Create concrete classes implementing the interface.

namespace PatternTest
{
    public class DHL : IShippingCourier
    {
        public string GenerateConsignmentLabelFor(Address address)
        {
            return "DHL-XXXX-XXXX-XXXX";
        }
    }
}

namespace PatternTest
{
    public class RoyalMail : IShippingCourier
    {
        public string GenerateConsignmentLabelFor(Address address)
        {
            return "RMXXXX-XXXX-XXXX";
        }
    }
}

 

Step 3: Create a Factory to generate object of concrete class based on given information.

namespace PatternTest
{
    public static class UKShippingCourierFactory
    {
        public static IShippingCourier CreateShippingCourier(Order order)
        {
            if ((order.TotalCost > 100) || (order.WeightInKG > 5))
                return new DHL();
            else
                return new RoyalMail();
        }
    }
}

 

Step 4: Use the Factory to get object of concrete class by passing information such as type.

namespace PatternTest
{
    public class OrderService
    {
        public void Dispatch(Order order)
        {
            IShippingCourier shippingCourier =
            UKShippingCourierFactory.CreateShippingCourierFor(order);
            order.CourierTrackingId =
            shippingCourier.GenerateConsignmentLabelFor(order.DispatchAddress);
        }
    }
}

 

Step 5: Verify the output.



we: `ª:- GB example G Avgiv Address I Order bv‡g Av‡iv `ywU class use K‡iwQ Dnv‡`i definition wb¤œiƒc-

namespace PatternTest
{
    public class Address
    {
        public string CountryCode { get; set; }
    }
}

namespace PatternTest
{
    public class Order
    {
        public decimal TotalCost { get; set; }
        public decimal WeightInKG { get; set; }
        public string CourierTrackingId { get; set; }
        public Address DispatchAddress { get; set; }
    }
}
 

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন