Proper OOP patterns practices with factory
Hey everyone! 👋
I'm working on a feature to decorate API responses with currency formats based on user config (from S3). I initially handled validation and decoration with simple singletons, since I'm actually writing code in Scala, I'm trying to follow fp practices and use singleton to create something where to place a function for logic. My idea to not overcomplicate things and just use plain functions, since this is a very simple feat imo.
A new team lead has recently joined my team. He asked me to refactor my pr using Factory class for creating a "currency plugin" instance. The proposed logic for this Factory is:
1. The Factory would be invoked/relevant only if the search query requests currency formats. (The lead's note said: "If search query is requesting currency formats -> None" which I interpret as the factory's specific logic below only applies if currency is requested, otherwise this specific factory/plugin isn't engaged for its primary purpose).
2. If currency format is requested, but the necessary user currency configuration is missing from S3 -> The Factory itself should throw a hard error. This error would occur during the initial request processing and validation phase.
3. If currency formats are requested and the configuration exists -> The Factory creates and returns the plugin instance.
My main question is about point 2: Is it a good practice for a Factory class to be responsible for input validation logic?
I'm confused, because I think factory should only be responsible for creating object instances which then in turn are supposed to do validation, transformations and so on... And I also think that factories should not be used when there's only two classes in the domain.
What are your thoughts on a Factory performing this type of validation leading to a hard stop? Is this a common or advisable pattern, or should such critical validation remain separate from the Factory's creation logic?
Thanks for any insights! 🙏
7 Replies
⌛
This post has been reserved for your question.
Hey @Lil Villa! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
follow fp practicesfp means factory patterny functionality programming or something else? a factory can create different objects depending on the input. If it cannot create any objects due to input and it make sense to throw an exception, that's a reasonable choice IMO now in general, you should probably do input validation as soon as it makes sense
functional programming, like no factories at all, just pass function or a lambda instead of whole object. Call on function, not on a class and so on
So in theory, with functional programming, you would return a result instance that is either an error or the created "object" (using a sum typ
But OPP and FP sometimes do things differently - you can somewhat mix things from both but saying "I adhere to both FP and OPP" makes limited sense
For example, in OPP, you would typically protect (mutable) state by accessor methods which define what can access the state and how while in FP, you would avoid having mutable state
But as I said, you can do some things from FP and some things from OOP (for example, you might want to solve some things with DOP (data-oriented programming))
But mainly, you should do what makes sense for your application
Hmm, makes sense. Thanks.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@249473164248219648>.