【设计模式】抽象工厂模式

吃可爱长大的小学妹 提交于 2020-02-15 05:28:29
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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!