Design Pattern Three: Factory method mode

Source: Internet
Author: User

Puzzle: The 1.interface interface cannot be instantiated (new), but a reference can be defined to point to an entity class that inherits the interface, such as: interface animal{void Eat (),} class Cat implements animal{p     ublic void Eat () {System.out.println ("the Cat eat!"); }} Animal an = new Cat (); I. What is the factory method mode      1. is a further abstraction of the simple factory model;     2. Defines a factory interface that creates product objects, deferring actual creation to subclasses.      3. The core factory class is no longer responsible for product creation, it is an abstract factory role and is only responsible for declaring the methods that the factory subclass must implement.      4. All factories and the like must implement this abstract factory, and each product has one by one corresponding factory subclass responsible for product creation (instantiation); second, the role of factory method mode product:     1. Product parent class or interface: A common parent class or interface for a specific product.      2. Specific product Categories: specific products, factories to instantiate the target; factory:     1. Abstract Factory (interface): is the core of the factory method pattern. Any factory subclass must implement this interface;     2. Specific product factory: With the specific product, is responsible for the creation of specific product examples. Three. Class diagram      four. In-depth analysis      1. The most important feature of the factory method model is that the right to create an instance is given to the client in relation to the simple Factory mode. Instead of a unified factory. Therefore, the implementation of the simple factory violates the "root-closed" defects, to achieve a scalable.      2. The factory method model abstracts the Simple factory model. There is an abstract factory class (which can be abstract classes and interfaces), and this class will no longer be responsible for the production of specific products, but only to formulate some specifications, specific production work by its subclasses to complete. In this mode, the factory class and the product class can often correspond in turn. That is, an abstract factory corresponding to an abstract product, a specific factory corresponding to a specific product, this specific factory is responsible for the production of the corresponding products.     3. Scenario:          A. For a product, the caller clearly knows which specific factory service should be used, instantiate the specific plant, and produce a specific product.           B. Just need a product, but do not want to know and do not need to know which factory is the production, that is, the final choice of which specific factory decision on the producer side, they according to the current system situation to instantiate a specific factory returnBack to the user, and this decision-making process is transparent to the user.   Five. Code  //Product Parent class product{     int num;           int Price;}  //specific Products Class ProductA extends product{     public void setnum (int num) {        &N Bsp num = num;     }     public void SetName (String name) {          NAME = Name;     }} class PRODUCTB extends product{      public void setnum (int num) {          num = num;     }    &NBS p;public void SetName (String name) {          name = name;     }}    //Abstract Factory class interface factory{     product creatproduct ();}  //specific factory Subclass class Productafactory Implemnets factory{     public Product creatproduct () {         return  new producta ();  &nbSp;   }} class Productbfactory implements factory{     public Product creatProduct () {     return new PRODUCTB ();      }}------------------------------------- ---client code: Factory Factory = new Productafactory (); Product Product = Factory.creatproduct ();  //production of specific products

Design mode Three: Factory method 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.