Builder design pattern explained
December 28, 2019
Image Fourier Transform
December 30, 2019
Show all

Design Pattern – Factory Pattern

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();
    }

}

https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

https://medium.com/@mrfksiv/python-design-patterns-03-the-factory-86cb351c68b0

https://medium.com/@hardikpatel_6314/design-patterns-in-python-factory-c728b88603eb

https://stackoverflow.com/questions/20848082/motivation-for-simple-factory-and-factory-method-pattern

https://medium.com/@hardikpatel_6314/design-patterns-in-python-factory-c728b88603eb

Factory Method vs. Factory Method design pattern – Software Engineering Stack Exchange
https://softwareengineering.stackexchange.com/questions/233166/factory-method-vs-factory-method-design-pattern

java – Design Patterns: Factory vs Factory method vs Abstract Factory – Stack Overflow
https://stackoverflow.com/questions/13029261/design-patterns-factory-vs-factory-method-vs-abstract-factory?answertab=votes#tab-top

What are the differences between Abstract Factory and Factory design patterns? – Stack Overflow
https://stackoverflow.com/questions/5739611/what-are-the-differences-between-abstract-factory-and-factory-design-patterns?answertab=votes#tab-top

What is the basic difference between the Factory and Abstract Factory Design Patterns? – Stack Overflow
https://stackoverflow.com/questions/1001767/what-is-the-basic-difference-between-the-factory-and-abstract-factory-design-pat?answertab=votes#tab-top

Amir Masoud Sefidian
Amir Masoud Sefidian
Data Scientist, Machine Learning Engineer, Researcher, Software Developer

Comments are closed.