Java design mode-Factory mode

Source: Internet
Author: User

Real World Example
> Blacksmith makes weapons. Elves need elf weapons and orcs need Orc weapons. Summon the right type of blacksmith according to the customer at hand.
> Blacksmith manufactures weapons. Elves require Elvish weapons, orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.

In plain words
> It provides a way to delegate instantiation logic to subclasses.
> It provides a-delegate the instantiation logic to child classes.

Wikipedia says
> in class-based programming, the factory method pattern is a creation pattern that uses factory methods to handle the problem of creating objects without specifying the exact category of objects that will be created.
This is done by invoking a factory method to create an object that is either specified in the interface or implemented by a subclass, implemented in a base class, and can be overridden by a derived class rather than by calling the constructor.
> in class-based programming, the factory method pattern was a creational pattern that uses factory methods to deal with The problem of creating objects without have to specify the exact class of the object, that would be created. This was done by creating objects by calling a factory method-either specified in an interface and implemented by child CLA SSEs, or implemented in a base class and optionally overridden by derived classes-rather than by calling a constructor.



Use the Factory Method pattern when
Use factory method mode:
* A class can ' t anticipate the class of objects it must create
* A class cannot predict the class of objects it must create
* A class wants its subclasses to specify the objects it creates
* A class that wants its subclasses to specify the objects it creates
* Classes delegate responsibility to one of several helper subclasses, and your want to localize the knowledge of which Hel Per subclass is the delegate
* Class will delegate responsibility to one of several helper subclasses, and you want to localize which helper subclasses are the knowledge of the delegate

Java design mode-Factory mode

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.