首页 > 代码库 > java设计模式_抽象工厂

java设计模式_抽象工厂


/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: DatabaseFactory.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午12:03:46 
 * @version V1.0
 */
public interface DatabaseFactory {
    
    public User createUser();
    public Department createDepartment();

}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: OracleFactory.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午2:15:54 
 * @version V1.0
 */
public class OracleFactory implements DatabaseFactory {

    @Override
    public User createUser() {
        return new OracleUserImp();
    }

    @Override
    public Department createDepartment() {
        return new OracleDepartmentImp();
    }

}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: SqlServerFactory.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午2:15:35 
 * @version V1.0
 */
public class SqlServerFactory implements DatabaseFactory {

    @Override
    public User createUser() {
        return new SqlServerUserImp();
    }

    @Override
    public Department createDepartment() {
        return new SqlServerDepartmentImp();
    }

}


package com.wangbiao.design.abstractfactory;

/**
 * 
 * @Title: User.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午12:04:25 
 * @version V1.0
 */
public interface User {
    public void insertUser();
    public void selectUser();
}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

import com.wangbiao.design.abstractfactory.User;

/**
 * @Title: SqlServerUserImp.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午2:02:29 
 * @version V1.0
 */
public class SqlServerUserImp implements User {

    @Override
    public void insertUser() {
        System.out.println("insert User using Sql Server ");
        
    }

    @Override
    public void selectUser() {
        System.out.println("select User using Sql Server ");
    }


}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

import com.wangbiao.design.abstractfactory.User;

/**
 * @Title: OracleUserImp.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午2:02:49 
 * @version V1.0
 */
public class OracleUserImp implements User {

    @Override
    public void insertUser() {
        System.out.println("insert User using Oracle ");
    }

    @Override
    public void selectUser() {
        System.out.println("select User using Oracle ");        
    }


}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: Department.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午12:04:19 
 * @version V1.0
 */
public interface  Department {
    public void insertDepartment();
    public void selectDepartment();
}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: OracleDepartmentImp.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午2:04:50 
 * @version V1.0
 */
public class OracleDepartmentImp implements Department {

    @Override
    public void insertDepartment() {
        System.out.println("insert Department using Oracle ");            
    }

    @Override
    public void selectDepartment() {
        System.out.println("select Department using Oracle ");            
    }

}

/**
 * 
 */
package com.wangbiao.design.abstractfactory;

/**
 * @Title: Client.java
 * @Package com.wangbiao.design.abstractfactory
 * @Description: TODO 
 * @author wangbiao   
 * @date 2014-9-26 下午12:03:00 
 * @version V1.0
 */
public class Client {

    public static void main(String[] args) {
        
        
        DatabaseFactory factory_oracel = new OracleFactory();
        User user_oracel = factory_oracel.createUser();
        Department depart_oracle = factory_oracel.createDepartment();
        
        user_oracel.insertUser();
        user_oracel.selectUser();
        depart_oracle.insertDepartment();
        depart_oracle.selectDepartment();
        
        
        DatabaseFactory factory_sqlserver = new SqlServerFactory();
        User user_sqlserver = factory_sqlserver.createUser();
        Department depart_sqlserver = factory_sqlserver.createDepartment();
        
        user_sqlserver.insertUser();
        user_sqlserver.selectUser();
        depart_sqlserver.insertDepartment();
        depart_sqlserver.selectDepartment();
        
    }

}


java设计模式_抽象工厂