+ * 广度优先遍历,从V0出发,访问V0的各个未曾访问的邻接点W1,W2,…,Wk;然后,依次从W1,W2,…,Wk出发访问各自未被访问的邻接点;
+ *
+ * @param begin
+ */
+ public void BFS(Integer begin) {
+ isVisited = new boolean[graph.length];
+ for (int i = 0; i < isVisited.length; i++) {
+ isVisited[i] = false;
+ }
+
+ Queue
+ * 红黑树的定义如下:
+ * 1. 任何节点要不是黑色要不是红色
+ * 2. 根节点是黑色节点
+ * 3. 红节点的两个子节点都是黑色节点
+ * 4. 空节点是黑色节点
+ * 5. 任何一个节点下面遍历其子孙的叶子节点,经过的黑色节点的个数必须相等。
+ *
+ * 红黑树也是通过第5点进行维持平衡的,而为了维持平衡,需要对树进行调整,即进行左旋,右旋。
+
+ */
+public class RedBlackTree {
+
+ Node root;
+
+ public RedBlackTree() {
+ }
+
+ public RedBlackTree(int value) {
+ root = new Node(value);
+ }
+
+ public Node find(int value) {
+ if (root == null) {
+ throw new RuntimeException("树是空的");
+ }
+
+ Node currentNode = root;
+ while (currentNode != null && currentNode.getValue() != value) {
+ if (currentNode.getValue() < value) {
+ currentNode = currentNode.getLeft();
+ } else {
+ currentNode = currentNode.getRight();
+ }
+ }
+
+ return currentNode;
+ }
+
+
+ public void insertNode(int value) {
+ Node node = new Node(value);
+ insertNode(node);
+
+ }
+
+ /**
+ * 插入节点
+ * 该方法首先找到要插入的位置,然后设置插入的节点为红色节点
+ * 然后因为可能会破坏平衡,因此需要进行平衡调整
+ *
+ * @param node
+ */
+ public void insertNode(Node node) {
+ int cmp;
+ Node y = null;
+ Node x = this.root;
+
+ while (x != null) {
+ y = x;
+ cmp = node.getValue() - x.getValue();
+ if (cmp < 0) {
+ x = x.left;
+ } else {
+ x = x.right;
+ }
+ }
+
+ node.parent = y;
+ if (y != null) {
+ cmp = node.getValue() - y.getValue();
+ if (cmp < 0) {
+ y.left = node;
+ } else {
+ y.right = node;
+ }
+ } else {
+ this.root = node;
+ }
+
+ node.isRed = true;
+ insertFixUp(node);
+
+ }
+
+ /**
+ * 插入修复: 新插入的节点是红色节点,插入修复操作如果遇到父节点的颜色为黑色则修复结束
+ * 也就是说只有在父节点为红色节点的时候才需要插入修复操作
+ * 插入修复操作分为3种情况,
+ * 1. 叔叔节点也为红色节点
+ * 2. 叔叔节点为空,且祖父节点,父节点与新节点在一个斜线上
+ * 3. 叔叔节点为空,且祖父节点,父节点与新节点不在一个斜线上
+ *
+ *
+ * 解决办法:对于第一种,只需要将祖父节点与父节点以及叔叔节点的颜色对调即可。
+ * 即原祖父节点是黑色,现在变成红色,父节点与叔叔节点都变成黑色。
+ * 对于第二种,我们将新插入的节点为C,父节点为B,祖父节点为A.
+ * 如果BC都是左节点,要现将A右旋,然后调整B与A的颜色,即B变成黑色,A变成红色
+ * 如果BC都是右节点,要现将A左旋,然后调整B与A的颜色,即B变成黑色,A变成红色
+ * 对于第三种,我们将新插入的节点为C,父节点为B,祖父节点为A.
+ * 如果C为右节点,B为左节点,要先将B左旋,然后就变成第二种的情况
+ * 如果C为左节点,B为右节点,要先B右旋,然后就变成第二种的情况
+ *
+ * @param node
+ */
+ private void insertFixUp(Node node) {
+ Node parent, grandParent, uncle;
+ while ((parent = parentOf(node)) != null && parent.isRed()) {
+ grandParent = parentOf(node);
+ //如果父节点是祖父节点的左孩子
+ if (parent == grandParent.left) {
+ uncle = grandParent.right;
+ //第一种情况
+ if ((uncle != null) && uncle.isRed()) {
+ uncle.makeBlack();
+ parent.makeBlack();
+ grandParent.makeRed();
+ node = grandParent;
+ continue;
+ }
+ //将第三种情况变成第二种情况
+ if (parent.right == node) {
+ Node tmp;
+ rotateLeft(parent);
+ tmp = parent;
+ parent = node;
+ node = tmp;
+ }
+ parent.makeBlack();
+ grandParent.makeRed();
+ rotateRight(grandParent);
+ } else {
+ uncle = grandParent.left;
+ if ((uncle != null) && uncle.isRed()) {
+ uncle.makeBlack();
+ parent.makeBlack();
+ grandParent.makeRed();
+ node = grandParent;
+ continue;
+ }
+
+ if (parent.left == node) {
+ Node tmp;
+ rotateRight(parent);
+ tmp = parent;
+ parent = node;
+ node = tmp;
+ }
+
+ parent.makeBlack();
+ grandParent.makeRed();
+ rotateLeft(grandParent);
+
+ }
+
+ }
+
+ root.makeBlack();
+ }
+
+
+ /**
+ * 对红黑树的节点(y)进行右旋转
+ *
+ * 右旋示意图(对节点y进行左旋):
+ * py py
+ * / /
+ * y x
+ * / \ --(右旋)-. / \ #
+ * x ry lx y
+ * / \ / \ #
+ * lx rx rx ry
+ *
+ * @param y 待旋转的节点
+ */
+ private void rotateRight(Node y) {
+
+ Node x = y.left;
+
+ // 将 “x的右孩子” 设为 “y的左孩子”;
+ // 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲”
+ y.left = x.right;
+ if (x.right != null)
+ x.right.parent = y;
+
+ // 将 “y的父亲” 设为 “x的父亲”
+ x.parent = y.parent;
+
+ if (y.parent == null) {
+ this.root = x; // 如果 “y的父亲” 是空节点,则将x设为根节点
+ } else {
+ if (y == y.parent.right)
+ y.parent.right = x; // 如果 y是它父节点的右孩子,则将x设为“y的父节点的右孩子”
+ else
+ y.parent.left = x; // (y是它父节点的左孩子) 将x设为“x的父节点的左孩子”
+ }
+
+ // 将 “y” 设为 “x的右孩子”
+ x.right = y;
+
+ // 将 “y的父节点” 设为 “x”
+ y.parent = x;
+ }
+
+ /**
+ *
+ * 对红黑树的节点(x)进行左旋转
+ *
+ * 左旋示意图(对节点x进行左旋):
+ * px px
+ * / /
+ * x y
+ * / \ --(左旋)-. / \ #
+ * lx y x ry
+ * / \ / \
+ * ly ry lx ly
+ *
+ * @param x 待旋转的节点
+ */
+ private void rotateLeft(Node x) {
+ Node y = x.getRight();
+ x.right = y.left;
+ if (y.left != null) {
+ y.left.parent = x;
+ }
+ y.parent = x.parent;
+ if (x.parent == null) {
+ root = y;
+ }else{
+ if (x.parent.left == x) {
+ x.parent.left = y;
+ }else{
+ x.parent.right = y;
+ }
+ }
+ y.left = x;
+ x.parent = y;
+ }
+
+ private Node parentOf(Node node) {
+ return node != null ? node.parent : null;
+ }
+
+
+ static class Node {
+ private int value;
+ private Node parent;
+ private boolean isRed;
+ private Node left;
+ private Node right;
+
+ public Node() {
+
+ }
+
+ public Node(int value) {
+ this.value = value;
+
+ }
+
+ public Node(int value, boolean isRed) {
+ this.value = value;
+ this.isRed = isRed;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ public Node getParent() {
+ return parent;
+ }
+
+ public void setParent(Node parent) {
+ this.parent = parent;
+ }
+
+ public boolean isRed() {
+ return isRed;
+ }
+
+ public boolean isBlack() {
+ return !isRed();
+ }
+
+ public Node getLeft() {
+ return left;
+ }
+
+ public void setLeft(Node left) {
+ this.left = left;
+ }
+
+ public Node getRight() {
+ return right;
+ }
+
+ public void setRight(Node right) {
+ this.right = right;
+ }
+
+ public void makeRed() {
+ isRed = true;
+ }
+
+ public void makeBlack() {
+ isRed = false;
+ }
+ }
+}
diff --git a/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java b/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java
new file mode 100644
index 0000000..1de9643
--- /dev/null
+++ b/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java
@@ -0,0 +1,109 @@
+package cn.byhieg.algorithmtutorial;
+
+/**
+ * Created by byhieg on 17/5/2.
+ * Mail to byhieg@gmail.com
+ */
+public class SingleLinkList {
+
+
+ public Node head;
+
+ /**
+ * 在当前链表尾部插入一个节点
+ *
+ * @param data
+ * @return
+ */
+ public Node insertFromTail(int data) {
+ Node cur = getHead();
+ Node node = new Node(data);
+ if (cur == null) {
+ head = node;
+ return head;
+ } else {
+ while (cur.next != null) {
+ cur = cur.next;
+ }
+ cur.next = node;
+ }
+ return cur;
+ }
+
+ /**
+ * 在当前链表头部插入一个节点
+ *
+ * @param data
+ * @return
+ */
+ public Node insertFromHead(int data) {
+ Node node = new Node(data);
+ node.next = head;
+ head = node;
+ return head;
+ }
+
+ /**
+ * 反转链表的非递归实现
+ * @return
+ */
+ public Node reverseLinkList() {
+ if (head == null) {
+ return head;
+ }
+ Node reverseHead = null;
+ Node cur = head;
+ Node prev = null;
+ while (cur != null) {
+ Node next = cur.next;
+ if (next == null) {
+ reverseHead = cur;
+ }
+ cur.next = prev;
+ prev = cur;
+ cur = next;
+ }
+ return reverseHead;
+ }
+
+
+ /**
+ * 反转链表的递归实现
+ * @return
+ */
+ public Node reverseLinkList(Node head){
+ if (head == null || head.next == null) {
+ return head;
+ }
+ Node newNode = reverseLinkList(head.next);
+ head.next.next = head;
+ head.next = null;
+ return newNode;
+ }
+
+
+
+ /**
+ * 打印链表
+ */
+ public void printLinkList(Node head) {
+ Node cur = head;
+ while (cur != null) {
+ System.out.print(cur.data + " ");
+ cur = cur.next;
+ }
+ }
+
+ public Node getHead() {
+ return head;
+ }
+
+ public static class Node {
+ public int data;
+ public Node next;
+
+ public Node(int data) {
+ this.data = data;
+ }
+ }
+}
diff --git a/src/main/java/cn/byhieg/algorithmtutorial/Sort.java b/src/main/java/cn/byhieg/algorithmtutorial/Sort.java
new file mode 100644
index 0000000..6794e42
--- /dev/null
+++ b/src/main/java/cn/byhieg/algorithmtutorial/Sort.java
@@ -0,0 +1,320 @@
+package cn.byhieg.algorithmtutorial;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * Created by shiqifeng on 2017/3/28.
+ * Mail byhieg@gmail.com
+ */
+public class Sort {
+
+ /**
+ * 选择排序,每一轮排序,选择数组中数字最小的那一个放到指定的位置上。
+ * 时间复杂度o(n^2),无论数组顺序如何都要选择一个最小的值,因为数组的是否有序,不影响时间复杂度
+ * 空间复杂度o(1)
+ * 不稳定排序
+ * @param nums
+ */
+ public void chooseSort(int[] nums) {
+ int length = nums.length;
+ for (int i = 0; i < length; i++) {
+ int min = i;//申请额外的空间o(1)
+ for (int j = i + 1; j < length; j++) {
+ if (nums[min] > nums[j]) {
+ min = j;
+ }
+ }
+ //将最小的下标代表的数与i位置的进行交换
+ int tmp = nums[i];
+ nums[i] = nums[min];
+ nums[min] = tmp;
+ }
+ }
+
+ /**
+ * 直接插入排序,每一轮排序,都是在i坐标之前,包括i坐标的序列是有序的,但是并不是最终的排序位置。
+ * 时间复杂度o(n^2),对于第二重循环,只会在非有序的环境下才会执行每个元素后移,因此针对有序的数组,时间复杂度最好的情况是o(N)。
+ * 空间复杂度o(1)
+ * 稳定排序
+ * @param nums
+ */
+ public void insertDirectlySort(int[] nums) {
+ int length = nums.length;
+ for (int i = 1; i < length; i++) {
+ for (int j = i; j > 0; j--) {
+ //这一步导致该算法是稳定排序
+ if (nums[j] < nums[j - 1]) {
+ int tmp = nums[j - 1];
+ nums[j - 1] = nums[j];
+ nums[j] = tmp;
+ }
+ }
+ }
+ }
+
+ /**
+ * 折半插入排序,针对直接排序而言,每一个要插入的元素都是插入在有序的数组中,因此,只需要查找到插入的位置即可,查找的方式利用二分查找
+ * 时间复杂度和直接插入是一样的,只是快在了查找的过程中,还是o(N^2),最好的环境下是o(N)
+ * 空间复杂度还是o(1)
+ *
+ * @param nums
+ */
+ public void insertBinarySort(int[] nums) {
+ int length = nums.length;
+ for (int i = 1; i < length; i++) {
+ int tmp = nums[i];
+ int low = 0;
+ int high = i - 1;
+ while (low <= high) {
+ int mid = (low + high) / 2;
+ if (tmp < nums[mid]) {
+ high = mid - 1;
+ } else {
+ low = mid + 1;
+ }
+ }
+
+ for (int j = i; j >= low + 1; j--) {
+ nums[j] = nums[j - 1];
+ }
+ nums[low] = tmp;
+ }
+ }
+
+ /**
+ * 冒泡排序,每i轮排序,就是不断交换两个元素,直到将最大的元素放到n - i的位置上
+ * 这种实现是按照算法定义的,但是效率是最低的
+ * 时间复杂度o(n^2)
+ * 空间复杂度o(1)
+ * 稳定排序
+ * @param nums
+ */
+ public void bubbleSort1(int[] nums) {
+ int length = nums.length;
+ for (int i = 1; i < length; i++) {
+ for (int j = 0; j < length - i; j++) {
+ //这一步导致该算法是稳定排序
+ if (nums[j] > nums[j + 1]) {
+ int tmp = nums[j];
+ nums[j] = nums[j + 1];
+ nums[j + 1] = tmp;
+ }
+ }
+ }
+ }
+
+ /**
+ * 冒泡排序,高效率实现,因为只需要用一个flag变量来记录本次的排序,是否修改
+ * 如果没有修改,说明已经有序
+ *
+ * @param nums
+ */
+ public void bubbleSort2(int[] nums) {
+ int length = nums.length;
+ boolean flag = true;
+ while (flag) {
+ flag = false;
+ for (int j = 0; j < length - 1; j++) {
+ if (nums[j] > nums[j + 1]) {
+ int tmp = nums[j];
+ nums[j] = nums[j + 1];
+ nums[j + 1] = tmp;
+ flag = true;
+ }
+ }
+ length--;
+ }
+ }
+
+ /**
+ * 归并排序,将数组一分为二,对于每一子数组继续进行上述步骤,直到子数组只有1个元素,那么自然是有序的。
+ * 然后不断合并两个数组,直到合并到整个数组。
+ * 时间复杂度o(NlgN)
+ * 空间复杂度o(N)
+ * @param nums
+ */
+ public void mergeSort(int[] nums) {
+ int length = nums.length;
+ int low = 0;
+ int high = length - 1;
+ realSort(nums, low, high);
+ }
+
+ /**
+ * 归并排序真正的sort函数
+ * @param nums 待排序的数组
+ * @param low 最低位
+ * @param high 最高位
+ */
+ private void realSort(int[] nums, int low, int high) {
+ int mid = (low + high) / 2;
+ if (low < high) {
+ realSort(nums, low, mid);
+ realSort(nums, mid + 1, high);
+ realMerge(nums, low, mid, high);
+ }
+ }
+
+ private void realMerge(int[] nums, int low, int mid, int high) {
+ int[] tmpNums = new int[high - low + 1];
+ int leftPoint = low;
+ int rightPoint = mid + 1;
+ int index = 0;
+
+ while (leftPoint <= mid && rightPoint <= high) {
+ if (nums[leftPoint] < nums[rightPoint]) {
+ tmpNums[index++] = nums[leftPoint++];
+ }else{
+ tmpNums[index++] = nums[rightPoint++];
+ }
+ }
+
+ while (leftPoint <= mid) {
+ tmpNums[index++] = nums[leftPoint++];
+ }
+ while (rightPoint <= high) {
+ tmpNums[index++] = nums[rightPoint++];
+ }
+
+ System.arraycopy(tmpNums, 0, nums, low, tmpNums.length);
+ }
+
+ /**
+ * 快速排序,选定一个切分元素,每一轮排序后,都保证切分元素之前的元素都小于切分元素,切分元素之后的元素都大于切分元素
+ * 时间复杂度o(NlgN)
+ * 空间复杂度o(lgN)
+ * 不稳定排序
+ * @param nums
+ */
+ public void quickSort(int[] nums) {
+ int low = 0;
+ int high = nums.length - 1;
+ sort(nums, low, high);
+ }
+
+ /**
+ * 快速排序的递归实现
+ *
+ * @param nums
+ * @param low
+ * @param high
+ */
+ public void sort(int[] nums, int low, int high) {
+ if (low >= high) return;
+ int j = partition(nums, low, high);
+ sort(nums, low, j - 1);
+ sort(nums, j + 1, high);
+ }
+
+ /**
+ * 快速排序的辅助方法,来对排序的数组,进行切分,
+ *
+ * @param nums
+ * @param low
+ * @param high
+ * @return
+ */
+ public int partition(int[] nums, int low, int high) {
+ int i = low;
+ int j = high;
+ int x = nums[i];
+ while (i < j) {
+ //从右向左找到nums[j]小于x的元素
+ while (i < j && nums[j] >= x) j--;
+ if (i < j) {
+ nums[i] = nums[j];
+ i++;
+ }
+
+ //从左向右找到nums[i]大于x的元素
+ while (i < j && nums[i] <= x) i++;
+ if (i < j) {
+ nums[j] = nums[i];
+ j--;
+ }
+ }
+ nums[i] = x;
+ return i;
+ }
+
+
+ /**
+ * 堆排序,建立一个小顶堆,小顶堆满足父节点比两个子节点的值要小
+ * 堆的性质满足:1. 只能在堆顶删除元素
+ * 2. 只能在堆的最后一位存元素。
+ * 3. 堆的存储利用数组,满足i节点是父节点,则子节点是2 * i+ 1,2 * i + 2
+ * 4. 堆的两种建方法,第一种是从上到下,@see sink(),第二种是从下到上 @see swim
+ * 5. 堆排序是指在弄好的堆中,输出第一个元素,然后将最后一个元素与第一个元素互换,换后调用sink,找到自己的位置后,在重复这个步骤,就输出一个有序的堆
+ * 6. 如果要生序就需要大顶堆,要降序就需要小顶堆。
+ * 时间复杂度:o(NlgN)
+ * 空间复杂度: o(1)
+ * 这是小顶堆的排序,所以nums数组最后是降序的
+ * 不稳定,不稳定的原因在建堆的时候,就可能把相同元素的位置换了,比如两个相同元素在不同的子树上
+ * @param nums
+ */
+ public void heapSort(int[] nums) {
+ int length = nums.length;
+// for (int i = 0; i < length; i++) {
+// swim(nums, i);
+// }
+ //只能从前一半开始sink
+ for (int i = length / 2 ; i >= 0;i--) {
+ sink(nums,i,length);
+ }
+ while (length > 0) {
+ int temp = nums[0];
+ nums[0] = nums[length - 1];
+ nums[length - 1] = temp;
+ length--;
+ sink(nums, 0, length);
+ }
+ }
+
+ /**
+ * 将i放入对堆中,i的父节点是(i - 1)/ 2,父节点的值是小于他的两个子节点的
+ * i节点放入后,要向上移动,如果父节点比i节点的值大,则i节点要继续上移。
+ *
+ * @param nums
+ * @param i
+ */
+ private void swim(int nums[], int i) {
+ while (i > 0) {
+ int father = (i - 1) / 2;
+ if (nums[father] > nums[i]) {
+ int temp = nums[father];
+ nums[father] = nums[i];
+ nums[i] = temp;
+ }
+ i = father;
+ }
+ }
+
+
+ /**
+ * 从i节点由上到下开始调整,i节点的子节点为2*i + 1, 2 * i + 2
+ * i节点要向下移动,直到满足i节点小于两个子节点
+ *
+ * @param nums nums[] 数组
+ * @param i i节点
+ */
+ public void sink(int [] nums, int i,int n) {
+ int son = 2 * i + 1;
+ while (son <= n - 1) {
+ if (son < n - 1 && nums[son] > nums[son + 1]) son++;
+ if (nums[i] > nums[son]) {
+ int temp = nums[i];
+ nums[i] = nums[son];
+ nums[son] = temp;
+ i = son;
+ son = 2 * i + 1;
+ }else{
+ break;
+ }
+ }
+ }
+
+
+
+}
diff --git a/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java b/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java
new file mode 100644
index 0000000..680408b
--- /dev/null
+++ b/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java
@@ -0,0 +1,14 @@
+package cn.byhieg.bitoperatetutorial;
+
+/**
+ * Created by byhieg on 2017/6/27.
+ * Mail to byhieg@gmail.com
+ */
+public class BitOperate {
+
+
+ public String getRightestOne(int n){
+ int res = n & (~n + 1);
+ return Integer.toBinaryString(res);
+ }
+}
diff --git a/src/main/java/cn/byhieg/collectiontutorial/maptutorial/HashMapExample.java b/src/main/java/cn/byhieg/collectiontutorial/maptutorial/HashMapExample.java
index e3a51ac..9e55ef1 100644
--- a/src/main/java/cn/byhieg/collectiontutorial/maptutorial/HashMapExample.java
+++ b/src/main/java/cn/byhieg/collectiontutorial/maptutorial/HashMapExample.java
@@ -1,8 +1,6 @@
package cn.byhieg.collectiontutorial.maptutorial;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.*;
/**
* Created by shiqifeng on 2017/2/24.
diff --git a/src/main/java/cn/byhieg/collectiontutorial/maptutorial/TreeMapExample.java b/src/main/java/cn/byhieg/collectiontutorial/maptutorial/TreeMapExample.java
index b791cbc..136999d 100644
--- a/src/main/java/cn/byhieg/collectiontutorial/maptutorial/TreeMapExample.java
+++ b/src/main/java/cn/byhieg/collectiontutorial/maptutorial/TreeMapExample.java
@@ -1,6 +1,5 @@
package cn.byhieg.collectiontutorial.maptutorial;
-import apple.laf.JRSUIUtils;
import java.util.Iterator;
import java.util.Map;
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java
new file mode 100644
index 0000000..91077fd
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java
@@ -0,0 +1,73 @@
+package cn.byhieg.designpatterntutorial.builder;
+
+/**
+ * Created by shiqifeng on 2017/5/7.
+ * Mail byhieg@gmail.com
+ */
+public class Person {
+
+ private int age;
+ private String name;
+ private int height;
+ private int weight;
+
+ public int getAge() {
+ return age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getHeight() {
+ return height;
+ }
+
+ public int getWeight() {
+ return weight;
+ }
+
+ public Person(Builder builder) {
+ age = builder.age;
+ name = builder.name;
+ height = builder.height;
+ weight = builder.weight;
+ }
+
+ @Override
+ public String toString() {
+ return "age = " + age + " name = " + name + " height = " + height + " weight = " + weight;
+ }
+
+ public static class Builder{
+
+ private int age;
+ private String name;
+ private int height;
+ private int weight;
+
+ public Builder setAge(int age) {
+ this.age = age;
+ return this;
+ }
+
+ public Builder setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder setHeight(int height) {
+ this.height = height;
+ return this;
+ }
+
+ public Builder setWeight(int weight) {
+ this.weight = weight;
+ return this;
+ }
+
+ public Person build() {
+ return new Person(this);
+ }
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java
new file mode 100644
index 0000000..0d90062
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java
@@ -0,0 +1,78 @@
+package cn.byhieg.designpatterntutorial.builder;
+
+/**
+ * Created by shiqifeng on 2017/5/7.
+ * Mail byhieg@gmail.com
+ */
+public class SimpleDialog {
+
+
+ public SimpleDialogController controller;
+
+ public SimpleDialog(){
+ controller = new SimpleDialogController();
+ }
+
+ public void setIcon(String icon) {
+ controller.setIcon(icon);
+ }
+
+ public void setTitle(String title) {
+ controller.setTitle(title);
+ }
+
+ public void setMessage(String message) {
+ controller.setMessage(message);
+ }
+
+ public void setPositiveButton(String positiveButton) {
+ controller.setPositiveButton(positiveButton);
+ }
+
+ public void setNegativeButton(String negativeButton) {
+ controller.setNegativeButton(negativeButton);
+ }
+
+
+ public static class Builder{
+ SimpleDialogController.DialogParams P;
+
+ public Builder(){
+ P = new SimpleDialogController.DialogParams();
+ }
+
+ public Builder setIcon(String icon){
+ P.icon = icon;
+ return this;
+ }
+
+ public Builder setTitle(String title) {
+ P.title = title;
+ return this;
+ }
+
+ public Builder setMessage(String message) {
+ P.message = message;
+ return this;
+ }
+
+ public Builder setPositiveButton(String positiveButton) {
+ P.positiveButton = positiveButton;
+ return this;
+ }
+
+ public Builder setNegativeButton(String negativeButton) {
+ P.negativeButton = negativeButton;
+ return this;
+ }
+
+ public SimpleDialog create(){
+ SimpleDialog dialog = new SimpleDialog();
+ P.apply(dialog.controller);
+ System.out.println(" ICON = " + P.icon + " MESSAGE = " + P.message + " positiveButton = " + P.positiveButton + " negativeButton" + P.negativeButton);
+ return dialog;
+ }
+ }
+
+
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java
new file mode 100644
index 0000000..4d41b74
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java
@@ -0,0 +1,81 @@
+package cn.byhieg.designpatterntutorial.builder;
+
+/**
+ * Created by shiqifeng on 2017/5/7.
+ * Mail byhieg@gmail.com
+ */
+public class SimpleDialogController {
+
+ private String icon;
+ private String title;
+ private String message;
+ private String positiveButton;
+ private String negativeButton;
+
+ public void setIcon(String icon) {
+ this.icon = icon;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public void setPositiveButton(String positiveButton) {
+ this.positiveButton = positiveButton;
+ }
+
+ public void setNegativeButton(String negativeButton) {
+ this.negativeButton = negativeButton;
+ }
+
+ public String getIcon() {
+ return icon;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getPositiveButton() {
+ return positiveButton;
+ }
+
+ public String getNegativeButton() {
+ return negativeButton;
+ }
+
+ public static class DialogParams{
+ public String icon;
+ public String title;
+ public String message;
+ public String positiveButton;
+ public String negativeButton;
+
+ public void apply(SimpleDialogController controller) {
+ if (icon != null) {
+ controller.setIcon(icon);
+ }
+ if (title != null) {
+ controller.setTitle(title);
+ }
+ if (message != null) {
+ controller.setMessage(message);
+ }
+ if (positiveButton != null) {
+ controller.setPositiveButton(positiveButton);
+ }
+ if (negativeButton != null) {
+ controller.setNegativeButton(negativeButton);
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Client.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Client.java
new file mode 100644
index 0000000..6d6a78d
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Client.java
@@ -0,0 +1,19 @@
+package cn.byhieg.designpatterntutorial.proxy.dynamicproxy;
+
+
+import java.lang.reflect.Proxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class Client {
+
+ public static void main(String[] args) {
+ Subject realSubject = new RealSubject();
+ DynamicProxy proxy = new DynamicProxy(realSubject);
+ ClassLoader classLoader = realSubject.getClass().getClassLoader();
+ Subject subject = (Subject) Proxy.newProxyInstance(classLoader, new Class[]{Subject.class}, proxy);
+ subject.visit();
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/DynamicProxy.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/DynamicProxy.java
new file mode 100644
index 0000000..5589d9c
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/DynamicProxy.java
@@ -0,0 +1,23 @@
+package cn.byhieg.designpatterntutorial.proxy.dynamicproxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class DynamicProxy implements InvocationHandler {
+ private Object object;
+
+
+ public DynamicProxy(Object object) {
+ this.object = object;
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ Object result = method.invoke(object, args);
+ return result;
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/RealSubject.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/RealSubject.java
new file mode 100644
index 0000000..ab391f7
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/RealSubject.java
@@ -0,0 +1,15 @@
+package cn.byhieg.designpatterntutorial.proxy.dynamicproxy;
+
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class RealSubject implements Subject {
+
+ private String name = "byhieg";
+ @Override
+ public void visit() {
+ System.out.println(name);
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Subject.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Subject.java
new file mode 100644
index 0000000..f662e32
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/dynamicproxy/Subject.java
@@ -0,0 +1,10 @@
+package cn.byhieg.designpatterntutorial.proxy.dynamicproxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public interface Subject {
+
+ void visit();
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Client.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Client.java
new file mode 100644
index 0000000..7e38f06
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Client.java
@@ -0,0 +1,13 @@
+package cn.byhieg.designpatterntutorial.proxy.staticproxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class Client {
+
+ public static void main(String[] args) {
+ ProxySubject subject = new ProxySubject(new RealSubject());
+ subject.visit();
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/ProxySubject.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/ProxySubject.java
new file mode 100644
index 0000000..9a6ed10
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/ProxySubject.java
@@ -0,0 +1,19 @@
+package cn.byhieg.designpatterntutorial.proxy.staticproxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class ProxySubject implements Subject{
+
+ private Subject subject;
+
+ public ProxySubject(Subject subject) {
+ this.subject = subject;
+ }
+
+ @Override
+ public void visit() {
+ subject.visit();
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/RealSubject.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/RealSubject.java
new file mode 100644
index 0000000..9669e0b
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/RealSubject.java
@@ -0,0 +1,14 @@
+package cn.byhieg.designpatterntutorial.proxy.staticproxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public class RealSubject implements Subject {
+
+ private String name = "byhieg";
+ @Override
+ public void visit() {
+ System.out.println(name);
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Subject.java b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Subject.java
new file mode 100644
index 0000000..1e93880
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/proxy/staticproxy/Subject.java
@@ -0,0 +1,10 @@
+package cn.byhieg.designpatterntutorial.proxy.staticproxy;
+
+/**
+ * Created by shiqifeng on 2017/3/17.
+ * Mail byhieg@gmail.com
+ */
+public interface Subject {
+
+ void visit();
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/singleton/DCLSingleton.java b/src/main/java/cn/byhieg/designpatterntutorial/singleton/DCLSingleton.java
new file mode 100644
index 0000000..ba45d43
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/singleton/DCLSingleton.java
@@ -0,0 +1,28 @@
+package cn.byhieg.designpatterntutorial.singleton;
+
+/**
+ * Created by shiqifeng on 2017/3/14.
+ * Mail byhieg@gmail.com
+ */
+public class DCLSingleton {
+
+ private static volatile DCLSingleton singleton;
+
+ private DCLSingleton(){
+
+ }
+
+ public static DCLSingleton getSingleton(){
+ if (singleton == null) {
+ synchronized (DCLSingleton.class) {
+ if (singleton == null) {
+ singleton = new DCLSingleton();
+ }
+ }
+ }
+ return singleton;
+ }
+
+
+
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/singleton/EnumSingleton.java b/src/main/java/cn/byhieg/designpatterntutorial/singleton/EnumSingleton.java
new file mode 100644
index 0000000..86c24cf
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/singleton/EnumSingleton.java
@@ -0,0 +1,14 @@
+package cn.byhieg.designpatterntutorial.singleton;
+
+/**
+ * Created by shiqifeng on 2017/3/14.
+ * Mail byhieg@gmail.com
+ */
+public enum EnumSingleton {
+ SINGLETON;
+
+
+ public void doSometings(){
+
+ }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/singleton/HungrySingleton.java b/src/main/java/cn/byhieg/designpatterntutorial/singleton/HungrySingleton.java
new file mode 100644
index 0000000..d3ac705
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/singleton/HungrySingleton.java
@@ -0,0 +1,17 @@
+package cn.byhieg.designpatterntutorial.singleton;
+
+/**
+ * Created by shiqifeng on 2017/3/14.
+ * Mail byhieg@gmail.com
+ */
+public class HungrySingleton {
+ private static final HungrySingleton singleton = new HungrySingleton();
+
+ private HungrySingleton(){
+
+ }
+
+// public static HungrySingleton getSingleton(){
+// return singleton;
+// }
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/singleton/SimpleSingleton.java b/src/main/java/cn/byhieg/designpatterntutorial/singleton/SimpleSingleton.java
new file mode 100644
index 0000000..fedad66
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/singleton/SimpleSingleton.java
@@ -0,0 +1,26 @@
+package cn.byhieg.designpatterntutorial.singleton;
+
+import java.io.Serializable;
+
+/**
+ * 该类线程不安全,是延迟加载的懒汉模式
+ * Created by shiqifeng on 2017/3/14.
+ * Mail byhieg@gmail.com
+ */
+public class SimpleSingleton implements Serializable {
+
+ private static SimpleSingleton simpleSingleton;
+
+ private SimpleSingleton(){
+
+ }
+
+ public static SimpleSingleton getInstance(){
+ if (simpleSingleton == null) {
+ simpleSingleton = new SimpleSingleton();
+ }
+ return simpleSingleton;
+ }
+
+
+}
diff --git a/src/main/java/cn/byhieg/designpatterntutorial/singleton/StaticSingleton.java b/src/main/java/cn/byhieg/designpatterntutorial/singleton/StaticSingleton.java
new file mode 100644
index 0000000..8a2ee20
--- /dev/null
+++ b/src/main/java/cn/byhieg/designpatterntutorial/singleton/StaticSingleton.java
@@ -0,0 +1,20 @@
+package cn.byhieg.designpatterntutorial.singleton;
+
+/**
+ * Created by shiqifeng on 2017/3/14.
+ * Mail byhieg@gmail.com
+ */
+public class StaticSingleton {
+
+ private StaticSingleton(){
+ }
+
+
+ public static final StaticSingleton getInstance(){
+ return Holder.singleton;
+ }
+
+ private static class Holder{
+ private static final StaticSingleton singleton = new StaticSingleton();
+ }
+}
diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java
new file mode 100644
index 0000000..f5da772
--- /dev/null
+++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java
@@ -0,0 +1,36 @@
+package cn.byhieg.threadtutorial.concurrent.atom;
+
+import java.util.concurrent.atomic.*;
+
+/**
+ * Created by shiqifeng on 2017/5/5.
+ * Mail byhieg@gmail.com
+ */
+public class AtomFactory {
+
+ private static final AtomFactory atomFactory = new AtomFactory();
+
+ private AtomFactory(){
+
+ }
+
+ public static AtomFactory getInstance(){
+ return atomFactory;
+ }
+
+ public AtomicInteger createAtomInt(int a){
+ return new AtomicInteger(a);
+ }
+
+ public AtomicIntegerArray createAtomArray(int[] a) {
+ return new AtomicIntegerArray(a);
+ }
+
+ public AtomicReference