Factory mode and abstract Factory mode

Source: Internet
Author: User

1. Factory mode: Defines a set of interfaces for creating objects, but subclasses decide which class to instantiate. The factory method defers the instantiation of the class to the subclass.

Now consider that there are pizzastore (franchisees), they may have some improvements to the pizza approach, that is, the Createpizza method needs to be upgraded. The approach of the factory method is to create a method in the base class that is shaped as abstract Product FactoryMethod (String type), which, as a factory method, is responsible for creating the object, which can be overridden by the subclass to alter the process of creating the object.

The subclass returns an object of type pizza, which can be used uniformly by the parent class without concern for the specific type

2. Abstract Factory mode: Provides an interface for creating an object family without specifying a specific class.

Factory methods only involve the creation of an object, sometimes we need a family of objects, such as the example in the book: The raw Material family. Making pizza requires a lot of raw materials, each of which requires a factory approach. At this point we set all the methods involved in creating objects together as an interface. The user is programmed for this interface, and the interface is implemented by a specific class. This way we can pass the corresponding class that implements this interface to the consumer, depending on the situation.

Factory mode and abstract 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.