20241112 ◎

My willpower to endure hunger for weight loss is deteriorating as I get older. Sad.

Simple Factory Pattern

Factory Comparision - refactoring.guru

The simple factory pattern describes a class that has one creation method with a large conditional that based on method parameters chooses which product class to instantiate and then return.

People usually confuse simple factories with a general factories or with one of the creational design patterns. In most cases, a simple factory is an intermetiate step of introducing Factory Method or Abstract Factory patterns.

A simple factory is usually represented by a single method in a single class. Over time, this method might become too big, so you may decide to extract parts of the method to subclasses. Once you do it several times, you migth discover that the whole thing turned into the class factory method pattern.

So basically, it is a class with a method that instantiates an object depending on the given parameter.

class PaymentFactory:
    @staticmethod
    def get_strategy(provider: str, **kwargs) -> PaymentStrategy:
        match provider:
            case "paypal":
                return PayPalPayment(**kwargs)
            case "credit_card":
                return CreditCardPayment(**kwargs)
            case _:
                raise ValueError(f"Unknown provider: {provider}")

Strategy + Factory Pattern

Strategy and Factory Pattern, The Best Software Design Pattern Combo (Jono Williams on YouTube)

This is super helpful to understand the combination, clear and concise.

Design Patterns: The Strategy and Factory Patterns - DZone

We addressed this by creating a factory to encapsulate the logic concerned with selecting appropriate strategies for various conditions.

It seems that using factory patterns to extract and encapsulate strategy selection is an useful pattern.

The code I listed above removes the need to specify a strategy by the client code, listed here 20241110.


160.8 lb :cry:

Rice 800 Pancake & Sausage 750 Protein shake 300 Oatmeal 150

Total 2000 kcal


MUST:

TODO:


index 20241111 20241113