3. Polymorphism: Flexibility in Business Operations
- Puneet Malhotra
- Nov 14, 2024
- 4 min read
Quick Recap: 🔁
In the prior topic, we explored how a businessman implemented an Inheritance strategy to centralize control over his business. As the business expanded, the owner encountered mismanagement and a loss of control. Through Inheritance, the owner can establish a foundational process and manage operations from a single location.
Flexibility and Freedom via Polymorphism
Over time, the owner is observing numerous changes and challenges in his business and the implementation of processes.
🤔 There are numerous situations where partners or franchisees desire to have same business processes but in customized way to suit their preferences.
Implementing customization isn't difficult, but managing and controlling it is challenging.
So, customization has to be done. But how? 🤔 🤔
Owner is not sure about the solution but needs to ensure that changes happen in a way that allows him to manage and control.
Customizations Required By Restaurants
Restaurant A : Need Payment in Cash
Restaurant B: Need Payment in Cash and Card Both
Restaurant C: Need Loyalty system as well
Base Process is same , which is Payment. But they need it in different way. So keeping everything at one place is becoming problematic.
Decentralized Approach
He think to give Payment method separately to each Restaurant while keeping other important function at base process.
Payment_Base Only Cash 💰
Payment_Restaurant_2: Cash + Card 💰+💾
Payment_Restaurant_3: Cash + Card + Loyalty system
He has already done same previously using Inheritance. Its simple and sweet, so No confusion!
Here is the initial draft of his handwritten process.

Is it Really a good solution? 🤔
👉 Payment is a crucial aspect of his restaurant business, he also wants to maintain centralized control over it. So customization Payment as per individual business needs can not be a better idea.
Same functions with multiple names! Very confusing, time-consuming to understand and manage. Not only for him but also for his managers. So, this approach must be rejected.❌
Although, above solution work for franchise owners but not good for his business management.
Hence, he take a closer look to requirement and try to find out a best solution.
What all three restaurant need, different type of same function. So by this logic, function name should not be changed.💡
Only the inputs which are different while method name can remain same
Base : Payment method: Input: Cash
UrbanBakery Payment method: Input: Cash, Card
RealTasteBakery: Payment method: Input Cash, Card, Loyalty
There is only one Payment method with different types.
Business owner finds it simple, and customize input of each function as per their requirement while keeping the name of function same.
Base : paymentMethod: Input: Cash Output: remaining amount + receipt
UrbanBakery : paymentMethod: Input: Cash, Card Output: remaining amount + receipt
RealTasteBakery: paymentMethod: Input Cash, Card, Loyalty Output: remaining amount + receipt
One Function has many (Poly) forms.

👉 This strategy prevents the business process from becoming cumbersome. It is now easier for business owners to manage the payment process, as it is established that the franchise will adhere to the payment process outlined in the base process by customizing the input (cash/card/loyalty).
Franchise owner consent to update processes, and the business continues accordingly.
Benefit of Polymorphism to Business
How can business management gain advantages by incorporating a name into the core process and encouraging partners to use these names? 🤔
Benefits of having Base Process
Base process is like Index page of book which quickly guide about content or flow of any book. By seeing the Index one can tell about the Chapters or Topic a book has.
Quick Visible: Base process can easily tell what are crucial process in use.
Save Time: No need to check individual partner process.
Centralize Control: Update or addition or removal to be happened only from base.
Efficient Management: Managing large number of partners efficiently needs process small, simple, easy and centralized.
By doing so, he has better picture of this business.
Monitoring: He can see what are the functions and methods are customized at process level.
Flexibility: Process are not strictly bound to base process.
Customized Business: While keeping function name same at base process, franchise can demand for customization.
Now there are two types of functions
To be executed as-is, maintaining input according to the needs of each specific process.
May be customized and specified according to the particular process.
I believe you have understood the gist of polymorphism in nonTech way. Now, lets see how we can translate this functional process into code.
Functional to Code
Let's explore how we can convert our business or functional knowledge into code.
public virtual class BaseRestaurant {
// Function Overloading: Payment Method
public void processPayment(String method) {
System.debug('Base Payment: Processing payment using ' + method);
}
public void processPayment(String method, String cardType) {
System.debug('Base Payment: Processing payment using ' + method + ' with card type ' + cardType);
}
public void processPayment(String method, String cardType, Boolean loyaltyApplied) {
System.debug('Base Payment: Processing payment using ' + method + ', card type ' + cardType + ', loyalty applied: ' + loyaltyApplied);
}
}
public class FranchiseA extends BaseRestaurant {
String method = 'Card';
String cardType = 'Visa';
public void PaymentProcess() {
processPayment(method, cardType);
}
}
public class FranchiseB extends BaseRestaurant {
String method = 'Card';
String cardType = 'Visa';
Boolean loyaltyApplied = true; // or false based on test
public void PaymentProcess() {
processPayment(method, cardType, loyaltyApplied);
}
}
Thought-Provoking
How Polymorphism got discovered:-
Problems arrived first:
Freedom , Flexibility and Customization
Difficult control and management
Risk to business due to freedom demanded by partners
Solution identified:
Control from single location or base process
Freedom and Flexibility
Design :
Indexing: Keep names of required function ,which can impact business value, in base process
Allow partners to override or overload to meet their need.
Upcoming ➡️ Abstract Class: Agreement for Surety
Comments