Suggest an editImprove this articleRefine the answer for “How is Factory Method related to the open/closed principle (OCP)?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Factory Method** directly supports the open/closed principle (OCP) because it lets you extend the system's behavior without changing existing code: a new product type is added through a new subclass rather than by editing the base code. **Key point:** the base code that uses `createProduct()` stays unchanged when a new product type appears.Shown above the full answer for quick recall.Answer (EN)Image**Factory Method** directly supports the **open/closed principle (OCP)** because it lets you **extend the system's behavior without changing existing code**. --- ### 1. **The essence of OCP** The principle states: > *Classes should be open for extension but closed for modification.* > In other words, when adding new behavior we should **add code**, not **change what already exists**. --- ### 2. **How it works in Factory Method** Instead of hard-coding in the client code which object to create with `new`, we use the abstract method `createProduct()`. If a new product type appears, we simply: - create a **new subclass**, - override the factory method in it. ```java abstract class Creator { abstract Product createProduct(); } class ConcreteCreatorA extends Creator { Product createProduct() { return new ProductA(); } } class ConcreteCreatorB extends Creator { Product createProduct() { return new ProductB(); } } ``` At the same time, the base code that uses `createProduct()` **stays unchanged**. --- ### 3. **Example** If you add a new type of transport (for example, `Plane`), you do not need to change the `Logistics` class - it is enough to create `AirLogistics` and override `createTransport()`. --- ### 4. **Conclusion** Factory Method lets you add new object types **without changing the existing logic**, which makes the system **flexible and extensible**, fully in line with the OCP.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.