设计模式

设计模式--抽象工厂模式

2019-03-21  本文已影响0人  二妹是只猫

抽象工厂:

定义与类型

UML关系图:

Abstract-Factory.png
抽象工厂主要分为4类:

模式代码

public abstract class Video {
    public abstract void produce();
}
public class PythonVideo extends Video {
    @Override
    public void produce() {
        System.out.println("Python课程视频");
    }
}
public class JavaVideo extends Video {
    @Override
    public void produce() {
        System.out.println("录制Java课程视频");
    }
}
public interface CourseFactory {
    Video getVideo();
    Article getArticle();

}
public class JavaCourseFactory implements CourseFactory {
    @Override
    public Video getVideo() {
        return new JavaVideo();
    }

    @Override
    public Article getArticle() {
        return new JavaArticle();
    }
}
public class PythonCourseFactory implements CourseFactory {
    @Override
    public Video getVideo() {
        return new PythonVideo();
    }

    @Override
    public Article getArticle() {
        return new PythonArticle();
    }
}

源码中的使用:

public interface Statement extends Wrapper, AutoCloseable {
    '省略代码'
}
public interface CallableStatement extends PreparedStatement {
    '省略代码'
}
public class StatementImpl implements Statement {
   '省略代码'
}
package com.mysql.jdbc;
public class CallableStatement extends PreparedStatement implements java.sql.CallableStatement {
   '省略代码'
}
public interface Connection  extends Wrapper, AutoCloseable {

    Statement createStatement() throws SQLException;

    CallableStatement prepareCall(String sql) throws SQLException;
    '省略代码'
public class ConnectionImpl extends ConnectionPropertiesImpl implements Connection {
    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
        this.checkClosed();
        StatementImpl stmt = new StatementImpl(this, this.database);
        stmt.setResultSetType(resultSetType);
        stmt.setResultSetConcurrency(resultSetConcurrency);
        return stmt;
    }
   public java.sql.CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
        if (this.versionMeetsMinimum(5, 0, 0)) {
            CallableStatement cStmt = null;
            if (!this.getCacheCallableStatements()) {
                cStmt = this.parseCallableStatement(sql);
            } else {
                LRUCache var5 = this.parsedCallableStatementCache;
                synchronized(this.parsedCallableStatementCache) {
                    ConnectionImpl.CompoundCacheKey key = new ConnectionImpl.CompoundCacheKey(this.getCatalog(), sql);
                    CallableStatementParamInfo cachedParamInfo = (CallableStatementParamInfo)this.parsedCallableStatementCache.get(key);
                    if (cachedParamInfo != null) {
                        cStmt = CallableStatement.getInstance(this, cachedParamInfo);
                    } else {
                        cStmt = this.parseCallableStatement(sql);
                        cachedParamInfo = cStmt.paramInfo;
                        this.parsedCallableStatementCache.put(key, cachedParamInfo);
                    }
                }
            }

            cStmt.setResultSetType(resultSetType);
            cStmt.setResultSetConcurrency(resultSetConcurrency);
            return cStmt;
        } else {
            throw SQLError.createSQLException("Callable statements not supported.", "S1C00");
        }
    }
}

抽象工厂在实际的开发中运用并不多,主要是在开发工程中很少会出现多个产品种类的情况,大部分情况使用工程模式即可解决

上一篇 下一篇

猜你喜欢

热点阅读