Summary of simple factory models

Source: Internet
Author: User

1. OO Principle: OCP Principle (Open-Close-Principle): the entity of a software should be Open to expansion and closed to modification. (For an existing software, if it needs to be extended, it should be implemented without modifying the existing code .) 2. Simple factory mode: (1) define a class to create instances of other classes. The created instances usually have common parent classes. (2) The simple factory mode, also known as the static factory method, belongs to the class creation mode. Generally, it returns instances of different classes according to different independent variables. (3) essence: A factory class dynamically decides which product class instance to create based on input variables. Factory role: it is responsible for implementing the internal logic of all instances. The factory class can be directly called by the outside, creating the product object abstract Product role: it is the parent class of all objects created by a simple factory, describes the common features of all instances. Specific product roles: targets for creating simple factories. All created objects are instances of all specific classes of this role. The following is a simple example: [java] package factory; public interface IFruit {void plant (); void grow (); void harvest ();} defines the fruit interface, abstract The common methods of fruit (Abstract Product role) [java] package factory; public class Apple implements IFruit {private int appleAge; public void plant () {System. out. println ("apple has been planted");} public void grow () {System. out. println ("apple is growing");} public void harvest () {System. out. println ("apple Has been harvesed ");} public void setTreeAge (int age) {this. appleAge = age;} public int getTreeAge () {return this. appleAge ;}} defines the apple class, inherits the fruit interface, and implements all internal methods of fruit (specific product roles) [java] package factory; public class Grape implements IFruit {boolean seedless = false; public void plant () {System. out. println ("Grape has been planted");} public void grow () {System. out. println ("Grape is growing");} public void Harvest () {System. out. println ("Grape has been harvested");} public void setSeedless (boolean bb) {this. seedless = bb;} public boolean getSeedless () {return this. seedless ;}} the same grape category as Apple. [Java] package factory;/***** @ author ustcqi * a factory class based on input parameters, dynamically decide which product instance to create * The factory class is the key to the entire model, including the necessary logical judgment, based on the information given by the outside world, * determines the specific class object to be created. * By Using the factory class, the outside world can get rid of the Situation of directly creating specific product objects * only responsible for consuming objects, without worrying about how these objects are created and organized. * Conducive to Software Architecture Optimization */public class Factory {public IFruit factory (String s) {if (s. equals ("Apple") return new Apple (); else if (s. equals ("Grape") return new Grape (); else System. out. println ("Don't exist this fruit"); return null ;}} defines the factory class for fruit production. [Java] import java. util. *; import java. io. *; import factory. factory; import factory. IFruit; public class FactoryPattern {public static void main (String [] args) {Factory ft = new Factory (); IFruit apple = ft. factory ("Apple"); apple. grow (); IFruit grape = ft. factory ("Grape"); grape. plant (); IFruit ff = ft. factory ("dfdf") ;}} the final MAIN execution class is below the simple factory implementation of the calculator abstract role product (Operation class) [java] package pacelist; public class Operation {private double firNum; private double secNum; public void setFirNum (double a) {firNum = a;} public double getFirNum () {return this. firNum;} public void setSecNum (double B) {secNum = B;} public double getSecNum () {return secNum;} public double getResult () {return 0.0 ;}} specific product role (specific Operation) [java] package pacelist; public class Addition extends Operation {public double getResult (){ Return this. getFirNum () + this. getSecNum () ;}} subtraction Operation: [java] package pacelist; public class Subtractor extends Operation {public double getResult () {return this. getFirNum ()-this. getSecNum () ;}} simple factory class (Corner class of production products) [java] package pacelist; public class SimpleFactory {private Operation opr; public Operation factoryOperate (String s) {if (s. equalsIgnoreCase ("addition") return new Addition (); else if (s. EqualsIgnoreCase ("subtractor") return new Subtractor (); else return null ;}} MAIN execution class [java] package pac.pdf; public class Main {public static void main (String [] args) {SimpleFactory factory = new SimpleFactory (); Operation opr = factory. factoryOperate ("addition"); opr. setFirNum (12.0); opr. setsecnum( 23.9); double ret = opr. getResult (); System. out. println (ret) ;}} the last sentence: the essence of the simple factory model: A factory class based on input variables Dynamically decide which product type instance to create.

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.