I am writing this article is anonymous class, as well as an understanding of the abstract factory ... find some inspiration to write down .. hope that we can learn together ...

Abstract factory class

package com.chusiyou.factory;

public abstract class Factory (

public abstract UserDao getUserDao ();
public abstract PersonDao getPersonDao ();
/ / Get an instance of a factory method
public static Factory getFactory (String dataBaseName)
(
/ / For the database JDBC different operations take different
if (dataBaseName.equals ( "mysql"))
(
return new Factory ()
(
public PersonDao getPersonDao () (
/ / TODO Auto-generated method stub
return new PersonDao ()
(
/ / JDBC code ......
public boolean addPerson (Person person) (
/ / TODO Auto-generated method stub
return false;
)
public boolean deletePerson (Person person) (
/ / TODO Auto-generated method stub
return false;
)
);
)

public UserDao getUserDao () (
/ / TODO Auto-generated method stub
return new UserDao ()
(

public boolean addUser (User user) (
/ / TODO Auto-generated method stub
return true;
)

public boolean deleteUser (User user) (
/ / TODO Auto-generated method stub
return false;
)

);
)

);
) else if (dataBaseName.equals ( "orcale"))
(
return new Factory ()
(

public PersonDao getPersonDao () (
/ / TODO Auto-generated method stub
return new PersonDao ()
(

public boolean addPerson (Person person) (
/ / TODO Auto-generated method stub
return false;
)

public boolean deletePerson (Person person) (
/ / TODO Auto-generated method stub
return false;
)

);
)

public UserDao getUserDao () (
/ / TODO Auto-generated method stub
return new UserDao ()
(

public boolean addUser (User user) (
/ / TODO Auto-generated method stub
return false;
)

public boolean deleteUser (User user) (
/ / TODO Auto-generated method stub
return false;
)

);
)

);
)
return null;
)

)

Interface and some other business entity class

package com.chusiyou.factory;
/ / Define the operational interface to the database access
public interface PersonDao (
/ / Delete operation
public boolean deletePerson (Person person);
/ / Additional operations
public boolean addPerson (Person person);

)

package com.chusiyou.factory;
/ / Define the operational interface to the database access
public interface UserDao (
public boolean deleteUser (User user);
public boolean addUser (User user);

)

package com.chusiyou.factory;

/ / Entity class
public class Person (

)

package com.chusiyou.factory;

/ / Entity class
public class User (

)

Test class

package com.chusiyou.factory;

public class FactoryTest (

/ **
* @ Param args
* /
public static void main (String [] args) (
/ / TODO Auto-generated method stub
User user = new User ();
if (Factory.getFactory ( "Access"). getUserDao (). addUser (user))
(
System.out.println ( "increasing success");
) else
(
System.out.println ( "increase failed");
)
)

)