lundi 19 juin 2023

Factory pattern can be used to create business objects?

I have read various articles on the factory pattern. However almost in all the examples the objects used are "flat" (beans, dtos,) ie. Dogs, Cars, etc. and that's good it helped me understand. But what about service objects, those that execute specific logic.

For example, let's say I want to create an app that supports credit card and debit card payments. Assuming that the logic is different for both cases, my idea is to create an interface CardPayment with both implementations: CreditCardPayemnt and DebitCardPayemnt.

The following would be to create the factory class that will return an object of type CardPayment (instantiated with one of the implementations based on some input parameter)

In this way, the processPayment() (method from CardPayment interface)could be executed without caring about the type of payment.

enter image description here

Would this be a valid use of the factory pattern? My concern is based on the fact that factory is a creational pattern and what I propose here is yes the creation of an object (CardPayment), but it is more behavior oriented.

Most of the examples I've seen consist of objects that have different attributes so it's useful to use factory to create one or the other, but what about objects that are different because their operation itself is different.

Aucun commentaire:

Enregistrer un commentaire