优化格式
This commit is contained in:
parent
4255e77bcc
commit
1511e663a9
@ -327,13 +327,13 @@ POST _bulk
|
||||
|
||||
```json
|
||||
{
|
||||
"script": {
|
||||
"source": "ctx._source.user+=params.user",
|
||||
"lang": "painless",
|
||||
"params": {
|
||||
"user": " Cat"
|
||||
}
|
||||
}
|
||||
"script": {
|
||||
"source": "ctx._source.user+=params.user",
|
||||
"lang": "painless",
|
||||
"params": {
|
||||
"user": " Cat"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -204,8 +204,8 @@ lang = lang + "Script";
|
||||
|
||||
```javascript
|
||||
var a = null;
|
||||
a.toString() // Uncaught TypeError: Cannot read property 'toString' of null
|
||||
String(a) // "null"
|
||||
a.toString() // Uncaught TypeError: Cannot read property 'toString' of null
|
||||
String(a) // "null"
|
||||
```
|
||||
|
||||
**3. 常用的字符串操作**
|
||||
@ -227,24 +227,24 @@ slice,substring,substr 等方法在传入正数参数时,其行为比较
|
||||
var stringValue = "hello world";
|
||||
|
||||
// 只接收一个参数时
|
||||
alert(stringValue.slice(3)); // "lo world"
|
||||
alert(stringValue.substring(3)); // "lo world"
|
||||
alert(stringValue.substr(3)); // "lo world"
|
||||
alert(stringValue.slice(3)); // "lo world"
|
||||
alert(stringValue.substring(3)); // "lo world"
|
||||
alert(stringValue.substr(3)); // "lo world"
|
||||
|
||||
// 接收两个参数时候
|
||||
alert(stringValue.slice(3, 7)); // "lo w"
|
||||
alert(stringValue.substring(3,7)); // "lo w"
|
||||
alert(stringValue.substr(3, 7)); // "lo worl"
|
||||
alert(stringValue.slice(3, 7)); // "lo w"
|
||||
alert(stringValue.substring(3,7)); // "lo w"
|
||||
alert(stringValue.substr(3, 7)); // "lo worl"
|
||||
|
||||
// 当第一个参数为负值时
|
||||
alert(stringValue.slice(-3)); // "rld" 按照规则等价于: slice(8)
|
||||
alert(stringValue.substring(-3)); // "hello world" 按照规则等价于: substring(0)
|
||||
alert(stringValue.substr(-3)); // "rld" 按照规则等价于: substr(8)
|
||||
alert(stringValue.slice(-3)); // "rld" 按照规则等价于: slice(8)
|
||||
alert(stringValue.substring(-3)); // "hello world" 按照规则等价于: substring(0)
|
||||
alert(stringValue.substr(-3)); // "rld" 按照规则等价于: substr(8)
|
||||
|
||||
// 当第二个参数为负值时
|
||||
alert(stringValue.slice(3, -4)); // "lo w" 按照规则等价于: slice(3,7)
|
||||
alert(stringValue.substring(3, -4)); // "hel" 按照规则等价于: substring(3,0)
|
||||
alert(stringValue.substr(3, -4)); // ""(空字符串) 按照规则等价于: substr(3,0)
|
||||
alert(stringValue.slice(3, -4)); // "lo w" 按照规则等价于: slice(3,7)
|
||||
alert(stringValue.substring(3, -4)); // "hel" 按照规则等价于: substring(3,0)
|
||||
alert(stringValue.substr(3, -4)); // ""(空字符串) 按照规则等价于: substr(3,0)
|
||||
```
|
||||
|
||||
### 2.3 基本类型检测
|
||||
@ -301,9 +301,9 @@ var colors = ["red", "blue", "green"];
|
||||
```javascript
|
||||
var colors = ["red", "blue", "green"];
|
||||
|
||||
colors.length = 2; // ["red", "blue"]
|
||||
colors[colors.length] = "green"; // ["red", "blue", "green"]
|
||||
colors[10] = "black"; // ["red", "blue", "green", empty × 7, "black"]
|
||||
colors.length = 2; // ["red", "blue"]
|
||||
colors[colors.length] = "green"; // ["red", "blue", "green"]
|
||||
colors[10] = "black"; // ["red", "blue", "green", empty × 7, "black"]
|
||||
```
|
||||
|
||||
数组的其他常用方法如下:
|
||||
@ -333,9 +333,9 @@ ECMAScript 的数组提供了类似栈的特性,能够实现后进先出:
|
||||
```javascript
|
||||
var colors = ["red", "blue", "green"];
|
||||
|
||||
colors.push("black"); // ["red", "blue", "green", "black"]
|
||||
colors.pop() // "black"
|
||||
colors // ["red", "blue", "green"]
|
||||
colors.push("black"); // ["red", "blue", "green", "black"]
|
||||
colors.pop() // "black"
|
||||
colors // ["red", "blue", "green"]
|
||||
```
|
||||
|
||||
**4. 队列方法**
|
||||
@ -353,7 +353,7 @@ colors // ["blue", "green", "black", "yellow"]
|
||||
```javascript
|
||||
var values = [1, 2, 3, 4, 5];
|
||||
values.reverse();
|
||||
values // [5, 4, 3, 2, 1]
|
||||
values // [5, 4, 3, 2, 1]
|
||||
|
||||
// 支持传入排序函数进行自定义排序
|
||||
function compare(value1, value2) {
|
||||
@ -366,7 +366,7 @@ function compare(value1, value2) {
|
||||
}
|
||||
}
|
||||
values.sort(compare)
|
||||
values // [1, 2, 3, 4, 5]
|
||||
values // [1, 2, 3, 4, 5]
|
||||
```
|
||||
|
||||
**6. 操作方法**
|
||||
@ -377,8 +377,8 @@ values // [1, 2, 3, 4, 5]
|
||||
var colors = ["red", "green", "blue"];
|
||||
var colors2 = colors.concat("yellow", ["black", "brown"]);
|
||||
|
||||
colors // ["red", "green", "blue"]
|
||||
colors2 // ["red", "green", "blue", "yellow", "black", "brown"]
|
||||
colors // ["red", "green", "blue"]
|
||||
colors2 // ["red", "green", "blue", "yellow", "black", "brown"]
|
||||
```
|
||||
|
||||
**slice()** 用于截取数组并返回新的数组,它接收两个参数,分别代表截取的开始位置和结束位置,它是一个前开后闭的区间:
|
||||
@ -386,8 +386,8 @@ colors2 // ["red", "green", "blue", "yellow", "black", "brown"]
|
||||
```javascript
|
||||
var colors = ["red", "green", "blue", "yellow", "purple"];
|
||||
|
||||
var colors2 = colors.slice(1); // ["green", "blue", "yellow", "purple"]
|
||||
var colors3 = colors.slice(0,2); // ["red", "green"]
|
||||
var colors2 = colors.slice(1); // ["green", "blue", "yellow", "purple"]
|
||||
var colors3 = colors.slice(0,2); // ["red", "green"]
|
||||
```
|
||||
|
||||
**splice()** 用于删除并在删除位置新增数据项,它接收任意个参数,其中第一个参数为删除的开始位置,第二个参数为删除多少个数据项,之后可以接任意个参数,用于表示待插入的数据项:
|
||||
@ -409,10 +409,10 @@ colors // ["red", "black", "green", "yellow"]
|
||||
```shell
|
||||
var colors = ["red", "green", "blue", "yellow", "green", "blue"];
|
||||
|
||||
colors.indexOf("green"); // 1
|
||||
colors.indexOf("green", 3); // 4
|
||||
colors.lastIndexOf("green"); // 4
|
||||
colors.lastIndexOf("green", 3); // 1
|
||||
colors.indexOf("green"); // 1
|
||||
colors.indexOf("green", 3); // 4
|
||||
colors.lastIndexOf("green"); // 4
|
||||
colors.lastIndexOf("green", 3); // 1
|
||||
```
|
||||
|
||||
**8. 迭代方法**
|
||||
@ -430,17 +430,17 @@ var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
|
||||
|
||||
numbers.every(function (value, index, array) {
|
||||
return value > 3;
|
||||
});
|
||||
});
|
||||
// false
|
||||
|
||||
numbers.some(function (value, index, array) {
|
||||
return value > 3;
|
||||
});
|
||||
});
|
||||
// true
|
||||
|
||||
numbers.filter(function (value, index, array) {
|
||||
return value > 3;
|
||||
});
|
||||
});
|
||||
// [4, 5, 4]
|
||||
|
||||
numbers.forEach(function (value, index, array) {
|
||||
@ -449,7 +449,7 @@ numbers.forEach(function (value, index, array) {
|
||||
|
||||
numbers.map(function (value, index, array) {
|
||||
return value * 10;
|
||||
});
|
||||
});
|
||||
// [10, 20, 30, 40, 50, 40, 30, 20, 10]
|
||||
```
|
||||
|
||||
@ -461,11 +461,11 @@ ECMAScript 5 提供了两个归并数组的方法: **reduce()** 和 **reduceRi
|
||||
var values = [1, 2, 3, 4, 5];
|
||||
var sum01 = values.reduce(function (prev, cur, index, array) {
|
||||
return prev + cur;
|
||||
}); // 15
|
||||
}); // 15
|
||||
|
||||
var sum02 = values.reduceRight(function (prev, cur, index, array) {
|
||||
return prev + cur;
|
||||
}); // 15
|
||||
}); // 15
|
||||
```
|
||||
|
||||
### 3.3 Date 类型
|
||||
@ -474,10 +474,10 @@ var sum02 = values.reduceRight(function (prev, cur, index, array) {
|
||||
|
||||
```java
|
||||
var now = new Date();
|
||||
now.toLocaleString() // 2019-9-14 9:53:59 AM
|
||||
now.toLocaleString() // 2019-9-14 9:53:59 AM
|
||||
|
||||
var date = new Date(2018, 7, 8, 8, 30, 20);
|
||||
date.toLocaleString(); // 2018-8-8 8:30:20 AM
|
||||
date.toLocaleString(); // 2018-8-8 8:30:20 AM
|
||||
```
|
||||
|
||||
如果你只想知道当前时间的毫秒数,可以直接使用 Date 对象的静态方法:
|
||||
@ -496,9 +496,9 @@ Date.now()
|
||||
```javascript
|
||||
var date = new Date(2018, 7, 8, 8, 30, 20);
|
||||
|
||||
console.log(date.toLocaleString()); // 2018-8-8 8:30:20 AM
|
||||
console.log(date.toString()); // Wed Aug 08 2018 08:30:20 GMT+0800 (GMT+08:00)
|
||||
console.log(date.valueOf()); // 1533688220000
|
||||
console.log(date.toLocaleString()); // 2018-8-8 8:30:20 AM
|
||||
console.log(date.toString()); // Wed Aug 08 2018 08:30:20 GMT+0800 (GMT+08:00)
|
||||
console.log(date.valueOf()); // 1533688220000
|
||||
```
|
||||
|
||||
由于 **valueOf()** 返回的是日期的时间戳格式,所以对于 date 对象,可以直接使用比较运算符来比较其大小:
|
||||
@ -507,8 +507,8 @@ console.log(date.valueOf()); // 1533688220000
|
||||
var date01 = new Date(2018, 7, 8, 8, 30, 20);
|
||||
var date02 = new Date(2016, 7, 8, 8, 30, 20);
|
||||
|
||||
console.log(date01 > date02); // true
|
||||
console.log(date01 < date02); // flase
|
||||
console.log(date01 > date02); // true
|
||||
console.log(date01 < date02); // flase
|
||||
```
|
||||
|
||||
**2. 常用方法**
|
||||
@ -533,9 +533,9 @@ ECMAScript 使用 function 关键字来声明函数,但和其他语言不同
|
||||
function test(first, second) {
|
||||
console.log("first:" + first + ",second:" + second);
|
||||
}
|
||||
test(1) // first:1,second:undefined
|
||||
test(1,2) // first:1,second:2
|
||||
test(1,2,3) // first:1,second:2
|
||||
test(1) // first:1,second:undefined
|
||||
test(1,2) // first:1,second:2
|
||||
test(1,2,3) // first:1,second:2
|
||||
```
|
||||
|
||||
之所以能实现这样的效果,是因为 ECMAScript 在函数内部使用了一个数组 arguments 来维护所有参数,函数接收到的始终都是这个数组,而在实际使用时指向的也是这个数组中的具体元素,所以以上的函数等价于下面的函数:
|
||||
@ -650,7 +650,7 @@ function test() {
|
||||
age // age is not defined
|
||||
|
||||
if (true) {
|
||||
var name = "heibaiying";
|
||||
var name = "heibaiying";
|
||||
}
|
||||
name // heibaiying
|
||||
```
|
||||
|
@ -404,25 +404,25 @@ Java 语言中的顺序语义可以分为以下四类:
|
||||
编译器和处理器出于性能考虑,通常会改变代码的实际执行顺序,这种情况就称为重排序,具体分为以下两类:
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>重排序类型</th>
|
||||
<th>重排序表现</th>
|
||||
<th>重排序主体(原因)</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan = "2">指令重排序</td>
|
||||
<td>程序顺序和源代码顺序不一致</td>
|
||||
<td>编译器</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>执行顺序和程序顺序不一致</td>
|
||||
<td>JIT 编译器、处理器</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>存储子系统重排序 <br/> (内存重排序)</td>
|
||||
<td>源代码顺序、程序顺序和执行顺序这三者保持一致,<br/> 但是感知顺序与执行顺序不一致</td>
|
||||
<td>高速缓存、写缓冲器</td>
|
||||
</tr>
|
||||
<th>重排序类型</th>
|
||||
<th>重排序表现</th>
|
||||
<th>重排序主体(原因)</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan = "2">指令重排序</td>
|
||||
<td>程序顺序和源代码顺序不一致</td>
|
||||
<td>编译器</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>执行顺序和程序顺序不一致</td>
|
||||
<td>JIT 编译器、处理器</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>存储子系统重排序 <br/> (内存重排序)</td>
|
||||
<td>源代码顺序、程序顺序和执行顺序这三者保持一致,<br/> 但是感知顺序与执行顺序不一致</td>
|
||||
<td>高速缓存、写缓冲器</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
### 5.3 貌似串行语义
|
||||
@ -1525,7 +1525,7 @@ public class J2_ScheduledTask {
|
||||
public ThreadPoolExecutor(int corePoolSize, //核心线程数量
|
||||
int maximumPoolSize, //最大线程数量
|
||||
long keepAliveTime, //超过核心线程数的线程的存活时间
|
||||
TimeUnit unit, //存活时间的单位
|
||||
TimeUnit unit, //存活时间的单位
|
||||
BlockingQueue<Runnable> workQueue, //任务队列
|
||||
ThreadFactory threadFactory, //线程工厂
|
||||
RejectedExecutionHandler handler) //拒绝策略
|
||||
|
@ -88,19 +88,19 @@
|
||||
```java
|
||||
public class HungrySingleton implements Serializable {
|
||||
|
||||
private static final HungrySingleton instance;
|
||||
private static final HungrySingleton instance;
|
||||
|
||||
static {
|
||||
instance = new HungrySingleton();
|
||||
}
|
||||
static {
|
||||
instance = new HungrySingleton();
|
||||
}
|
||||
|
||||
// 确保构造器私有
|
||||
private HungrySingleton() {}
|
||||
private HungrySingleton() {}
|
||||
|
||||
// 获取单例对象
|
||||
public static HungrySingleton getInstance() {
|
||||
return instance;
|
||||
}
|
||||
public static HungrySingleton getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -133,17 +133,17 @@ public class StaticInnerClassHungrySingleton {
|
||||
```java
|
||||
public class LazySingletonUnsafe {
|
||||
|
||||
private static LazySingletonUnsafe instance = null;
|
||||
private static LazySingletonUnsafe instance = null;
|
||||
|
||||
private LazySingletonUnsafe() {
|
||||
}
|
||||
private LazySingletonUnsafe() {
|
||||
}
|
||||
|
||||
public static LazySingletonUnsafe getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LazySingletonUnsafe();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
public static LazySingletonUnsafe getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LazySingletonUnsafe();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -159,11 +159,11 @@ if (instance == null) {
|
||||
|
||||
```java
|
||||
public synchronized static LazySingletonUnsafe getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LazySingletonUnsafe();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
if (instance == null) {
|
||||
instance = new LazySingletonUnsafe();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
```
|
||||
|
||||
此时该方法是线程安全的,但是性能却存在问题。因为 synchronized 修饰的是静态方法,其锁住的是整个类对象,这意味着所有想要获取该单例对象的线程都必须要等待内部锁的释放。假设单例对象已经创建完成,并有 100 个线程并发获取该单例对象,则这 100 个线程都需要等待,显然这会降低系统的吞吐量,因此更好的方式是采用 **双重检查锁的机制** 来实现懒汉式单例:
|
||||
@ -238,9 +238,9 @@ public class SerializationDamage {
|
||||
```java
|
||||
public class HungrySingleton implements Serializable {
|
||||
......
|
||||
private Object readResolve() {
|
||||
return instance;
|
||||
}
|
||||
private Object readResolve() {
|
||||
return instance;
|
||||
}
|
||||
......
|
||||
}
|
||||
```
|
||||
@ -285,20 +285,20 @@ public class ReflectionDamage {
|
||||
```java
|
||||
public class HungrySingleton implements Serializable {
|
||||
|
||||
private static final HungrySingleton instance;
|
||||
private static final HungrySingleton instance;
|
||||
|
||||
static {
|
||||
instance = new HungrySingleton();
|
||||
}
|
||||
static {
|
||||
instance = new HungrySingleton();
|
||||
}
|
||||
|
||||
// 由于instance在类创建时就已经初始化完成,所以当使用反射调用构造器时就会抛出自定义的RuntimeException异常
|
||||
private HungrySingleton() {
|
||||
if (instance != null) {
|
||||
throw new RuntimeException("单例模式禁止反射调用");
|
||||
}
|
||||
}
|
||||
private HungrySingleton() {
|
||||
if (instance != null) {
|
||||
throw new RuntimeException("单例模式禁止反射调用");
|
||||
}
|
||||
}
|
||||
|
||||
......
|
||||
......
|
||||
}
|
||||
```
|
||||
|
||||
@ -313,21 +313,21 @@ public class HungrySingleton implements Serializable {
|
||||
```java
|
||||
public enum EnumInstance {
|
||||
|
||||
INSTANCE;
|
||||
INSTANCE;
|
||||
|
||||
private String field;
|
||||
private String field;
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public static EnumInstance getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
public static EnumInstance getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -398,20 +398,20 @@ public final class EnumInstance extends Enum
|
||||
|
||||
```java
|
||||
public class EnumInstanceTest {
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 序列化攻击
|
||||
EnumInstance instance = EnumInstance.getInstance();
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("EnumSingletonFile"));
|
||||
outputStream.writeObject(instance);
|
||||
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("EnumSingletonFile")));
|
||||
EnumInstance newInstance = (EnumInstance) inputStream.readObject();
|
||||
System.out.println(instance == newInstance);
|
||||
// 反射攻击,Enum类中只有一个两个参数的构造器:Enum(String name, int ordinal)
|
||||
Constructor<EnumInstance> constructor = EnumInstance.class.getDeclaredConstructor(String.class, int.class);
|
||||
constructor.setAccessible(true);
|
||||
EnumInstance enumInstance = constructor.newInstance("name", 0);
|
||||
System.out.println(instance == enumInstance);
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 序列化攻击
|
||||
EnumInstance instance = EnumInstance.getInstance();
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("EnumSingletonFile"));
|
||||
outputStream.writeObject(instance);
|
||||
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("EnumSingletonFile")));
|
||||
EnumInstance newInstance = (EnumInstance) inputStream.readObject();
|
||||
System.out.println(instance == newInstance);
|
||||
// 反射攻击,Enum类中只有一个两个参数的构造器:Enum(String name, int ordinal)
|
||||
Constructor<EnumInstance> constructor = EnumInstance.class.getDeclaredConstructor(String.class, int.class);
|
||||
constructor.setAccessible(true);
|
||||
EnumInstance enumInstance = constructor.newInstance("name", 0);
|
||||
System.out.println(instance == enumInstance);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -419,8 +419,8 @@ public class EnumInstanceTest {
|
||||
|
||||
```java
|
||||
Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflectively create enum objects
|
||||
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
|
||||
at com.heibaiying.creational.singleton.EnumInstanceTest.main(EnumInstanceTest.java:18)
|
||||
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
|
||||
at com.heibaiying.creational.singleton.EnumInstanceTest.main(EnumInstanceTest.java:18)
|
||||
```
|
||||
|
||||
|
||||
@ -441,7 +441,7 @@ Exception in thread "main" java.lang.IllegalArgumentException: Cannot reflective
|
||||
|
||||
```java
|
||||
public abstract class Phone {
|
||||
public abstract void call(String phoneNum);
|
||||
public abstract void call(String phoneNum);
|
||||
}
|
||||
```
|
||||
|
||||
@ -483,11 +483,11 @@ public class PhoneFactory {
|
||||
|
||||
```java
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
PhoneFactory phoneFactory = new PhoneFactory();
|
||||
phoneFactory.getPhone("xiaomi").call("123");
|
||||
phoneFactory.getPhone("huawei").call("321");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
PhoneFactory phoneFactory = new PhoneFactory();
|
||||
phoneFactory.getPhone("xiaomi").call("123");
|
||||
phoneFactory.getPhone("huawei").call("321");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -670,12 +670,12 @@ public class XiaomiPhoneFactory implements Factory {
|
||||
|
||||
```java
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
XiaomiPhoneFactory xiaomiPhoneFactory = new XiaomiPhoneFactory();
|
||||
xiaomiPhoneFactory.produceCharger().Charge(xiaomiPhoneFactory.producePhone());
|
||||
HuaweiPhoneFactory huaweiPhoneFactory = new HuaweiPhoneFactory();
|
||||
huaweiPhoneFactory.produceCharger().Charge(huaweiPhoneFactory.producePhone());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
XiaomiPhoneFactory xiaomiPhoneFactory = new XiaomiPhoneFactory();
|
||||
xiaomiPhoneFactory.produceCharger().Charge(xiaomiPhoneFactory.producePhone());
|
||||
HuaweiPhoneFactory huaweiPhoneFactory = new HuaweiPhoneFactory();
|
||||
huaweiPhoneFactory.produceCharger().Charge(huaweiPhoneFactory.producePhone());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -699,12 +699,12 @@ public class ZTest {
|
||||
|
||||
```java
|
||||
public class Phone {
|
||||
/*处理器*/
|
||||
private String processor;
|
||||
/*摄像头*/
|
||||
private String camera;
|
||||
/*屏幕*/
|
||||
private String screen;
|
||||
/*处理器*/
|
||||
private String processor;
|
||||
/*摄像头*/
|
||||
private String camera;
|
||||
/*屏幕*/
|
||||
private String screen;
|
||||
}
|
||||
```
|
||||
|
||||
@ -714,11 +714,11 @@ public class Phone {
|
||||
public abstract class Builder {
|
||||
|
||||
protected Phone phone = new Phone();
|
||||
/*安装处理器*/
|
||||
/*安装处理器*/
|
||||
public abstract void addProcessor();
|
||||
/*组装摄像头*/
|
||||
/*组装摄像头*/
|
||||
public abstract void addCamera();
|
||||
/*安装屏幕*/
|
||||
/*安装屏幕*/
|
||||
public abstract void addScreen();
|
||||
|
||||
public Phone produce() {
|
||||
@ -750,20 +750,20 @@ public class HuaweiBuilder extends Builder {
|
||||
|
||||
```java
|
||||
public class XiaomiBuilder extends Builder {
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
phone.setProcessor("高通骁龙处理器");
|
||||
}
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
phone.setProcessor("高通骁龙处理器");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCamera() {
|
||||
phone.setCamera("索尼摄像头");
|
||||
}
|
||||
@Override
|
||||
public void addCamera() {
|
||||
phone.setCamera("索尼摄像头");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScreen() {
|
||||
phone.setScreen("OLED");
|
||||
}
|
||||
@Override
|
||||
public void addScreen() {
|
||||
phone.setScreen("OLED");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -772,18 +772,18 @@ public class XiaomiBuilder extends Builder {
|
||||
```java
|
||||
public class Manager {
|
||||
|
||||
private Builder builder;
|
||||
private Builder builder;
|
||||
|
||||
public Manager(Builder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
public Manager(Builder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Phone buy() {
|
||||
builder.addCamera();
|
||||
builder.addProcessor();
|
||||
builder.addScreen();
|
||||
return builder.produce();
|
||||
}
|
||||
public Phone buy() {
|
||||
builder.addCamera();
|
||||
builder.addProcessor();
|
||||
builder.addScreen();
|
||||
return builder.produce();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -791,12 +791,12 @@ public class Manager {
|
||||
|
||||
```java
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
Phone huawei = new Manager(new HuaweiBuilder()).buy();
|
||||
System.out.println(huawei);
|
||||
Phone xiaomi = new Manager(new XiaomiBuilder()).buy();
|
||||
System.out.println(xiaomi);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Phone huawei = new Manager(new HuaweiBuilder()).buy();
|
||||
System.out.println(huawei);
|
||||
Phone xiaomi = new Manager(new XiaomiBuilder()).buy();
|
||||
System.out.println(xiaomi);
|
||||
}
|
||||
}
|
||||
// 输出:
|
||||
Phone(processor=海思麒麟处理器, camera=莱卡摄像头, screen=OLED)
|
||||
@ -896,7 +896,7 @@ public class SmartPhone implements Cloneable {
|
||||
|
||||
```java
|
||||
public interface IService {
|
||||
void compute();
|
||||
void compute();
|
||||
}
|
||||
```
|
||||
|
||||
@ -974,9 +974,9 @@ proxyInstance.compute();
|
||||
|
||||
```java
|
||||
public class ComputeService {
|
||||
public void compute() {
|
||||
System.out.println("业务处理");
|
||||
}
|
||||
public void compute() {
|
||||
System.out.println("业务处理");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -1268,7 +1268,7 @@ public class Folder extends Component {
|
||||
@Override
|
||||
public void print() {
|
||||
System.out.println(getName());
|
||||
componentList.forEach(x -> System.out.println(" " + x.getName()));
|
||||
componentList.forEach(x -> System.out.println(" " + x.getName()));
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -1831,13 +1831,13 @@ public class GroupLeader extends Leader {
|
||||
|
||||
```java
|
||||
public class DepartManager extends Leader {
|
||||
@Override
|
||||
public void approval(Application application) {
|
||||
System.out.println(application.getTitle() + "被部门经理审批通过");
|
||||
if (application.getDayNum() >= 5) {
|
||||
leader.approval(application);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void approval(Application application) {
|
||||
System.out.println(application.getTitle() + "被部门经理审批通过");
|
||||
if (application.getDayNum() >= 5) {
|
||||
leader.approval(application);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2011,24 +2011,24 @@ public interface Strategy {
|
||||
}
|
||||
|
||||
public class TravelStrategy implements Strategy {
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("集体旅游");
|
||||
}
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("集体旅游");
|
||||
}
|
||||
}
|
||||
|
||||
public class BonusStrategy implements Strategy {
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("奖金激励");
|
||||
}
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("奖金激励");
|
||||
}
|
||||
}
|
||||
|
||||
public class WorkOvertimeStrategy implements Strategy {
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("奖励加班");
|
||||
}
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("奖励加班");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2141,19 +2141,19 @@ public class CloseState extends State {
|
||||
|
||||
```java
|
||||
public class PauseState extends State {
|
||||
@Override
|
||||
public void speed() {
|
||||
System.out.print("操作失败:暂停状态下不支持加速");
|
||||
}
|
||||
@Override
|
||||
public void speed() {
|
||||
System.out.print("操作失败:暂停状态下不支持加速");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class SpeedState extends State {
|
||||
@Override
|
||||
public void paly() {
|
||||
System.out.println("系统提示:你当前已处于加速播放状态");
|
||||
}
|
||||
@Override
|
||||
public void paly() {
|
||||
System.out.println("系统提示:你当前已处于加速播放状态");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2162,47 +2162,47 @@ public class SpeedState extends State {
|
||||
```java
|
||||
public class Player {
|
||||
|
||||
private State state;
|
||||
private State state;
|
||||
|
||||
public final static PlayState PLAY_STATE = new PlayState();
|
||||
public final static PauseState PAUSE_STATE = new PauseState();
|
||||
public final static CloseState CLOSE_STATE = new CloseState();
|
||||
public final static SpeedState SPEED_STATE = new SpeedState();
|
||||
public final static PlayState PLAY_STATE = new PlayState();
|
||||
public final static PauseState PAUSE_STATE = new PauseState();
|
||||
public final static CloseState CLOSE_STATE = new CloseState();
|
||||
public final static SpeedState SPEED_STATE = new SpeedState();
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
this.state.setPlayer(this);
|
||||
}
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
this.state.setPlayer(this);
|
||||
}
|
||||
|
||||
Player() {
|
||||
// 假设播放器初始状态为关闭
|
||||
this.state = new CloseState();
|
||||
this.state.setPlayer(this);
|
||||
}
|
||||
Player() {
|
||||
// 假设播放器初始状态为关闭
|
||||
this.state = new CloseState();
|
||||
this.state.setPlayer(this);
|
||||
}
|
||||
|
||||
public void paly() {
|
||||
System.out.println("播放视频");
|
||||
state.paly();
|
||||
}
|
||||
public void paly() {
|
||||
System.out.println("播放视频");
|
||||
state.paly();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
System.out.println("暂停视频");
|
||||
state.pause();
|
||||
}
|
||||
public void pause() {
|
||||
System.out.println("暂停视频");
|
||||
state.pause();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
System.out.println("关闭视频");
|
||||
state.close();
|
||||
}
|
||||
public void close() {
|
||||
System.out.println("关闭视频");
|
||||
state.close();
|
||||
}
|
||||
|
||||
public void speed() {
|
||||
System.out.println("视频加速");
|
||||
state.speed();
|
||||
}
|
||||
public void speed() {
|
||||
System.out.println("视频加速");
|
||||
state.speed();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@ -2251,9 +2251,9 @@ player.speed();
|
||||
```java
|
||||
abstract class Mediator {
|
||||
|
||||
public abstract void register(Person person);
|
||||
public abstract void register(Person person);
|
||||
|
||||
public abstract void send(String from, String message);
|
||||
public abstract void send(String from, String message);
|
||||
}
|
||||
```
|
||||
|
||||
@ -2262,26 +2262,26 @@ abstract class Mediator {
|
||||
```java
|
||||
public class HouseMediator extends Mediator {
|
||||
|
||||
private List<Person> personList = new ArrayList<>();
|
||||
private List<Person> personList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void register(Person person) {
|
||||
if (!personList.contains(person)) {
|
||||
personList.add(person);
|
||||
person.setMediator(this);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void register(Person person) {
|
||||
if (!personList.contains(person)) {
|
||||
personList.add(person);
|
||||
person.setMediator(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String from, String message) {
|
||||
System.out.println(from + "发送消息:" + message);
|
||||
for (Person person : personList) {
|
||||
String name = person.getName();
|
||||
if (!name.equals(from)) {
|
||||
person.receive(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void send(String from, String message) {
|
||||
System.out.println(from + "发送消息:" + message);
|
||||
for (Person person : personList) {
|
||||
String name = person.getName();
|
||||
if (!name.equals(from)) {
|
||||
person.receive(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2290,20 +2290,20 @@ public class HouseMediator extends Mediator {
|
||||
```java
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
private Mediator mediator;
|
||||
private String name;
|
||||
private Mediator mediator;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
mediator.send(this.name, message);
|
||||
}
|
||||
public void send(String message) {
|
||||
mediator.send(this.name, message);
|
||||
}
|
||||
|
||||
public void receive(String message) {
|
||||
System.out.println(name + "收到消息:" + message);
|
||||
}
|
||||
public void receive(String message) {
|
||||
System.out.println(name + "收到消息:" + message);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2311,16 +2311,16 @@ public class Person {
|
||||
|
||||
```java
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
HouseMediator houseMediator = new HouseMediator();
|
||||
Person seller = new Person("卖方");
|
||||
Person buyer = new Person("买方");
|
||||
houseMediator.register(seller);
|
||||
houseMediator.register(buyer);
|
||||
buyer.send("价格多少");
|
||||
seller.send("10万");
|
||||
buyer.send("太贵了");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
HouseMediator houseMediator = new HouseMediator();
|
||||
Person seller = new Person("卖方");
|
||||
Person buyer = new Person("买方");
|
||||
houseMediator.register(seller);
|
||||
houseMediator.register(buyer);
|
||||
buyer.send("价格多少");
|
||||
seller.send("10万");
|
||||
buyer.send("太贵了");
|
||||
}
|
||||
}
|
||||
|
||||
// 输出:
|
||||
@ -2346,10 +2346,10 @@ public class ZTest {
|
||||
|
||||
```java
|
||||
public class Book {
|
||||
private String name;
|
||||
public Book(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
private String name;
|
||||
public Book(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2357,29 +2357,29 @@ public class Book {
|
||||
|
||||
```java
|
||||
public interface Bookshelf {
|
||||
void addBook(Book book);
|
||||
void removeBook(Book book);
|
||||
BookIterator iterator();
|
||||
void addBook(Book book);
|
||||
void removeBook(Book book);
|
||||
BookIterator iterator();
|
||||
}
|
||||
|
||||
public class BookshelfImpl implements Bookshelf {
|
||||
|
||||
private List<Book> bookList = new ArrayList<>();
|
||||
private List<Book> bookList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void addBook(Book book) {
|
||||
bookList.add(book);
|
||||
}
|
||||
@Override
|
||||
public void addBook(Book book) {
|
||||
bookList.add(book);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBook(Book book) {
|
||||
bookList.remove(book);
|
||||
}
|
||||
@Override
|
||||
public void removeBook(Book book) {
|
||||
bookList.remove(book);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookIterator iterator() {
|
||||
return new BookIterator(bookList);
|
||||
}
|
||||
@Override
|
||||
public BookIterator iterator() {
|
||||
return new BookIterator(bookList);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2387,28 +2387,28 @@ public class BookshelfImpl implements Bookshelf {
|
||||
|
||||
```java
|
||||
public interface Iterator<E> {
|
||||
E next();
|
||||
boolean hasNext();
|
||||
E next();
|
||||
boolean hasNext();
|
||||
}
|
||||
|
||||
public class BookIterator implements Iterator<Book> {
|
||||
|
||||
private List<Book> bookList;
|
||||
private int position = 0;
|
||||
private List<Book> bookList;
|
||||
private int position = 0;
|
||||
|
||||
public BookIterator(List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
}
|
||||
public BookIterator(List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book next() {
|
||||
return bookList.get(position++);
|
||||
}
|
||||
@Override
|
||||
public Book next() {
|
||||
return bookList.get(position++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return position < bookList.size();
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return position < bookList.size();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2460,21 +2460,21 @@ while (iterator.hasNext()) {
|
||||
```java
|
||||
public interface Archive {
|
||||
// 接受访问者
|
||||
void accept(Visitor visitor);
|
||||
void accept(Visitor visitor);
|
||||
}
|
||||
|
||||
public class PublicArchive implements Archive {
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
public class SecretArchive implements Archive {
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
@Override
|
||||
public void accept(Visitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2483,39 +2483,39 @@ public class SecretArchive implements Archive {
|
||||
```java
|
||||
public interface Visitor {
|
||||
// 访问公开档案
|
||||
void visit(PublicArchive publicArchive);
|
||||
void visit(PublicArchive publicArchive);
|
||||
// 访问加密档案
|
||||
void visit(SecretArchive secretArchive);
|
||||
void visit(SecretArchive secretArchive);
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class DepartManager implements Visitor {
|
||||
|
||||
@Override
|
||||
public void visit(PublicArchive publicArchive) {
|
||||
System.out.println("所有公开档案");
|
||||
}
|
||||
@Override
|
||||
public void visit(PublicArchive publicArchive) {
|
||||
System.out.println("所有公开档案");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SecretArchive secretArchive) {
|
||||
System.out.println("三级以下权限的加密档案");
|
||||
}
|
||||
@Override
|
||||
public void visit(SecretArchive secretArchive) {
|
||||
System.out.println("三级以下权限的加密档案");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
public class President implements Visitor {
|
||||
|
||||
@Override
|
||||
public void visit(PublicArchive publicArchive) {
|
||||
System.out.println("所有公开档案");
|
||||
}
|
||||
@Override
|
||||
public void visit(PublicArchive publicArchive) {
|
||||
System.out.println("所有公开档案");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SecretArchive secretArchive) {
|
||||
System.out.println("所有加密档案");
|
||||
}
|
||||
@Override
|
||||
public void visit(SecretArchive secretArchive) {
|
||||
System.out.println("所有加密档案");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2524,24 +2524,24 @@ public class President implements Visitor {
|
||||
```java
|
||||
public class Company {
|
||||
|
||||
private List<Archive> archives = new ArrayList<>();
|
||||
private List<Archive> archives = new ArrayList<>();
|
||||
|
||||
// 接收档案
|
||||
void add(Archive archive) {
|
||||
archives.add(archive);
|
||||
}
|
||||
void add(Archive archive) {
|
||||
archives.add(archive);
|
||||
}
|
||||
|
||||
// 移除档案
|
||||
void remove(Archive archive) {
|
||||
archives.remove(archive);
|
||||
}
|
||||
void remove(Archive archive) {
|
||||
archives.remove(archive);
|
||||
}
|
||||
|
||||
// 接待访问者
|
||||
void accept(Visitor visitor) {
|
||||
for (Archive archive : archives) {
|
||||
archive.accept(visitor);
|
||||
}
|
||||
}
|
||||
void accept(Visitor visitor) {
|
||||
for (Archive archive : archives) {
|
||||
archive.accept(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@ -2584,8 +2584,8 @@ company.accept(new President());
|
||||
|
||||
```java
|
||||
public class Article {
|
||||
private String title;
|
||||
private String content;
|
||||
private String title;
|
||||
private String content;
|
||||
}
|
||||
```
|
||||
|
||||
@ -2595,20 +2595,20 @@ public class Article {
|
||||
// 备忘录对象
|
||||
public class Memorandum {
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
private Date createTime;
|
||||
private String title;
|
||||
private String content;
|
||||
private Date createTime;
|
||||
|
||||
// 根据目标对象来创建备忘录对象
|
||||
public Memorandum(Article article) {
|
||||
this.title = article.getTitle();
|
||||
this.content = article.getContent();
|
||||
this.createTime = new Date();
|
||||
}
|
||||
public Memorandum(Article article) {
|
||||
this.title = article.getTitle();
|
||||
this.content = article.getContent();
|
||||
this.createTime = new Date();
|
||||
}
|
||||
|
||||
public Article toArticle() {
|
||||
return new Article(this.title, this.content);
|
||||
}
|
||||
public Article toArticle() {
|
||||
return new Article(this.title, this.content);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@ -2618,24 +2618,24 @@ public class Memorandum {
|
||||
```java
|
||||
public class GitRepository {
|
||||
|
||||
private List<Memorandum> repository = new ArrayList<>();
|
||||
private List<Memorandum> repository = new ArrayList<>();
|
||||
|
||||
// 保存当前数据
|
||||
public void save(Article article) {
|
||||
Memorandum memorandum = new Memorandum(article);
|
||||
repository.add(memorandum);
|
||||
}
|
||||
public void save(Article article) {
|
||||
Memorandum memorandum = new Memorandum(article);
|
||||
repository.add(memorandum);
|
||||
}
|
||||
|
||||
// 获取指定版本类的数据
|
||||
public Article get(int version) {
|
||||
Memorandum memorandum = repository.get(version);
|
||||
return memorandum.toArticle();
|
||||
}
|
||||
public Article get(int version) {
|
||||
Memorandum memorandum = repository.get(version);
|
||||
return memorandum.toArticle();
|
||||
}
|
||||
|
||||
// 撤销当前操作
|
||||
public Article back() {
|
||||
return repository.get(repository.size() - 1).toArticle();
|
||||
}
|
||||
public Article back() {
|
||||
return repository.get(repository.size() - 1).toArticle();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -90,7 +90,7 @@
|
||||
|
||||
```shell
|
||||
processManagement:
|
||||
# 以后台进程的方式启动
|
||||
# 以后台进程的方式启动
|
||||
fork: true
|
||||
systemLog:
|
||||
destination: file
|
||||
@ -103,10 +103,10 @@ net:
|
||||
port: 27018
|
||||
bindIp: 0.0.0.0
|
||||
replication:
|
||||
# 副本集的名称
|
||||
# 副本集的名称
|
||||
replSetName: rs0
|
||||
sharding:
|
||||
# 在集群中的角色为分片
|
||||
# 在集群中的角色为分片
|
||||
clusterRole: shardsvr
|
||||
```
|
||||
|
||||
@ -125,10 +125,10 @@ net:
|
||||
port: 37018
|
||||
bindIp: 0.0.0.0
|
||||
replication:
|
||||
# 副本集的名称
|
||||
# 副本集的名称
|
||||
replSetName: rs1
|
||||
sharding:
|
||||
# 在集群中的角色为分片
|
||||
# 在集群中的角色为分片
|
||||
clusterRole: shardsvr
|
||||
```
|
||||
|
||||
@ -146,13 +146,13 @@ systemLog:
|
||||
storage:
|
||||
dbPath: "/home/mongodb/config-serve-data"
|
||||
net:
|
||||
# 配置服务的默认端口为27019
|
||||
# 配置服务的默认端口为27019
|
||||
port: 27019
|
||||
bindIp: 0.0.0.0
|
||||
replication:
|
||||
replSetName: configReplSet
|
||||
sharding:
|
||||
# 在集群中的角色为配置服务
|
||||
# 在集群中的角色为配置服务
|
||||
clusterRole: configsvr
|
||||
```
|
||||
|
||||
|
@ -105,11 +105,11 @@ db.user.insertMany([
|
||||
|
||||
# 此时会返回新插入的数据的ObjectId
|
||||
{
|
||||
"acknowledged" : true,
|
||||
"insertedIds" : [
|
||||
ObjectId("5d3d0489ad38cd3becc7b03b"),
|
||||
ObjectId("5d3d0489ad38cd3becc7b03c")
|
||||
]
|
||||
"acknowledged" : true,
|
||||
"insertedIds" : [
|
||||
ObjectId("5d3d0489ad38cd3becc7b03b"),
|
||||
ObjectId("5d3d0489ad38cd3becc7b03c")
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -78,12 +78,12 @@ db.employees.aggregate([
|
||||
|
||||
```shell
|
||||
{
|
||||
"_id" : ObjectId("5d3fe6488ba16934ccce999d"),
|
||||
"fullName" : "GeorgiFacello"
|
||||
"_id" : ObjectId("5d3fe6488ba16934ccce999d"),
|
||||
"fullName" : "GeorgiFacello"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d3fe6488ba16934ccce99a0"),
|
||||
"fullName" : "ChirstianKoblick"
|
||||
"_id" : ObjectId("5d3fe6488ba16934ccce99a0"),
|
||||
"fullName" : "ChirstianKoblick"
|
||||
}
|
||||
```
|
||||
|
||||
@ -158,16 +158,16 @@ db.employees.aggregate(
|
||||
|
||||
```json
|
||||
{
|
||||
"_id" : "M",
|
||||
"totalAge" : 78,
|
||||
"avgAge" : 39,
|
||||
"count" : 2
|
||||
"_id" : "M",
|
||||
"totalAge" : 78,
|
||||
"avgAge" : 39,
|
||||
"count" : 2
|
||||
},
|
||||
{
|
||||
"_id" : "F",
|
||||
"totalAge" : 66,
|
||||
"avgAge" : 33,
|
||||
"count" : 2
|
||||
"_id" : "F",
|
||||
"totalAge" : 66,
|
||||
"avgAge" : 33,
|
||||
"count" : 2
|
||||
}
|
||||
```
|
||||
|
||||
@ -188,10 +188,10 @@ db.employees.aggregate(
|
||||
|
||||
# 输出如下
|
||||
{
|
||||
"_id" : null,
|
||||
"totalAge" : 144,
|
||||
"avgAge" : 36,
|
||||
"count" : 4
|
||||
"_id" : null,
|
||||
"totalAge" : 144,
|
||||
"avgAge" : 36,
|
||||
"count" : 4
|
||||
}
|
||||
```
|
||||
|
||||
@ -337,11 +337,11 @@ db.employees.aggregate([
|
||||
"emp_no" : 10001,
|
||||
........
|
||||
"emp_title" : [
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@ -352,16 +352,16 @@ db.employees.aggregate([
|
||||
"emp_no" : 10004,
|
||||
........
|
||||
"emp_title" : [
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
@ -412,31 +412,31 @@ db.employees.aggregate([
|
||||
"emp_no" : 10001,
|
||||
.......
|
||||
"emps_title" : [
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a6"),
|
||||
"emp_no" : 10002,
|
||||
"title" : "Staff"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a7"),
|
||||
"emp_no" : 10003,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a6"),
|
||||
"emp_no" : 10002,
|
||||
"title" : "Staff"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a7"),
|
||||
"emp_no" : 10003,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@ -445,31 +445,31 @@ db.employees.aggregate([
|
||||
"emp_no" : 10002,
|
||||
.......
|
||||
"emps_title" : [
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a6"),
|
||||
"emp_no" : 10002,
|
||||
"title" : "Staff"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a7"),
|
||||
"emp_no" : 10003,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a5"),
|
||||
"emp_no" : 10001,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a6"),
|
||||
"emp_no" : 10002,
|
||||
"title" : "Staff"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a7"),
|
||||
"emp_no" : 10003,
|
||||
"title" : "Senior Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a8"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Engineer"
|
||||
},
|
||||
{
|
||||
"_id" : ObjectId("5d4011728ba16934ccce99a9"),
|
||||
"emp_no" : 10004,
|
||||
"title" : "Senior Engineer"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@ -565,10 +565,10 @@ MongoDB 的 MapReduce 在概念上与 Hadoop MapReduce 类似,MongoDB 将 *map
|
||||
```shell
|
||||
db.employees.mapReduce(
|
||||
function(){
|
||||
emit(this.gender,this.age)
|
||||
emit(this.gender,this.age)
|
||||
},
|
||||
function(key,values){
|
||||
return Array.avg(values)
|
||||
return Array.avg(values)
|
||||
},
|
||||
{ out:"age_avg"}
|
||||
)
|
||||
|
@ -110,10 +110,10 @@ mysql> SHOW MASTER STATUS;
|
||||
|
||||
```shell
|
||||
CHANGE MASTER TO MASTER_HOST='192.168.0.226',\
|
||||
MASTER_USER='repl', \
|
||||
MASTER_PASSWORD='123456',\
|
||||
MASTER_LOG_FILE='mysql-bin.000001',\
|
||||
MASTER_LOG_POS=887;
|
||||
MASTER_USER='repl', \
|
||||
MASTER_PASSWORD='123456',\
|
||||
MASTER_LOG_FILE='mysql-bin.000001',\
|
||||
MASTER_LOG_POS=887;
|
||||
```
|
||||
|
||||
开始复制:
|
||||
@ -220,9 +220,9 @@ STOP SLAVE IO_THREAD FOR CHANNEL '';
|
||||
|
||||
```shell
|
||||
CHANGE MASTER TO MASTER_HOST='192.168.0.226',\
|
||||
MASTER_USER='repl',
|
||||
MASTER_PASSWORD='123456',
|
||||
MASTER_AUTO_POSITION=1;
|
||||
MASTER_USER='repl',
|
||||
MASTER_PASSWORD='123456',
|
||||
MASTER_AUTO_POSITION=1;
|
||||
```
|
||||
|
||||
开始复制:
|
||||
@ -236,9 +236,9 @@ START SLAVE;
|
||||
```sql
|
||||
mysql> SHOW SLAVE STATUS\G
|
||||
....
|
||||
Master_UUID : e1148574-bdd0-11e9-8873-0800273acbfd
|
||||
Retrieved_Gtid_Set : e1148574-bdd0-11e9-8873-0800273acbfd:1-2
|
||||
Executed_Gtid_Set : e1148574-bdd0-11e9-8873-0800273acbfd:1-2
|
||||
Master_UUID : e1148574-bdd0-11e9-8873-0800273acbfd
|
||||
Retrieved_Gtid_Set : e1148574-bdd0-11e9-8873-0800273acbfd:1-2
|
||||
Executed_Gtid_Set : e1148574-bdd0-11e9-8873-0800273acbfd:1-2
|
||||
.....
|
||||
```
|
||||
|
||||
|
@ -713,19 +713,19 @@ drop user youUsername cascade;
|
||||
```sql
|
||||
-- mysql
|
||||
create table 【if not exists】 表名(
|
||||
字段名 字段类型 【约束】,
|
||||
字段名 字段类型 【约束】,
|
||||
...
|
||||
字段名 字段类型 【约束】
|
||||
字段名 字段类型 【约束】,
|
||||
字段名 字段类型 【约束】,
|
||||
...
|
||||
字段名 字段类型 【约束】
|
||||
|
||||
)
|
||||
|
||||
-- oracle
|
||||
CREATE TABLE [schema.]表名(
|
||||
字段名 字段类型 【约束】
|
||||
字段名 字段类型 【约束】,
|
||||
...
|
||||
字段名 字段类型 【约束】
|
||||
字段名 字段类型 【约束】,
|
||||
...
|
||||
字段名 字段类型 【约束】
|
||||
);
|
||||
|
||||
```
|
||||
@ -897,11 +897,11 @@ insert into 表1(column1,column2,column3) select column1x,column2x,column3x from
|
||||
|
||||
```sql
|
||||
create table 表名(
|
||||
字段名 字段类型 not null,#非空
|
||||
字段名 字段类型 primary key,#主键
|
||||
字段名 字段类型 unique,#唯一
|
||||
字段名 字段类型 default 值,#默认
|
||||
constraint 约束名 foreign key(字段名) references 主表(被引用列)
|
||||
字段名 字段类型 not null,#非空
|
||||
字段名 字段类型 primary key,#主键
|
||||
字段名 字段类型 unique,#唯一
|
||||
字段名 字段类型 default 值,#默认
|
||||
constraint 约束名 foreign key(字段名) references 主表(被引用列)
|
||||
)
|
||||
|
||||
```
|
||||
@ -929,8 +929,8 @@ create table 表名(
|
||||
-- 删除默认
|
||||
alter table 表名 modify column 字段名 字段类型 ;
|
||||
-- oracle
|
||||
-- 添加默认
|
||||
alter table 表名 modify 字段名 default 默认值;
|
||||
-- 添加默认
|
||||
alter table 表名 modify 字段名 default 默认值;
|
||||
-- 删除默认
|
||||
alter table 表名 modify 字段名 ;
|
||||
|
||||
@ -965,7 +965,7 @@ create table 表名(
|
||||
```sql
|
||||
-- 创建表时设置自增长列
|
||||
create table 表(
|
||||
字段名 字段类型 约束 auto_increment
|
||||
字段名 字段类型 约束 auto_increment
|
||||
)
|
||||
|
||||
-- 修改表时设置自增长列
|
||||
@ -1204,7 +1204,7 @@ SHOW TRIGGERS [FROM schema_name];
|
||||
```sql
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE 过程名([[IN|OUT|INOUT] 参数名 数据类型],...)
|
||||
BEGIN
|
||||
BEGIN
|
||||
过程体
|
||||
END
|
||||
DELIMITER ;
|
||||
@ -1322,7 +1322,7 @@ end if;
|
||||
|
||||
```sql
|
||||
【名称:】while 循环条件 do
|
||||
循环体
|
||||
循环体
|
||||
end while 【名称】;
|
||||
```
|
||||
|
||||
@ -1331,7 +1331,7 @@ end while 【名称】;
|
||||
|
||||
```sql
|
||||
【名称:】loop
|
||||
循环体
|
||||
循环体
|
||||
end loop 【名称】;
|
||||
```
|
||||
|
||||
@ -1339,7 +1339,7 @@ end loop 【名称】;
|
||||
|
||||
```sql
|
||||
【名称:】repeat
|
||||
循环体
|
||||
循环体
|
||||
until 结束条件
|
||||
end repeat 【名称】;
|
||||
```
|
||||
|
@ -461,7 +461,7 @@ chmod +x /etc/keepalived/haproxy_check.sh
|
||||
|
||||
```yaml
|
||||
global_defs {
|
||||
# 路由id,主备节点不能相同
|
||||
# 路由id,主备节点不能相同
|
||||
router_id node2
|
||||
|
||||
}
|
||||
|
@ -55,9 +55,9 @@ $ ./configure \
|
||||
--prefix=/usr/app/nginx-1.16.1 \
|
||||
--with-pcre=/usr/app/pcre-8.42 \
|
||||
--with-zlib=/usr/app/zlib-1.2.11 \
|
||||
--with-http_ssl_module \ # 启用HTTPS支持
|
||||
--with-stream \ # 启用TCP和UDP代理功能
|
||||
--with-mail=dynamic # 启用邮件代理功能
|
||||
--with-http_ssl_module \ # 启用HTTPS支持
|
||||
--with-stream \ # 启用TCP和UDP代理功能
|
||||
--with-mail=dynamic # 启用邮件代理功能
|
||||
|
||||
$ make
|
||||
$ sudo make install
|
||||
|
Loading…
x
Reference in New Issue
Block a user