抽象工厂模式实质上是在工厂方法的基础上,由一个产品增加到多个产品。
示例在工厂方法的基础上进行了改造:
先前是关系型数据库:oracle 、sqlserver
现在改进增加了非关系型数据:mongodb、hbase
package com.designepattern.abstractfactorytest.v3;
/**
* @Date:2020/12/8
* @Content:
*/
import com.mongodb.DB;
/**
*
*/
public class MainDemo {
public static void main(String[] args) {
DBFactory dbFactory1=new DBFactoryImplA();
System.out.println(dbFactory1.getRelationFactory().getDbHelper().select());
dbFactory1.getRelationFactory().getDbHelper().update();
System.out.println(dbFactory1.getNoRelationFactory().getNoRelationDbHelper().fastQuery());
DBFactory dbFactory2=new DBFactoryImplB();
System.out.println(dbFactory2.getRelationFactory().getDbHelper().select());
dbFactory2.getRelationFactory().getDbHelper().update();
System.out.println(dbFactory2.getNoRelationFactory().getNoRelationDbHelper().fastQuery());
}
}
interface RelationDbHelper {
void update();
String select();
}
class SqlserverRelationDbHelper implements RelationDbHelper {
@Override
public void update() {
System.out.println("sqlserver update");
}
@Override
public String select() {
return "sqlserver select";
}
}
class OracleRelationDbHelper implements RelationDbHelper {
@Override
public void update() {
System.out.println("oracle update");
}
@Override
public String select() {
return "oracle select";
}
}
/**
class Factory{
public DbHelper getDbHelper(String dbType){
DbHelper dbHelper=null;
if(dbType.equals("sqlserver")){
return new SqlserverDbHelper();
}
if(dbType.equals("oracle")){
return new OracleDbHelper();
}
return dbHelper;
}
}
**/
//這裡改進一下,將v2版本中的Factory具體抽象成抽象類
abstract class RelationFactoryAbstract {
abstract RelationDbHelper getDbHelper();
}
class SqlserverRelationFactoryImpl extends RelationFactoryAbstract {
@Override
RelationDbHelper getDbHelper() {
return new SqlserverRelationDbHelper();
}
}
class OracleRelationFactoryImpl extends RelationFactoryAbstract {
@Override
RelationDbHelper getDbHelper() {
return new OracleRelationDbHelper();
}
}
interface NoRelationDbHelper {
String fastQuery();
}
class MongodbNoRelationDbHelper implements NoRelationDbHelper{
@Override
public String fastQuery() {
System.out.println("MongodbNoRelationDbHelper--fastQuery");
return "MongodbNoRelationDbHelper--fastQuery";
}
}
class HbaseNoRelationDbHelper implements NoRelationDbHelper{
@Override
public String fastQuery() {
System.out.println("HbaseNoRelationDbHelper--fastQuery");
return "HbaseNoRelationDbHelper--fastQuery";
}
}
abstract class NoRelationFactoryAbstract {
abstract NoRelationDbHelper getNoRelationDbHelper();
}
class MongoDBNoRelationFactoryImpl extends NoRelationFactoryAbstract{
@Override
NoRelationDbHelper getNoRelationDbHelper() {
return new MongodbNoRelationDbHelper();
}
}
class HbaseNoRelationFactoryImpl extends NoRelationFactoryAbstract{
@Override
NoRelationDbHelper getNoRelationDbHelper() {
return new HbaseNoRelationDbHelper();
}
}
//將RelationFactoryAbstract和
abstract class DBFactory{
abstract RelationFactoryAbstract getRelationFactory();
abstract NoRelationFactoryAbstract getNoRelationFactory();
}
//兩種不同的方案(sqlserver+mongodb)
class DBFactoryImplA extends DBFactory {
@Override
RelationFactoryAbstract getRelationFactory() {
return new SqlserverRelationFactoryImpl();
}
@Override
NoRelationFactoryAbstract getNoRelationFactory() {
return new MongoDBNoRelationFactoryImpl();
}
}
//兩種不同的方案(oracle+hbase)
class DBFactoryImplB extends DBFactory {
@Override
RelationFactoryAbstract getRelationFactory() {
return new OracleRelationFactoryImpl();
}
@Override
NoRelationFactoryAbstract getNoRelationFactory() {
return new HbaseNoRelationFactoryImpl();
}
}