Design pattern--2. Factory Method Mode

Source: Internet
Author: User

* 1. Workshop Method Mode:
* Define an interface for creating objects so that subclasses decide which class to instantiate. Factory methods, which instantiate a class, defer to its subclasses


* 2. Workshop method General Class diagram
* (1) Abstract product category, responsible for defining product commonality
* (2) creator Create factory class for abstraction
* (3) Concretecreator Create a specific implementation class for the abstract factory class, responsible for the specific creation of the product class
* (4) Concreteproduct is the specific implementation class of the product, created by Concretecreator
*
* 3. Advantages of the Workshop method model
* (1) Good encapsulation, code structure is clear
* (2) Excellent extensibility. In the case of adding a product class (for example, to add a strawberry), simply add a strawberry class if you modify the factory class appropriately, or expand a factory class, and the factory class does not need
* Make any changes.
* (3) Shielding product category. How the implementation class of the Product class changes, the caller does not need to know that it only cares about the interface of the product, as long as the interface does not change, the upper module in the system will not change.
* (4) decoupling.
* A. Conform to the Dimitri law, do not need to communicate
* B. Conform to the dependency inversion law and rely only on the product's abstract class
* C. Conform to the Richter scale substitution law, use the subclass of the product, replace the product's parent class
*
* 4. Simple Factory mode:
* Remove the Abstract factory

Scene class

 Public classScene Class {Private voidGrowfruit () {abstractfruitfactory absfruitfactory=Newfruitfactory (); //1.AppleApple Apple = Absfruitfactory.growfruit (apple.class);        System.out.println (Apple.getcolor ());                Apple.taste (); //2.BananaBanana Banana = Absfruitfactory.growfruit (Banana.class);        System.out.println (Banana.getcolor ());                Banana.taste (); //3.grapeGrapes grape = absfruitfactory.growfruit (grapes.class);        System.out.println (Grape.getcolor ());    Grape.taste (); }         Public Static voidMain (string[] args) {scene Class C=Newscene class ();    C.growfruit (); }}

Abstractfruitfactory

 Public Abstract class abstractfruitfactory {        //  abstract Factory, Harvest fruit public    abstract  Extends fruit> T growfruit (class<t>  c);}

Fruitfactory

 Public classFruitfactoryextendsabstractfruitfactory {@SuppressWarnings ("Unchecked") @Override Public<textendsFruit> T Growfruit (class<t>c) {Fruit Fruit=NULL; Try{Fruit=(T) Class.forName (C.getname ()). Newinstance (); }Catch(Exception e) {System.out.println ("Harvest Fruit Error"); }        return(T) fruit; }}

Fruit class

 Public Interface Fruit {    String getColor ();         void taste ();}

Specific Product Categories

Banana

 Public class Implements fruit{    @Override    public  String getColor () {        return "banana is yellow";    }    @Override      Public void taste () {         System.out.println ("banana tastes a little noodles");}    }

Apple

 Public class Implements Fruit {    @Override    public  String getColor () {        return "Apple is Red";    }    @Override      Public void taste () {         System.out.println ("Apple tastes Sweet");}    }

Grapes

 Public class Implements Fruit {    @Override    public  String getColor () {        return "Grape is purple";    }    @Override      Public void taste () {         System.out.println ("grapes taste a bit sour");}    }

Design pattern--2. 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.