cool hit counter java design pattern of template pattern and hook method use_Intefrankly

java design pattern of template pattern and hook method use


1. Background of use

   The template method pattern is a great platform for code reuse by moving the invariant behavior to the superclass and removing the duplicate code inside the subclasses to bring out its advantages. When immutable and mutable methods are mixed together in a subclass, the

Methods that don't change will then appear multiple times in the subclass, so that if touching a method needs to be modified then many will need to be modified, although this this should have been thought of at the beginning of the design. This is where the template method pattern comes into play, the

By moving these recurring methods to a single place via the template method pattern, this helps subclasses get out of the tangle of repetitive invariants.

2、thenSpring hit the target JdbcTemplate Using the template schema as an example Explain its superiority;

approachexecute(StatementCallback<T> sc) Methods public methods, The reusable code is encapsulated inside;

The parameter StatementCallback is an interface The interface method is doInStatement() This method is the method that implements the different operations; that is, the different implementations are presented here; the

public <L> L execute(StatementCallback<L> action)
    {
          
        try{ 
                     1. Loading Drivers 
                     2. Establish connection 
                     3. Get Statement stmt
@Override
public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException {
    Assert.notNull(sql, "SQL must not be null");
    Assert.notNull(rse, "ResultSetExtractor must not be null");
    if (logger.isDebugEnabled()) {
        logger.debug("Executing SQL query [" + sql + "]");
    }
    // anonymous internal class
    class QueryStatementCallback implements StatementCallback<T>, SqlProvider {
        @Override
        public T doInStatement(Statement stmt) throws SQLException {
            ResultSet rs = null;
            try {
                rs = stmt.executeQuery(sql);
                ResultSet rsToUse = rs;
                if (nativeJdbcExtractor != null) {
                    rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
                }
                return rse.extractData(rsToUse);
            }
            finally {
                JdbcUtils.closeResultSet(rs);
            }
        }
        @Override
        public String getSql() {
            return sql;
        }
    }
    // Methods of real implementation
    return execute(new QueryStatementCallback());
}

invokequery When the method will implementexecute approach, This method is a template method, Then because the method internally calls the incomingStatementCallback interface doInStatement method But this method can be customised for use in the query method by passing in an anonymous internal class; fully consistent with the use of the template pattern.

1. Use the hook method to control different behaviors of the template

The following example of a car illustrates the use of the hook method.

public abstract class HummerModel {
     protected abstract void start();  //Start
     protected abstract void stop();   //Stop
     protected abstract void alarm();  //Hooter
protected abstract void engineBoom();  / Boom
     final public void run() { // The car always runs
        this.start();
        this.engineBoom();
         if(this.isAlarm()) {//If you want it to bark, it will bark, if not, it will not bark        
            this.alarm();
        }
        this.stop();
    }
     protected boolean isAlarm() { //we added a judgment method that returns true by default
        return true;
    }
}
public class HummerH1 extends HummerModel {
 
     private boolean alarmFlag = true;  // Judgment marker
    @Override
    public void start() {
        System.out.println("H1 unleash……");
    }
 
    @Override
    public void stop() {
        System.out.println("H1 stop……");
    }
 
    @Override
    public void alarm() {
        System.out.println("H1 hoot……");
    }
 
    @Override
    public void engineBoom() {
        System.out.println("H1 boom (sound of explosion)……");
    }
    
    @Override
     protected boolean isAlarm() { // override the isAlarm method to return a judgment token
        return this.alarmFlag;
    }
    
     public void setAlarm(boolean isAlarm) { // Set the judgment flag
        this.alarmFlag = isAlarm;
    }
    
}
 In this code, we add the judgment token to the template method, and then the subclass provides a public interface setAlarm to the outside world to set this judgment token, which acts as a switch to make it ture and false if you want.
 This isAlarm method is commonly known as the hook method. The template method pattern with hook methods is perfect, making our control behavior more proactive and flexible.
          
           
            
             
              
               
                
                 
                 
Recommended>>
1、Which is the best bitcoin wallet 2018 Bitcoin Wallet Latest Rankings
2、Python Language Programming 61 The Basics of Documentation
3、A man from 2030 who cant even take a lie detector You might not think of that last point without saying it
4、Artificial intelligence will revolutionize education deputy education minister
5、Deciphering The Blockchain Technology And The Quantum Wealth View For You

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号