package com.depthmind.designpattern;
import java.sql.Connection;
public class AbstractFactoryPattern {
public static void main(String[] args) {
IDatabaseUtils iDatabaseUtils = new OracleDatabaseUtils();
IConnection connection = iDatabaseUtils.getConnection();
connection.connect();
ICommand command = iDatabaseUtils.getCommand();
command.command();
}
}
interface IConnection {
void connect();
}
class MysqlConnection implements IConnection{
@Override
public void connect() {
System.out.println("mysql connect complete!!!!");
}
}
class OracleConnection implements IConnection {
@Override
public void connect() {
System.out.println("oracle connect complete!!!!");
}
}
interface ICommand {
void command();
}
class MysqlCommand implements ICommand{
@Override
public void command() {
System.out.println("mysql command send!!!!");
}
}
class OracleCommand implements ICommand {
@Override
public void command() {
System.out.println("oracle command send!!!!");
}
}
interface IDatabaseUtils {
IConnection getConnection();
ICommand getCommand();
}
class MysqlDatabaseUtils implements IDatabaseUtils{
@Override
public IConnection getConnection() {
return new MysqlConnection();
}
@Override
public ICommand getCommand() {
return new MysqlCommand();
}
}
class OracleDatabaseUtils implements IDatabaseUtils {
@Override
public IConnection getConnection() {
return new OracleConnection();
}
@Override
public ICommand getCommand() {
return new OracleCommand();
}
}
来源:CSDN
作者:点不点外卖
链接:https://blog.csdn.net/hansirl/article/details/104310658