Video in progress

What is the Factory Method pattern, and how does it differ from a plain `new` call?

Factory Method moves object creation into a separate method: the caller doesn't know the concrete class, it only gets the interface. new ties the caller tightly to a concrete class.

📖 Answer

The place where you wrote new EmailSender() now always depends on EmailSender. If you need SMS tomorrow, you open that code and add an if. With a Factory the decision is gathered in one place:

public interface Sender { void send(String to, String text); }

public final class SenderFactory {
    public static Sender create(Channel channel) {
        return switch (channel) {
            case EMAIL -> new EmailSender();
            case SMS -> new SmsSender();
            case TELEGRAM -> new TelegramSender();
        };
    }
}

Sender s = SenderFactory.create(user.preferredChannel());
s.send(user.contact(), "Kodingiz: 4821");

The caller works only with Sender. If a new channel is added, the change happens only in the factory.

The "classic" Factory Method in the GoF book actually works through inheritance: the parent class has an abstract createSender() and the subclasses override it. The static variant above is often called a "simple factory", and in practice it's more common.