24-day design model-factory method and 24-day Design Model

Source: Internet
Author: User

24-day design model-factory method and 24-day Design Model
I. Factory method model

Defines an interface for creating objects, but the subclass determines which class to instantiate. The factory method allows classes to postpone Instantiation to subclass. The so-called decision does not allow the subclass to make decisions at runtime in the batch mode, but does not need to know which product to create when writing the Creator class, select to use

The subclass determines what the product is.

Ii. Example

/*** File name: factorymethod. java * Description: factory mode * created by: Lin bingwen * Date: 2015.1.26 **/package factory;/** abstract product class, **/abstract class Product {public abstract void use ();}/** specific Product category, low-level membership card **/class IdcardLow extends Product {private String owner; // name of the current card to be created // The card building constructor IdcardLow (String owner) {this. owner = owner;} // overload function @ Overridepublic void use () {System. out. println ("this is a low-level member" + owner + "in-use card") ;}/ ** specific Product class, advanced membership card **/class idcardhweigh extends Product {private String owner; // name of the current card to be created // The card building constructor IdcardHigh (String owner) {this. owner = owner;} // overload function @ Overridepublic void use () {System. out. println ("this is a senior member" + owner + "in-use card") ;}}/** abstract factory class, **/abstract class Factory {protected abstract Product CreateProduct (String owner);}/** specific Factory class, dedicated production of low-level membership cards **/class FactoryLow extends Factory {// overload function @ Overrideprotected Product CreateProduct (String owner) {return new IdcardLow (owner );}} /** specific Factory class, specializing in the production of advanced membership cards **/class FactoryHigh extends Factory {// overload function @ Overrideprotected Product CreateProduct (String owner) {return new IdcardHigh (owner) ;}} public class factorymethod {public static void main (String [] args) {Factory factory1 = new FactoryLow (); factory factory2 = new FactoryHigh ();/* produce a low-level membership card */Product card1 = factory1.CreateProduct ("Xiao Ming"); Product card2 = factory1.CreateProduct ("xiao hong "); /* produce senior membership cards */Product card3 = factory2.CreateProduct ("Daming"); Product card4 = factory2.CreateProduct (""); card1.use (); card2.use (); card3.use (); card4.use ();}}

Effect:




Iii. Advantages and disadvantages of the factory method model

Advantages:

Implemented the design of polymorphism Based on the factory role and Product role. It enables the factory to determine the product object to be created, and the details of how to create this object are completely encapsulated in a specific factory. In addition, when adding new products to the system, you do not need to modify the interfaces provided by the abstract factory, instead of modifying other specific factories. You only need to add new specific factories and products.

 Disadvantages:

Creator and the corresponding subclass are required to serve as the carrier of the factory method, adding a level.

 Contact:

The factory method mode further abstracts and promotes the simple factory mode, defines the factory interface, and delays the actual creation to the subclass. If there is only one specific factory class, it can be transformed into a simple factory model.







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.