简单工厂模式

抽象产品角色 Apple
具体产品角色 IPhone、Mac
工厂类 AppleSimpleFactory

public interface Apple {
    void createProduct();
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class AppleSimpleFactory {

    Apple createProductFactory(String name){
        if("mac".equals(name)){
            return new Mac();
        }else if("iphone".equals(name)){
            return new IPhone();
        }else
            return null;
    }
}
public class Test {
    public static void main(String[] args) {
        AppleSimpleFactory appleSimpleFactory = new AppleSimpleFactory();
        Apple apple = appleSimpleFactory.createProductFactory("mac");
        System.out.println(apple);
    }
}

工厂方法模式

工厂方法模式也叫工厂模式、虚拟构造器模式、多态工厂模式,工厂方法模式中一个工厂生产一个产品

抽象产品角色 Apple
具体产品角色 Mac 、IPhone
抽象工厂角色 AppleFactory
具体工厂角色 MacFactory 、IPhoneFactory

public interface Apple {
        void createProduct();
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public interface AppleFactory {
    Apple createProduct();
}
public class IPhoneFactory implements AppleFactory {
    @Override
    public Apple createProduct() {
        return new IPhone();
    }
}
public class MacFactory implements AppleFactory {

    @Override
    public Apple createProduct() {
        return new Mac();
    }
}
public class Test {
    public static void main(String[] args) {
        AppleFactory macFactory = new MacFactory();
        Apple mac = macFactory.createProduct();
        System.out.println(mac);
        AppleFactory iphoneFactory = new IPhoneFactory();
        Apple iphone = iphoneFactory.createProduct();
        System.out.println(iphone);
    }
}

抽象工厂模式

抽象产品角色 Apple
具体产品角色 Mac、IPhone、IPad
抽象工厂角色 AppleAbstractFactory
具体工厂角色 YanTaiFctory、TaiwanFactory

public interface Apple {
        void createProduct();
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public class IPad implements Apple {
    @Override
    public void createProduct() {
        System.out.println("iPad ");
    }
}
public interface AppleAbstractFactory {

    void createMac();

    void createIPhone();

    void createIPad();
}
public class YanTaiFctory implements AppleAbstractFactory {
    @Override
    public void createMac() {
        System.out.println("MacBook Pro");
    }

    @Override
    public void createIPhone() {
        System.out.println("iPhone X");
    }

    @Override
    public void createIPad() {
        System.out.println("iPad mini4");
    }
}
public class TaiwanFactory implements AppleAbstractFactory {

    @Override
    public void createMac() {
        System.out.println("MacBook Air");
    }

    @Override
    public void createIPhone() {
        System.out.println("iPhone 8s");
    }

    @Override
    public void createIPad() {
        System.out.println("iPad mini2");
    }
}
public class Test {
    public static void main(String[] args) {
        AppleAbstractFactory appleAbstractFactory = new TaiwanFactory();
        appleAbstractFactory.createIPad();
        appleAbstractFactory.createIPhone();
        appleAbstractFactory.createMac();
    }
}

2018-08-17 增加线程安全的懒汉模式

单例模式即只有一个实例,实现分为懒汉模式和饿汉模式。

饿汉模式

/**
 * 单例模式_饿汉模式
 * 类加载的时候完成初始化,因此加载速度相对较慢,但运行是获取对象的速度相对较快
 * 线程安全
 * Created by zhangguoli on 09-18-018.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */
public class HungrySingleton {

    //① 构造方法私有化,外部不可以直接创建对象
    private HungrySingleton () {

    }

    //② 创建类的唯一实例,使用private static修饰
    private static HungrySingleton instance = new HungrySingleton ();

    //③ 提供统一对外获取实例的方法
    public static HungrySingleton getInstance() {
        return instance;
    }
}

懒汉模式

- 阅读剩余部分 -

二分查找的前提是数组已经是排好序的了

递归方式

 /**
     * 二分查找_递归
     *
     * @param array 排序好的数组
     * @param low   数组下标
     * @param high  数组下标
     * @param key   要查找的值
     * @return 返回数组下标, 找不到返回-1
     */
    static int binarySearchRecursion(int array[], int low, int high, int key) {
        if (low > high)
            return -1;
        int mid = (low + high) / 2;
        if (array[mid] > key)
            return binarySearchRecursion(array, low, mid - 1, key);
        if (array[mid] < key)
            return binarySearchRecursion(array, mid + 1, high, key);
        return mid;
    }

非递归方式

- 阅读剩余部分 -

实现Runnable接口并编写run()方法

public class FirstThread implements Runnable {

    @Override
    public void run() {
        System.out.println("implement the Runnable interface and write the run () method");
    }
}

new Thread(new FirstThread().start())

继承Thread类

Thread类本身就是实现的Runnable接口: Thread implements Runnable

public class SecondThread extends Thread {

    @Override
    public void run() {
        System.out.println("second thread run...");
    }
}
new Thread(new SecondThread().start())

实现Callable接口来创建Thread线程

- 阅读剩余部分 -

package cn.gl.work;

/**
 * Created by guoli on 2017/9/9 0009.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu

 */
public class StringIndexOf {

    public int IndexOf(String source, String target) {
        char[] sources = source.toCharArray();
        char[] targets = target.toCharArray();
        int sourceCount = sources.length;
        int targetCount = targets.length;
        char first = targets[0];
        int max = sourceCount - targetCount;
        for (int i = 0; i <= max; i++) {
            //1. 子符第一个字符与母串第一个字符相等
            if (sources[i] != first) {
                while (++i < max && sources[i] != first) {
                    ;
                }
            }
            //2、匹配2到子串
            if (i < max) {//未到最后
                int j = i + 1;
                int end = j + targetCount - 1;//注意end位置
                for (int k = 1; j < end && sources[j] == targets[k]; j++, k++) {
                    ;//子串和原串截取的部分在循环对比
                }
                if (j == end) {//找到了
                    return i;
                }
            }
        }
        return -1;
    }
}

执行顺序如下所示,原因太长就不写了

(8) SELECT <select_list>
(9) DISTINCT <select_list>
(1) FROM <left_table>
(3) <join_type> JOIN <right_table>
(2) ON <join_condition>
(4) WHERE <where_condition>
(5) GROUP BY <group_by_list>
(6) WITH {CUBE|ROLLUP}
(7) HAVING <having_condition>
(10) ORDER BY <order_by_list>
(11) LIMIT <limit_number>

这里看到一个SQL题目:查询学生总成绩的前三名,给出MySQL和Oracle的写法

namecoursegrade
nickyuwen88
jackshuxue95
nickshuxue68
jackyuwen96
susanyuwen100
susanshuxue95
leeyuwen100
leeshuxue95
zhangyuwen58
zhangshuxue91

MySQL使用LIMIT关键字:

SELECT name
FROM student_grade
GROUP BY name
ORDER BY sum(grade) DESC
LIMIT 0,3

Oracle使用ROWNUM关键字

select name FROM(
    SELECT name
    FROM student_grade
    GROUP BY name
    ORDER BY sum(grade) DESC
)WHERE rownum <=3;