Java implements the simple factory Mode

Source: Internet
Author: User

Java implements the simple factory Mode

The factory mode belongs to the creation mode.

Simple factory Mode

Create an object using a factory class and a condition

// Business function public interface ICalculation {double getResult (double numA, double numB );}

Public class CalcAdd implements ICalculation {@ Overridepublic double getResult (double numA, double numB) {System. out. println ("addition"); return numA + numB ;}}

Public class CalcSubtract implements ICalculation {@ Overridepublic double getResult (double numA, double numB) {System. out. println ("subtraction"); return numA-numB ;}}

/** Simple factory mode follow: Object creation * creates a factory class, create instances for classes that implement the same interface or have an inheritance relationship */public class Test4Simple {public static void main (String [] args) {// String operation = "/"; String operation = "+"; // String operation = "-"; // String operation = "*"; double numA = 182, numB = 33; ICalculation createc1c = CalcSimpleFactory. createcequal (operation); double result = createcequal. getResult (numA, numB); System. out. println (result );}}

Defects of a simple factory: when creating a product in a simple factory, you must pass the corresponding parameters. If the parameters are incorrect, the object cannot be obtained.

Improvement: multiple methods to create different products

public class CalcMultipleFactory {public static ICalculation produceAdd() {return new CalcAdd();}public static ICalculation produceSubtract() {return new CalcSubtract();}public static ICalculation produceMultiply() {return new CalcMultiply();}public static ICalculation produceDivide() {return new CalcDivide();}}

public class Test4Multiple {public static void main(String[] args) {double numA = 182, numB = 33;ICalculation createCalc = CalcMultipleFactory.produceAdd();double result = createCalc.getResult(numA, numB);System.out.println(result);createCalc = CalcMultipleFactory.produceSubtract();result = createCalc.getResult(numA, numB);System.out.println(result);createCalc = CalcMultipleFactory.produceMultiply();result = createCalc.getResult(numA, numB);System.out.println(result);createCalc = CalcMultipleFactory.produceDivide();result = createCalc.getResult(numA, numB);System.out.println(result);}}



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.