Motivation for Simple Factory and Factory Method Pattern

后端 未结 6 891
猫巷女王i
猫巷女王i 2020-12-16 20:22

I know there are a lot of questions out there about differences of different factory patterns, but the answers are so different and confusing. The books that i read use uncl

6条回答
  •  旧巷少年郎
    2020-12-16 21:10

    Simple Factory:

    Definition:

    Creates objects without exposing the instantiation logic to the client. Refers to the newly created object through a common interface

    public interface PaymentMethod {
        public void makePayment();
    }
    
    class CreditCard implements PaymentMethod {
        public void makePayment() {
            System.out.println("Payment through credit card...");
        }
    }
    
    class NetBanking implements PaymentMethod {
        public void makePayment() {
            System.out.println("Payment through net banking...");
        }
    }
    
    public class PaymentMethodFactory {
        public static PaymentMethod getPaymentMethod(String method) {
            if ("creditcard".equalsIgnoreCase(method)) {
                return new CreditCard();
            } else if ("netbanking".equalsIgnoreCase(method)) {
                return new NetBanking();
            } else {
                throw new IllegalArgumentException("Payment method not supported!");
            }
        }
    }
    
    public class SimpleFactoryTest {
    
        public static void main(String[] args) {
            PaymentMethodFactory factory = new PaymentMethodFactory();
            PaymentMethod paymentMethod = factory.getPaymentMethod("creditcard");
            paymentMethod.makePayment();
        }
    
    }
    

    Factory Method:

    Definition:

    Defines an interface for creating objects, but let subclasses to decide which class to instantiate Refers the newly created object through a common interface.

    public interface PaymentMethod {
        public void makePayment();
    }
    
    class CreditCard implements PaymentMethod {
        public void makePayment() {
            System.out.println("Payment through credit card...");
        }
    }
    
    class NetBanking implements PaymentMethod {
        public void makePayment() {
            System.out.println("Payment through net banking...");
        }
    }
    public interface IPaymentMethodFactory {
        public PaymentMethod getPaymentMethod();
    }
    
    class CreditCardFactory implements IPaymentMethodFactory {
        public PaymentMethod getPaymentMethod() {
            return new CreditCard();
        }
    }
    
    class NetBankingFactory implements IPaymentMethodFactory {
        public PaymentMethod getPaymentMethod() {
            return new NetBanking();
        }
    }
    
    public class FactoryMethodTest {
    
        public static void main(String[] args) {
            IPaymentMethodFactory factory = new CreditCardFactory();
            PaymentMethod paymentMethod = factory.getPaymentMethod();
            paymentMethod.makePayment();
        }
    
    }
    

提交回复
热议问题