Interpreting Design Patterns----Factory method patterns (FactoryMethod pattern)

Source: Internet
Author: User

Overview:

Factory method mode is the most widely used design pattern, after all, he is responsible for the creation of a series of objects, and object creation is the most tedious behavior in object-oriented programming. Gof in design mode, "Factory method mode causes the instantiation of a class to be deferred to a subclass." "Exactly, the Factory method pattern is the responsibility to create an object instance, transfer it to a factory class, and use abstract principles to delay the instantiation behavior to a specific factory class."

Intention:

Defines an interface for creating objects, allowing subclasses to decide which class to instantiate. The Factory method pattern delays the instantiation of a class to a subclass.

UML diagram:

A common example:

Animals--Cats, dogs, cows, sheep, pigs ... People know that animals are all shaped, they have their own different appearance (Display), is an animal he will eat food (most animals can eat, eat ...) )。 What is the shape of the concrete? How do they eat things? This is not what we need to be concerned about, and what we care about is how to create an example of a particular animal through a factory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.