Skip to content

Commit

Permalink
Java设计模式
Browse files Browse the repository at this point in the history
  • Loading branch information
heibaiying committed Dec 5, 2019
1 parent f1286d8 commit efc1088
Show file tree
Hide file tree
Showing 28 changed files with 443 additions and 0 deletions.
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);
}
}
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);
}
}
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);
}
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);
}
}
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;
}
}
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);
}
}
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;
}
}
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;
}

}
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();
}
}
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();
}
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);
}
}
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();
}
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());
}
}
}
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);
}
}
}
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);
}
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);
}
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);
}
}
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("商品促销通知");
}

}
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("奖金激励");
}
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.heibaiying.behavioral.strategy;

public interface Strategy {
void execute();
}
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("集体旅游");
}
}
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("奖励加班");
}
}
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();
}
}
}
Loading

0 comments on commit efc1088

Please sign in to comment.