Java Implements abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode

There are several concepts: abstract factory, physical factory, abstract product, physical product

Abstract Factory : Define an abstract method for creating a product

Physical Factory : What kind of product is created specifically

Abstract Product : An interface or base class

Physical Products : Implementing specific functions, or deriving


Abstract Product 1public interface IProduct1 {public void show ();

Abstract Product 2public interface IProduct2 {public void show ();

Physical product 1public class Product1 implements IProduct1 {public      void Show () {          System.out.println ("Created 1 type Product");      }  }  

Physical product 2public class Product2 implements IPRODUCT2 {public void Show () {System.out.println ("Created 2 type Product");}}

/* * 1 a 1 B belongs to Product1 in the same product hierarchy * 2A 2B belongs to Product2 */public class Gradeproduct1a extends Product1 {@Overridepublic void show () {super.show (); System.out.println ("This is under Product 1: Product a");}}

public class Gradeproduct1b extends Product1 {@Overridepublic void Show () {super.show (); System.out.println ("This is under Product 1: Product B");}}

public class GRADEPRODUCT2A extends Product2 {@Overridepublic void Show () {super.show (); System.out.println ("This is under Product 2: Product A");}}

public class Gradeproduct2b extends Product2 {@Overridepublic void Show () {super.show (); System.out.println ("This is under Product 2: Product B");}}

Abstract factory create different abstract products public interface Ifactory {public IProduct1 createproduct1a ();//1 class model Apublic IProduct1 CREATEPRODUCT1B ();//1 type product model Bpublic IProduct2 CREATEPRODUCT2A ();//2 class model Apublic IProduct2 createproduct2b ();//2 Products Model B}

Entity Factory Create entity product, return type abstract product public class Factory implements Ifactory {public IProduct1 createproduct1a () {return new Gradeprod UCT1A ();} Public IProduct1 createproduct1b () {return new gradeproduct1b ();} Public IProduct2 CREATEPRODUCT2A () {return new GRADEPRODUCT2A ();} Public IProduct2 createproduct2b () {return new GRADEPRODUCT2B ();}}

/* Abstract Factory mode: Used to create a set of related or interdependent objects *  process: Abstract factory, physical factory production products * abstract products, physical products to achieve product functionality * Disadvantage: When a product needs to be added (composed of abstractions and entities), the factory needs to change the */public class test4abstract {public static void main (string[] args) {Ifactory factory = new Factory (); IProduct1 product1a = Factory.crea TEPRODUCT1A ();p roduct1a.show (); IProduct1 product1b = factory.createproduct1b ();p roduct1b.show (); IProduct2 PRODUCT2A = FACTORY.CREATEPRODUCT2A ();p roduct2a.show (); IProduct2 product2b = FACTORY.CREATEPRODUCT2B (); Product2b.show ();}}


Java Implements abstract Factory 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.