Examples of simple factories and abstract factory patterns in Java design pattern programming _java

Source: Internet
Author: User

Simple Factory mode
class Diagram

Create a corresponding object in one condition through a factory class

//Business functions public interface Icalculation {double GetResult (double NumA, double numB); 
    public class Calcadd implements Icalculation {@Override public double getresult (double NumA, double NumB) { 
    System.out.println ("addition"); 
  return NumA + NumB;  } public class Calcsubtract implements Icalculation {@Override public double getresult (double-NumA, double 
    NumB) {System.out.println ("subtraction"); 
  return numa-numb; * * * Simple Factory mode concerns: Object creation * Create a factory class to create an instance of some classes that implement the same interface or that have inheritance relationships/public class Test4simple {public Stati 
    c void Main (string[] args) {//String operation = '/'; 
String operation = "+"; 
String operation = "-"; 
    String operation = "*"; 
    Double NumA = NumB = 33; 
    Icalculation Createcalc = calcsimplefactory.createcalc (operation); 
    Double result = Createcalc.getresult (NumA, NumB); 
  SYSTEM.OUT.PRINTLN (result); } 
} 

Simple factory defects: Simple factory to create products need to pass the appropriate parameters, if the transmission is not correct to get the object.
Improvements: Multiple methods to create different products
Class diagram

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 =, 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); 

 } 
}

Abstract Factory pattern
There are several concepts: abstract factories, physical factories, abstract products, physical products
Abstract Factory: Defining an abstract method for creating a product
Entity factory: What kind of product is created specifically
Abstract Product: An interface or base class
Entity Products: Implementing specific functions, or deriving
Class diagram

Abstract Product 1 public interface IProduct1 {public void show (); 
}//Abstract product 2 public interface IProduct2 {public void show ();  
  }//Entity Product 1 public class Product1 implements IProduct1 {public void Show () {System.out.println ("created 1 products"); }//Entity product 2 public class Product2 implements IPRODUCT2 {public void Show () {System.out.println ("Created 2 products 
  "); }/* * 1 a 1 B belongs to Product1 the same product hierarchy * 2A 2B belongs to Product2/public class GRADEPRODUCT1A extends Product1 {@Ove 
    Rride public void Show () {super.show (); 
  System.out.println ("This is Product 1: Product a"); 
    } public class Gradeproduct1b extends Product1 {@Override public void Show () {super.show (); 
  System.out.println ("This is product 1: Product B"); 
    } public class GRADEPRODUCT2A extends Product2 {@Override public void Show () {super.show (); 
  System.out.println ("This is Product 2: Product a"); } public class Gradeproduct2b extends Product2 {@Override public void Show () {
    Super.show (); 
  System.out.println ("This is product 2: Product B"); }//abstract factory create different abstract products public interface Ifactory {public IProduct1 createproduct1a ();//1 class product Model A public IProduct1  CREATEPRODUCT1B ()//1 product model B public IProduct2 CREATEPRODUCT2A ()//2 class product Model A public IProduct2 createproduct2b ();//2 Products 
    Model B}//entity factory create entity product, return type is abstract product public class Factory implements Ifactory {public IProduct1 createproduct1a () { 
  return new GRADEPRODUCT1A (); 
  Public IProduct1 createproduct1b () {return new gradeproduct1b (); 
  Public IProduct2 CREATEPRODUCT2A () {return new GRADEPRODUCT2A (); 
  Public IProduct2 createproduct2b () {return new gradeproduct2b (); }/* Abstract factory pattern: Used to create a set of related or interdependent objects * Process: Abstract factory, Entity factory production * Abstract product, Entity product realization Product Function * Disadvantage: When need to add a product (by abstraction and entity), factory Need to change */public class Test4abstract {public static void main (string[] args) {Ifactory factory = new Factory () 
    ; IProduct1 PRODUCT1A = factory.createproduct1A (); 
    Product1a.show (); 
    IProduct1 product1b = factory.createproduct1b (); 
     
    Product1b.show (); 
    IProduct2 PRODUCT2A = FACTORY.CREATEPRODUCT2A (); 
    Product2a.show (); 
    IProduct2 product2b = FACTORY.CREATEPRODUCT2B (); 
  Product2b.show (); 
 } 
}

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.