Design Mode -- Creation Mode -- factory method mode -- java

Source: Internet
Author: User

Intent • Define an interface for creating an object, but letsubclasses decide which class to instantiate. factory Method lets a class deferinstantiation to subclasses. • Create an interface for the object to be created so that the subclass can create the object. Applicability • a class can't anticipate the class of objects it mustcreate. • a class is not sure which of the objects it must create. • A class wants its subclasses to specify the objects itcreates • A class requires its subclass to determine which object to create. • Classes delegate responsibility to one of several helpersubclasses, and you want to localize the knowledge of which helper subclass isthe delegate. • delegate the function of the created object to the subclass and want these functions to be delegated to the subclass in a centralized manner. Consequences • it is more flexible to generate objects within the factory method than to generate objects directly. • Connects parallel class hierarchies. What is the difference between the UML factory method and the simple factory method? • A factory method interface is added. • The Factory method supports polymorphism, which means that the created object is postponed to the subclass. Example --- the iterator Collection Interface contains a method Iterator, which is implemented by ArrayList and sorted list. For ArrayList and sorted list, their Iterator objects are returned. For this Collection. iterator method, it is the factory method. The ArrayList and lifecycle list return specific products. For the above, JDK is different from this. It is mainly abstracted for explanation. For details, you can read the source code in JDK to clarify the relationship between them. In addition, Iterator is the iteration mode. • Collection. Iterator () is a factory method role • Collection is an abstract Creator role. • Iterator is an abstract Product role. • Itr (available with jdk Source Code) is a specific product role. (In fact, the template method mode is used in Itr. If you are interested, refer to the source code for analysis ). Example -- toString • the object information string is pieced together and combined according to its own needs. It is equivalent to postponing the implementation of the Object Information string created by toString In the subclass. • ToString is FactoryMethod. • Object is the abstract Creator. • The String object returned by toString is the product. Factory methods and five basic principles • compliant with the principle of opening and closing. • The Factory method cannot meet the requirements when multiple product levels exist. Is the Iterator and toString template 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.