@Override publicvoidwork(){ //挖金。 LOGGER.info("{} digs for gold.", name()); }
@Override public String name(){ //矮人淘金者 return"Dwarf gold digger"; } }
/** * DwarvenTunnelDigger is one of the goldmine subsystems. DwarvenTunnelDigger(矮人隧道挖掘者)是金矿子系统之一。 */ publicclassDwarvenTunnelDiggerextendsDwarvenMineWorker{
@Override publicvoidwork(){ //创造另一个有希望的隧道 LOGGER.info("{} creates another promising tunnel.", name()); }
@Override public String name(){ //矮人隧道挖掘机 return"Dwarven tunnel digger"; } }
/** * DwarvenCartOperator is one of the goldmine subsystems. DwarvenCartOperator(矮人推车运营商)是金矿子系统之一。 */ publicclassDwarvenCartOperatorextendsDwarvenMineWorker{
@Override publicvoidwork(){ //将金块从矿山中移出 LOGGER.info("{} moves gold chunks out of the mine.", name()); }
@Override public String name(){ //矮推车操作员 return"Dwarf cart operator"; } }
/** * DwarvenGoldmineFacade provides a single interface through which users can operate the * subsystems. * * <p>This makes the goldmine easier to operate and cuts the dependencies from the goldmine user to * the subsystems. DwarvenGoldmineFacade提供了一个界面,用户可以通过该界面来操作子系统。 <p>这使金矿更易于操作,并减少了金矿用户与子系统之间的依赖关系。 */ publicclassDwarvenGoldmineFacade{
privatefinal List<DwarvenMineWorker> workers;
/** * Constructor. */ publicDwarvenGoldmineFacade(){ workers = List.of( new DwarvenGoldDigger(), new DwarvenCartOperator(), new DwarvenTunnelDigger()); }
/** * The Facade design pattern is often used when a system is very complex or difficult to understand * because the system has a large number of interdependent classes or its source code is * unavailable. This pattern hides the complexities of the larger system and provides a simpler * interface to the client. It typically involves a single wrapper class which contains a set of * members required by client. These members access the system on behalf of the facade client and * hide the implementation details. * * <p>In this example the Facade is ({@link DwarvenGoldmineFacade}) and it provides a simpler * interface to the goldmine subsystem. 当系统非常复杂或难以理解时,通常会使用Facade设计模式,因为该系统具有大量相互依赖的类,或者其源代码不可用。 这种模式隐藏了较大系统的复杂性,并为客户端提供了更简单的界面。 它通常涉及单个包装器类,其中包含客户端所需的一组成员。 这些成员代表外观客户端访问系统,并隐藏实现细节。 <p>在此示例中,外观为({@link DwarvenGoldmineFacade}),它为金矿子系统提供了更简单的界面。 */ publicclassApp{
/** * Program entry point. * * @param args command line args */ publicstaticvoidmain(String[] args){ var facade = new DwarvenGoldmineFacade(); facade.startNewDay(); facade.digOutGold(); facade.endDay(); } }
应用场景
spring jdbc中的外观模式
查看 org.springframework.jdbc.support.JdbcUtils
publicabstractclassJdbcUtils{ publicstaticvoidcloseConnection(Connection con){ if (con != null) { try { con.close(); } catch (SQLException ex) { logger.debug("Could not close JDBC Connection", ex); } catch (Throwable ex) { // We don't trust the JDBC driver: It might throw RuntimeException or Error. logger.debug("Unexpected exception on closing JDBC Connection", ex); } } }
publicstatic Object getResultSetValue(ResultSet rs, int index, Class<?> requiredType)throws SQLException { if (requiredType == null) { return getResultSetValue(rs, index); }
Object value = null; boolean wasNullCheck = false;
// Explicitly extract typed value, as far as possible. if (String.class.equals(requiredType)) { value = rs.getString(index); } elseif (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) { value = rs.getBoolean(index); wasNullCheck = true; } elseif (byte.class.equals(requiredType) || Byte.class.equals(requiredType)) { value = rs.getByte(index); wasNullCheck = true; } elseif (short.class.equals(requiredType) || Short.class.equals(requiredType)) { value = rs.getShort(index); wasNullCheck = true; } elseif (int.class.equals(requiredType) || Integer.class.equals(requiredType)) { value = rs.getInt(index); wasNullCheck = true; } elseif (long.class.equals(requiredType) || Long.class.equals(requiredType)) { value = rs.getLong(index); wasNullCheck = true; } elseif (float.class.equals(requiredType) || Float.class.equals(requiredType)) { value = rs.getFloat(index); wasNullCheck = true; } elseif (double.class.equals(requiredType) || Double.class.equals(requiredType) || Number.class.equals(requiredType)) { value = rs.getDouble(index); wasNullCheck = true; } elseif (byte[].class.equals(requiredType)) { value = rs.getBytes(index); } elseif (java.sql.Date.class.equals(requiredType)) { value = rs.getDate(index); } elseif (java.sql.Time.class.equals(requiredType)) { value = rs.getTime(index); } elseif (java.sql.Timestamp.class.equals(requiredType) || java.util.Date.class.equals(requiredType)) { value = rs.getTimestamp(index); } elseif (BigDecimal.class.equals(requiredType)) { value = rs.getBigDecimal(index); } elseif (Blob.class.equals(requiredType)) { value = rs.getBlob(index); } elseif (Clob.class.equals(requiredType)) { value = rs.getClob(index); } else { // Some unknown type desired -> rely on getObject. value = getResultSetValue(rs, index); }
if (wasNullCheck && value != null && rs.wasNull()) { value = null; } return value; } // ...省略... }
该工具类主要是对原生的 jdbc 进行了封装
Mybatis中的外观模式
查看 org.apache.ibatis.session.Configuration 类中以 new 开头的方法