Open Closed Principle confusion

In the case of some logic involving a class. Eg. There maybe an Account class and we calculate the interest differently depending on the type of account with if else. ( eg. if Regular calc this way, else Saving calc that way) . This is undesirable. With Open Closed Principle, we were taught to turn the original class ( Account ) into an interface, and then introduce derived classes which have their own versions of calcInterest . Now that calcInterest method is implemented in derived classes, we don't need to update existing code logic to accomodate for any new Account type everytime we introduce a new derived Account class. However, my question is that, when we create the derived class. Wouldn't there still be if else loop to determine which type of derived class we consturct, doesn't that go against open closed principle?
9 Replies
salt_pepper1765
salt_pepper17653mo ago
Open Closed Principle in SOLID
In this article, we will learn about the Open Closed Principle. The Open Closed Principle (OCP) in SOLID, coined by Robert C. Martin, asserts that software entities should be open for extension but closed for modification, fostering adaptability without altering existing logic.
salt_pepper1765
salt_pepper17653mo ago
I need to figure out which type of account to create, that would depend on AccountType, so there's still if else going on, everytime we introduce a new Account we still have to modify existing code to account for it.
exokem
exokem3mo ago
If the account created depends on the account type then each type should provide a way to create an appropriate account for itself You can apply the same principle as you did when introducing the account interface Assuming the account types available should also be flexible
salt_pepper1765
salt_pepper17653mo ago
@exokem Right now the account type is passed into the constructor, and we have if else loop to create the appropriate account. Not sure how I can create the account dynamically
Joschi
Joschi3mo ago
At some point in your code you will have to write out some kind of mapping to determine which derived Account to create under which circumstances. This could be centralised using a factory pattern. I guess you could use reflection or source generators to maybe build this theoretical factory instead of having to write it yourself. But is that payoff really worth the complexity?
salt_pepper1765
salt_pepper17653mo ago
Yes, you are right Joschi. I was thinking of using the factory pattern. Like you said using reflection is not really worth the complexity in my opnion. @Joschi When using the factory pattern. Should all the class properties be of { get; set; }, such that the factory class method can call upon either the base class or the derived class to create the appropriate object? Is there a way better way of handling it? I feel like the class properties should be immutable like { get; private set; }
Joschi
Joschi3mo ago
If immutability is what you want you could use {get; init;}. I never really used factories myself, but from the top of my head I would have assumed that the factory needs to have all the properties passed into it that are required for creation. Then you can instantiate all those private set properties from your constructor, or some static creation method.
salt_pepper1765
salt_pepper17653mo ago
Thanks. I considered making set public so I can use object initializer to create the classes. Upon some more thought, I can it's better to mark setter as private and create new instance by calling the constructor, as all the properties are required.
Joschi
Joschi3mo ago
If you just wanna use a public initializer you can use init and a backing field. That's then basically a public init; private set.
Want results from more Discord servers?
Add your server
More Posts
✅ How do I replace each character with a character that corresponds to the standard English layout?There is a string with the characters "ЙЦУКЕН". It corresponds to the "QWERTY" string of the standar✅ Using Options pattern in MAUII'm trying to squeeze some Options pattern config into my MAUI app, in `MauiProgram`, like this: ```✅ JWT questionI can generate a token fine. However when I use the token, it refuses to work. If anyone is kind enoPossible to F12 into C# source?VS 2022 latest version. When I F12 onto something like `.FirstOrDefault()` it just tells me a summaThread.sleep in a constructorSince you can't await in a constructor, I'm calling an async method and just sleeping the thread forEbay's new "RESTFUL" api's seem to have done away with seller JSON responsesIt looks like I can't just query my own ebay listings (auctions, buynows). They have some weird scheNpgsql.NpgsqlOperationInProgressException: A Command is Already in Progress in MassTransitHey peeps, I've been struggling with this issue for a few days now, I'm trying to publish outbox mesThe best-practice way to use a plausibility checkSo, I have the following class ```csharp namespace CleanLib.Lego.Parts; public class Color : EntitMaking a field eligible for GCrecently i've been working on a small game engine and i'm trying to call the GC to free my entities Object reference not set to an instance of an objectI'm making a simple UDP packet sender but for some reason I have this error when I attempt to start