Abstract Factory mode

Source: Internet
Author: User

What is abstract Factory mode:
anforoforwithout specifying their concrete classes.

1. is to provide an interface, the purpose of this interface is to create objects.

2. What kind of objects are created? Create an object with an ethnic relationship.

The so-called product group refers to a family of products that are related to functions in different product hierarchies.

3. How to create? Not by making a specific type.

1. We have an abstract factory interface:

    • Abstractfactory

All specific factories are to produce (create) objects by implementing this interface.
2. Several (two) specific factories that implement this abstract plant, plant a, plant B.
Factory a generates a series of products.

    • Concretefactorya

Plant B generates products from the B-series.

    • Concretefactoryb

3. Because a, b more than a single product, they each have a family of products (product A, group B, including many products), so there is another abstract a, B product definition.

    • abstractproducta
    • Abstractproductb

      4. The client interacts only with Abstractfactory, ABSTRACTPRODUCTB, Abstractproducta. And the client completely does not know the existence of the implementation class, only at runtime to decide which specific factory implementation to complete the creation of specific products .

      In other words, the client is fully decoupled from the specific implementation. On the other hand, new product families are also very flexible to be added (easy to expand). How to join? By going to create an implementation class that implements the abstract factory (CONCRETEFACTORYC, Concretefactoryd ... ), and the corresponding creation implements the abstract product family (ABSTRACTPRODUCTC, abstractproductd ... ) of specific products.

Applicable scenarios

When you want to create multiple product (object) Families or a range of products, but do not want to burst the details about the implementation of the product creation. because, the client (caller) is fully decoupled from the specific implementation.

code example
//our abstractproduct  public  interface  window { public  void  settitle     (String text); public  void  repaint  ();}
//ConcreteProductA1publicclass MSWindow implements Window{   publicvoidsetTitle()  {     //MS Windows specific behaviour   }
   publicvoidrepaint()  {     //MS Windows specific behaviour   }}
//concreteproducta2  public  class  macosxwindow  implements  Window  { public  void  settitle  () {//mac OSX specific Behaviour } public  void  repaint  () {//mac OSX Specific Behaviour }}  
//abstractfactory  public  interface  abstractwidgetfactory{public  window createwindow  ();}  
//concretefactory1  public  class  mswindowswidgetfactory  implements   Abstractwidgetfactory  { //create an mswindow  public  window createwindow  () {mswindow window = new  Mswindow ();   return  window; }}
//concretefactory2  public  class  macosxwidgetfactory  implements   Abstractwidgetfactory  { //create a macosxwindow  public  window createwindow  () {macosxwindow window = new  Macosxwindow ();   return  window; }}
//client   Public  class  guibuilder{public  void  buildwindow  (abstractwidgetfactory widgetFactory         {window window = Widgetfactory.createwindow ();   Window.settitle ( "New window" ); }}
publicclass Main{   publicstaticvoidmain(String[] args)      {     new GUIBuilder();     null;     //check what platform we‘re on      if(Platform.currentPlatform()=="MACOSX")     {        widgetFactory  new MacOSXWidgetFactory();     }     else     {           widgetFactory  new MsWindowsWidgetFactory();     }     builder.buildWindow(widgetFactory);     }}

As a result, we get specific products on the client side through an abstract product interface.
Did you find a problem? Abstract factories are actually using the hands of specific factories to dynamically obtain specific products. The specific factory is coupled with the client, shouldn't it be dynamically acquired? Why instantiate?

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.