Java Abstract Factory mode (Liar design mode)

Source: Internet
Author: User

See a few times the abstract Factory mode, each view needs to be re-understood, it may be involved in a number of classes and interfaces, so more difficult to clear the relationship it! As far as I'm concerned, we still have to learn from our thoughts rather than mechanically.

Look at the class diagram:

Liar design mode-Class diagram

Look at the class diagram has been very chaotic, to simply comb under. We only look at the interface, so it is not difficult to see that the abstract factory is actually the production of abstract products, and concrete implementation is the implementation of the class to do.

Look at the specific demo:

Factory Floor

 /**   * Factory interface   */ public  interface   ifactory { public   Iuser    CreateUser ();  public   idepartment createdepartment ();}  
 /**   * MySQL factory   */ public  class  mysqlfactory implements   ifactory{@Override  public   Iuser CreateUser () { retu    RN  new   Usermapper (); @Override  public   idepartment Createdepartment () { return  new     Span style= "COLOR: #000000" > Departmentmapper (); }}
/***/Publicclassimplements  ifactory{    @Override      public iuser CreateUser () {        returnnew  usermapper ();    }    @Override    public  idepartment createdepartment () {        returnNew  departmentmapper ();    }}

Product Layer

 /**   * Departmental Layer interface   */ public  interface   idepartment { public  int
     Savedepartment (Department Department);  public  idepartment getdepartment (int   ID);}  
/***/Publicinterface  iuser    {publicint saveuser (user user);      Public Iuser getUser (int  ID);}
/*** Departmental Data layer*/ Public classDepartmentmapperImplementsidepartment{@Override Public intsavedepartment (Department Department) {System.out.println ("Saved the department."); return1; } @Override PublicIdepartment Getdepartment (intID) {SYSTEM.OUT.PRINTLN ("Got a department."); return NULL; }}
/*** Role Data layer*/ Public classUsermapperImplementsIuser {@Override Public intsaveuser (user user) {System.out.println ("Save Uer"); return1; } @Override PublicIuser GetUser (intID) {SYSTEM.OUT.PRINTLN ("Get a user"); return NULL; }}

Client and entity classes

/***/Publicclass  user{}
/***/Publicclass  Department {}
/***/Publicclass  Test    {publicstatic  void  Main (string[] args) {//        ifactory factory = new Mysqlfactory ();        New sqlserverfactory ();         = Factory.createuser ();        Usermapper.getuser (Usermapper.saveuser (new  User ()));}    }

If we switch the database, we only need to switch the factory, the other code does not need to change.

The above is the author of the abstract Factory understanding, hope to help learn the small partner abstract factory!

Java Abstract Factory mode (Liar design mode)

Related Article

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.