Home  >  Article  >  What are the applicable scenarios for the factory method pattern?

What are the applicable scenarios for the factory method pattern?

藏色散人
藏色散人Original
2020-07-01 10:25:137427browse

The applicable scenarios of the factory method pattern include: 1. When a class does not know the class of the object it must create; 2. When a class wants its subclass to specify the object it creates. 3. When a class delegates the responsibility of creating objects to one of multiple helper subclasses, and you want to localize the information about which helper subclass is the delegate.

What are the applicable scenarios for the factory method pattern?

Factory Method Pattern

Define an interface for creating objects and let subclasses determine the instances Which class to use. Factory methods enable deferring instantiation of a class to its subclasses

Applicability

  • When a class does not know the objects it must create When a class

  • When a class wants its subclass to specify the object it creates

  • When the class will create When delegating an object's responsibilities to one of multiple helper subclasses, and you want to localize information about which helper subclass is the delegate

More related For knowledge, please visit PHP中文网!

The above is the detailed content of What are the applicable scenarios for the factory method pattern?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn