Look at the function below and say if it is polymorphic or not.
const sayHiByEmail = (user) => {
const sender = new EmailSender();
// Sending an email to a user
sender.send(user.getEmail(), 'hi!');
};
On the one hand, yes, the user is passed from outside, and we can substitute it by passing an object of another class there. On the other hand, the EmailSender class is explicitly used inside the function, and you can't replace it without rewriting the code itself.
This code demonstrates a simple but important idea. Subtype polymorphism is possible when an object is passed into a function from outside, rather than being constructed directly inside it.
Honestly, you can also create an object inside a function, but in that case, the class name must be created (or obtained) dynamically. We will look at this technique later, in the lesson about metaprogramming.
Another example with a catch. Is there polymorphism in the code below?
const sayHi = (user) => {
if (user instanceof User) {
console.log(`Hello, ${user}!`);
} else if (user instanceof Guest) {
console.log('Hello, guest!');
} else {
console.log('Who are you?');
}
};
In this example, everything seems to be fine, the object is passed from outside, but there is one snag. Inside the function, the type is explicitly checked, which means that the behavior is not determined by the type, the function itself decides how to behave. Moreover, the function is hardwired to the types that are defined within it and will have to be rewritten if they are changed. And as a result, there is no polymorphism of subtypes.
Type checking sometimes occurs and can be used to keep the code simple, but more often, it's a sign of poor design. One might say that such code doesn't correspond to OOP in its modern sense.
To solve the problem above, there are several approaches:
user.sayHi()
. You have to be careful with this approach because it's easy to end up with god object. Generally, it's better to take a different approach.We need to add a new interface in the form of the isUser
and isGuest
methods.
const sayHi = (user) => {
if (user.isUser()) {
console.log(`Hello, ${user}!`);
} else if (user.isGuest()) {
console.log('Hello, guest!');
} else {
console.log('Who you are?');
}
};
Although there's no less code, it's still subtype polymorphism. The code is bound to methods, not types. Changing the class structure will not affect this function if the logic itself remains the same.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.