设计模式-抽象工厂模式

抽象工厂模式,定义工厂接口,生产某一种类型的配件全部由某一家工厂所提供,解决不同工厂的兼容性问题。

/**
 * 抽象工厂模式
 */
public class AbstratFactoryMethod {
    public static void main(String[] args) {
     Application application = new ConcreteProductA();
// Application application = new ConcreteProductA1();
Product product = application.getObject();
product.method1();
} } //数据库连接接口,抽象工厂 interface IDatabaseUtils { IConnection getConnection(); ICommand getCommand(); } //连接接口 interface IConnection { void connect(); } //指令接口 interface ICommand { void command(); } //实现类1 class MysqlConnection implements IConnection { @Override public void connect() { System.out.println("mysql connect"); } } //实现类2 class OracleConnection implements IConnection { @Override public void connect() { System.out.println("Oracle connect"); } } //实现类1 class MysqlCommand implements ICommand { @Override public void command() { System.out.println("mysql command"); } } //实现类2 class OracleCommand implements ICommand { @Override public void command() { System.out.println("Oracle command"); } } //抽象工厂实现1 class MysqlDataBaseUtils implements IDatabaseUtils { @Override public IConnection getConnection() { return new MysqlConnection(); } @Override public ICommand getCommand() { return new MysqlCommand(); } } //抽象工厂实现1 class OracleDataBaseUtils implements IDatabaseUtils { @Override public IConnection getConnection() { return new OracleConnection(); } @Override public ICommand getCommand() { return new OracleCommand(); } }

  

posted @ 2021-05-17 23:59  炼金术士0z  阅读(36)  评论(0编辑  收藏  举报