Abstract Factory example

Abstract Factory:  

   Each of the plurality of parent parent several subclasses

  A student Dao abstract parent class CRUD method
  corresponding to a student database storage Dao subclasses implement
  a file storage implementation subclasses students Dao

  A teacher Dao abstract parent class CRUD method
  corresponding to a database storage teacher Dao implementation subclasses
  of teachers of a file storage implementation subclasses Dao

  Creating an abstract factory method to create a student teacher dao dao method
  methods db factory to create a subclass of student teacher dao dao create a
  file created sub-class factory method to create a student teacher dao dao method

  To determine which storage method selected by the configuration file
  in servise, the need to create an instance of an object dao

Abstract factory which is determined by the configuration file storage using an abstract entity objects from the plant is determined,

By entities subject to be called to choose their own teachers or students Dao Dao
entity class

public interface StudentDao {
    void insertStudent() ;
}
Students Dao Interface
public interface TeacherDao {
    void insertTeacher() ;
}
Teacher Dao Interface
1  / * *
 2  * abstract factory,
 3  * 1, by selecting the profile data storage
 4  * 2, Dao specification method of creating a subclass
 . 5  * @author Administrator
 . 6  *
 . 7   * / 
. 8  public  abstract  class DaoFactory {
 . 9          
10  //      1, there is provided a method of obtaining a factory object, factory object obtained by this method   
. 11       public  static DaoFactory getDaoFactory () {
 12 is  //          1.1 creating properties object 
13 is           the Properties properties = new new the Properties ();
 14  //          1.2 load profile profile It is stored in a database or file type dao type of Dao 
15          the try {
 16              Properties.load ( new new the FileReader ( " abstractfactory.properties " ));
 . 17  //          1.3 naming authority for access by the key 
18 is              String = Properties.getProperty Property ( " Property " );
 . 19  //          1.4-based reflective acquired by this class and creates an object 
20 is              DaoFactory DAOFactory = (DaoFactory) the Class.forName (Property) .newInstance ();
 21 is               return DAOFactory;
 22 is          } the catch (Exception E) {
 23 is              e.printStackTrace ();
 24         }
 25  //          1.5 returns the object   
26 is          return  null ;
 27       }
 28  //      2, the user gets to achieve DaoFactory class object to create a subject in need Dao method object is called by the plant 
29       public  abstract StudentDAO createStudentDao ();
 30       public  abstract TeacherDao createTeacherDao ();
 31 is }
1  / * *
 2  * naming rights in the profiles
 3  * If the user selects the database storage, the plant is used to create corresponding Dao
 . 4  * @author Administrator
 . 5  *
 . 6   * / 
. 7  public  class DbDaoFactory the extends DaoFactory {
 . 8  
. 9      @Override
 10      public StudentDAO createStudentDao () {
 . 11          
12 is          return  new new DbStudentDao ();
 13 is      }
 14  
15      @Override
 16      public TeacherDao createTeacherDao () {
 . 17          return  new new DbTeacherDao ();
18     }
19 
20 }
Database storage facility
1  / * *
 2  * naming rights in the profiles
 3  * If the user selects the database storage, the plant is used to create corresponding Dao
 . 4  * @author Administrator
 . 5  *
 . 6   * / 
. 7  public  class FileDaoFactory the extends DaoFactory {
 . 8  
. 9      @Override
 10      public StudentDAO createStudentDao () {
 . 11          
12 is          return  new new FileStudentDao ();
 13 is      }
 14  
15      @Override
 16      public TeacherDao createTeacherDao () {
 . 17          return  new new FileTeacherDao ();
18     }
19 
20 }
File storage facility
. 1  public  class DbTeacherDao the implements TeacherDao {
 2  
. 3      @Override
 . 4      public  void insertTeacher () {
 . 5          the System. OUT .println ( " Database Storage Add teacher " );
 6      }
 . 7  
. 8 }
Dao database storage type of teacher
. 1  public  class DbStudentDao the implements StudentDAO {
 2  
. 3      @Override
 . 4      public  void insertStudent () {
 . 5          the System. OUT .println ( " Database Storage add student " );
 . 6          
. 7      }
 . 8  
. 9 }
Dao database storage type of student
. 1  public  class FileTeacherDao the implements TeacherDao {
 2  
. 3      @Override
 . 4      public  void insertTeacher () {
 . 5          the System. OUT .println ( " File Storage Add teacher " );
 6      }
 . 7  
. 8 }
File storage type of teacher Dao
. 1  public  class FileStudentDao the implements StudentDAO {
 2  
. 3      @Override
 . 4      public  void insertStudent () {
 . 5          the System. OUT .println ( " file stores add student " );
 . 6          
. 7      }
 . 8  
. 9 }
File storage type of student Dao

Then Servise create factories, call the corresponding Dao perform CRUD

Profiles

property=com.methodfactory.DbDaoFactory
#property=com.methodfactory.FileDaoFactory

Guess you like

Origin www.cnblogs.com/19322li/p/10983185.html