forked from heibaiying/Full-Stack-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1286d8
commit efc1088
Showing
28 changed files
with
443 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...ava/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/AddExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.util.HashMap; | ||
|
||
public class AddExpression extends SymbolExpression { | ||
public AddExpression(Expression left, Expression right) { | ||
super(left, right); | ||
} | ||
|
||
//把左右两个表达式运算的结果加起来 | ||
public int interpreter(HashMap<String, Integer> var) { | ||
return super.left.interpreter(var) + super.right.interpreter(var); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/Calculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.util.HashMap; | ||
import java.util.Stack; | ||
|
||
public class Calculator { | ||
//定义表达式 | ||
private Expression expression; | ||
|
||
//构造函数传参,并解析 | ||
public Calculator(String expStr) { | ||
//定义一个栈,安排运算的先后顺序 | ||
Stack<Expression> stack = new Stack<>(); | ||
//表达式拆分为字符数组 | ||
char[] charArray = expStr.toCharArray(); | ||
//运算 | ||
Expression left; | ||
Expression right; | ||
for (int i = 0; i < charArray.length; i++) { | ||
switch (charArray[i]) { | ||
case '+': //加法 | ||
//加法结果放到栈中 | ||
left = stack.pop(); | ||
right = new VarExpression(String.valueOf(charArray[++i])); | ||
stack.push(new AddExpression(left, right)); | ||
break; | ||
case '-': | ||
left = stack.pop(); | ||
right = new VarExpression(String.valueOf(charArray[++i])); | ||
stack.push(new SubExpression(left, right)); | ||
break; | ||
default: //公式中的变量 | ||
stack.push(new VarExpression(String.valueOf(charArray[i]))); | ||
} | ||
} | ||
//把运算结果抛出来 | ||
this.expression = stack.pop(); | ||
} | ||
|
||
//开始运算 | ||
public int run(HashMap<String, Integer> var) { | ||
return this.expression.interpreter(var); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/Expression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.util.HashMap; | ||
|
||
public abstract class Expression { | ||
//解析公式和数值,其中var中的key值是公式中的参数,value值是具体的数字 | ||
public abstract int interpreter(HashMap<String, Integer> var); | ||
} |
13 changes: 13 additions & 0 deletions
13
...ava/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/SubExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.util.HashMap; | ||
|
||
public class SubExpression extends SymbolExpression { | ||
public SubExpression(Expression left,Expression right){ | ||
super(left,right); | ||
} | ||
//左右两个表达式相减 | ||
public int interpreter(HashMap<String, Integer> var) { | ||
return super.left.interpreter(var) - super.right.interpreter(var); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/SymbolExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
public abstract class SymbolExpression extends Expression { | ||
protected Expression left; | ||
protected Expression right; | ||
|
||
//所有的解析公式都应只关心自己左右两个表达式的结果 | ||
public SymbolExpression(Expression left, Expression right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ava/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/VarExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.util.HashMap; | ||
|
||
public class VarExpression extends Expression { | ||
|
||
private String key; | ||
|
||
public VarExpression(String key) { | ||
this.key = key; | ||
} | ||
|
||
//从map中取之 | ||
public int interpreter(HashMap<String, Integer> var) { | ||
return var.get(this.key); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/interpreter/ZTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.heibaiying.behavioral.interpreter; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
|
||
public class ZTest { | ||
//运行四则运算 | ||
public static void main(String[] args) throws IOException { | ||
String expStr = getExpStr(); | ||
//赋值 | ||
HashMap<String, Integer> var = getValue(expStr); | ||
Calculator cal = new Calculator(expStr); | ||
System.out.println("运算结果为:" + expStr + "=" + cal.run(var)); | ||
} | ||
|
||
//获得表达式 | ||
public static String getExpStr() throws IOException { | ||
System.out.print("请输入表达式:"); | ||
return (new BufferedReader(new InputStreamReader(System.in))).readLine(); | ||
} | ||
|
||
//获得值映射 | ||
public static HashMap<String, Integer> getValue(String exprStr) throws IOException { | ||
HashMap<String, Integer> map = new HashMap<String, Integer>(); | ||
//解析有几个参数要传递 | ||
for (char ch : exprStr.toCharArray()) { | ||
if (ch != '+' && ch != '-') { | ||
//解决重复参数的问题 | ||
if (!map.containsKey(String.valueOf(ch))) { | ||
String in = (new BufferedReader(new InputStreamReader(System.in))).readLine(); | ||
map.put(String.valueOf(ch), Integer.valueOf(in)); | ||
} | ||
} | ||
} | ||
return map; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Book { | ||
private String name; | ||
|
||
public Book(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/BookIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
import java.util.List; | ||
|
||
public class BookIterator implements Iterator<Book> { | ||
|
||
private List<Book> bookList; | ||
private int position = 0; | ||
|
||
public BookIterator(List<Book> bookList) { | ||
this.bookList = bookList; | ||
} | ||
|
||
@Override | ||
public Book next() { | ||
return bookList.get(position++); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return position < bookList.size(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/Bookshelf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
/** | ||
* 书柜 | ||
*/ | ||
public interface Bookshelf { | ||
|
||
void addBook(Book book); | ||
|
||
void removeBook(Book book); | ||
|
||
BookIterator iterator(); | ||
} |
27 changes: 27 additions & 0 deletions
27
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/BookshelfImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 书柜 | ||
*/ | ||
public class BookshelfImpl implements Bookshelf { | ||
|
||
private List<Book> bookList = new ArrayList<>(); | ||
|
||
@Override | ||
public void addBook(Book book) { | ||
bookList.add(book); | ||
} | ||
|
||
@Override | ||
public void removeBook(Book book) { | ||
bookList.remove(book); | ||
} | ||
|
||
@Override | ||
public BookIterator iterator() { | ||
return new BookIterator(bookList); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
public interface Iterator<E> { | ||
E next(); | ||
boolean hasNext(); | ||
} |
14 changes: 14 additions & 0 deletions
14
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/iterator/ZTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.heibaiying.behavioral.iterator; | ||
|
||
public class ZTest { | ||
public static void main(String[] args) { | ||
BookshelfImpl bookshelf = new BookshelfImpl(); | ||
bookshelf.addBook(new Book("Java书籍")); | ||
bookshelf.addBook(new Book("Python书籍")); | ||
bookshelf.addBook(new Book("Go书籍")); | ||
BookIterator iterator = bookshelf.iterator(); | ||
while (iterator.hasNext()) { | ||
System.out.println(iterator.next()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/observer/Business.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.heibaiying.behavioral.observer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Business implements Observable { | ||
|
||
private List<Observer> observerList = new ArrayList<>(); | ||
|
||
@Override | ||
public void addObserver(Observer observer) { | ||
observerList.add(observer); | ||
} | ||
|
||
@Override | ||
public void removeObserver(Observer observer) { | ||
observerList.remove(observer); | ||
} | ||
|
||
@Override | ||
public void notifyObservers(String message) { | ||
for (Observer observer : observerList) { | ||
observer.receive(message); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/observer/Observable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.heibaiying.behavioral.observer; | ||
|
||
public interface Observable { | ||
|
||
void addObserver(Observer observer); | ||
|
||
void removeObserver(Observer observer); | ||
|
||
void notifyObservers(String message); | ||
} |
5 changes: 5 additions & 0 deletions
5
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/observer/Observer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.heibaiying.behavioral.observer; | ||
|
||
public interface Observer { | ||
void receive(String message); | ||
} |
18 changes: 18 additions & 0 deletions
18
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/observer/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.heibaiying.behavioral.observer; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class User implements Observer { | ||
|
||
private String name; | ||
|
||
public User(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void receive(String message) { | ||
System.out.println(getName() + "收到消息:" + message); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/observer/ZTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.heibaiying.behavioral.observer; | ||
|
||
public class ZTest { | ||
|
||
public static void main(String[] args) { | ||
Business business = new Business(); | ||
business.addObserver(new User("用户1")); | ||
business.addObserver(new User("用户2")); | ||
business.addObserver(new User("用户3")); | ||
business.notifyObservers("商品促销通知"); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/strategy/BonusStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public class BonusStrategy implements Strategy { | ||
@Override | ||
public void execute() { | ||
System.out.println("奖金激励"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/strategy/Company.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public class Company { | ||
|
||
private Strategy strategy; | ||
|
||
public Company setStrategy(Strategy strategy) { | ||
this.strategy = strategy; | ||
return this; | ||
} | ||
|
||
public void execute() { | ||
strategy.execute(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/strategy/Strategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public interface Strategy { | ||
void execute(); | ||
} |
8 changes: 8 additions & 0 deletions
8
.../Java/design-pattern/src/main/java/com/heibaiying/behavioral/strategy/TravelStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public class TravelStrategy implements Strategy { | ||
@Override | ||
public void execute() { | ||
System.out.println("集体旅游"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...design-pattern/src/main/java/com/heibaiying/behavioral/strategy/WorkOvertimeStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public class WorkOvertimeStrategy implements Strategy { | ||
@Override | ||
public void execute() { | ||
System.out.println("奖励加班"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
code/Java/design-pattern/src/main/java/com/heibaiying/behavioral/strategy/ZTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.heibaiying.behavioral.strategy; | ||
|
||
public class ZTest { | ||
public static void main(String[] args) { | ||
// 营业额 | ||
int turnover = Integer.parseInt(args[0]); | ||
Company company = new Company(); | ||
if (turnover > 1000) { | ||
company.setStrategy(new BonusStrategy()).execute(); | ||
} else if (turnover > 100) { | ||
company.setStrategy(new TravelStrategy()).execute(); | ||
} else { | ||
company.setStrategy(new WorkOvertimeStrategy()).execute(); | ||
} | ||
} | ||
} |
Oops, something went wrong.