Published on

ARTS 第11周

Authors

Algorithm

Jewels and Stones

package org.nocoder.leetcode.solution;

/**
 * You're given strings J representing the types of stones that are jewels, and S
 * representing the stones you have.  Each character in S is a type of
 * stone you have.  You want to know how many of the stones you have are also jewels.
 *
 * The letters in J are guaranteed distinct, and all characters in J and S are letters.
 * Letters are case sensitive, so "a" is considered a different type of stone from "A".
 *
 * Example 1:
 *
 * Input: J = "aA", S = "aAAbbbb"
 * Output: 3
 * Example 2:
 *
 * Input: J = "z", S = "ZZ"
 * Output: 0
 * Note:
 *
 * S and J will consist of letters and have length at most 50.
 * The characters in J are distinct.
 * @author jason
 * @date 18/9/16.
 */
public class JewelsAndStones {
    public static int numJewelsInStones(String J, String S) {
        int res = 0;
        if(J == null || "".equals(J) || S == null || "".equals(S)){
            return res;
        }

        String [] jArr = J.split("");
        String [] sArr = S.split("");

        for (int i = 0; i < jArr.length; i++) {
            for (int j = 0; j < sArr.length; j++) {
                if(jArr[i].equals(sArr[j])){
                    ++res;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(numJewelsInStones("ABC", "ABCCDE"));
        System.out.println(numJewelsInStones("", ""));
        System.out.println(numJewelsInStones(null, null));
        System.out.println(numJewelsInStones("ABC", ""));
        System.out.println(numJewelsInStones("", "ABCCDE"));
        System.out.println(numJewelsInStones("abc", "rewqabc"));
    }
}

Review

Strategy Pattern Tutorial with Java Examples

https://dzone.com/articles/design-patterns-strategy

a useful pattern in changing algorithm implementations at runtime, without causing tight coupling.

Defines a set of encapsulated algorithms that can be swapped to carry out a specific behaviour

简单实用的策略模式,Context 由一个 Strategy接口和若干行为方法组成。在运行时向Context传入不同的策略实现类来执行对应的行为方法。

作者使用压缩文件格式rar或者zip为示例,阐述了策略模式的使用场景和具体实现,代码如下:

//Strategy Interface
public interface CompressionStrategy {
  public void compressFiles(ArrayList<File> files);
}
public class ZipCompressionStrategy implements CompressionStrategy {
  public void compressFiles(ArrayList<File> files) {
    //using ZIP approach
  }
}
public class RarCompressionStrategy implements CompressionStrategy {
  public void compressFiles(ArrayList<File> files) {
    //using RAR approach
  }
}
public class CompressionContext {
  private CompressionStrategy strategy;
  //this can be set at runtime by the application preferences
  public void setCompressionStrategy(CompressionStrategy strategy) {
    this.strategy = strategy;
  }
  
  //use the strategy
  public void createArchive(ArrayList<File> files) {
    strategy.compressFiles(files);
  }
}
public class Client {
  public static void main(String[] args) {
    CompressionContext ctx = new CompressionContext();
    //we could assume context is already set by preferences
    ctx.setCompressionStrategy(new ZipCompressionStrategy());
    //get a list of files...
    ctx.createArchive(fileList);
  }
}

Tip

canal-server docker

升级canal到1.1.1版本,本体验证通过,后续准备替换测试和生产环境的canal-server为docker版本。

docker pull canal/canal-server:v1.1.0

docker run 需要挂载logs,其它的参数都可以通过-e的方式来配置

-e canal.auto.scan=false \
-e canal.destinations=test \
-e canal.instance.master.address=127.0.0.1:3306  \
-e canal.instance.dbUsername=canal  \
-e canal.instance.dbPassword=canal  \
-e canal.instance.connectionCharset=UTF-8 \
-e canal.instance.tsdb.enable=true \
-e canal.instance.gtidon=false

https://github.com/alibaba/canal/wiki/Docker-QuickStart

Share

Strategy pattern

https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/strategy.html#id10

策略模式的优点

策略模式提供了对“开闭原则”的完美支持,用户可以在不修改原有系统的基础上选择算法或行为,也可以灵活地增加新的算法或行为。 策略模式提供了管理相关的算法族的办法。 策略模式提供了可以替换继承关系的办法。 使用策略模式可以避免使用多重条件转移语句。

策略模式的缺点

客户端必须知道所有的策略类,并自行决定使用哪一个策略类。 策略模式将造成产生很多策略类,可以通过使用享元模式在一定程度上减少对象的数量

在以下情况下可以使用策略模式

如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。 一个系统需要动态地在几种算法中选择一种。 如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。 不希望客户端知道复杂的、与算法相关的数据结构,在具体策略类中封装算法和相关的数据结构,提高算法的保密性与安全性。