/**
* 抽象表达式:定义所有终结符表达式和非终结符表达式的解释操作
*
* @author huaisf
*
*/
public interface Expression {
public boolean interpret(String value);
}
新建非终结符表达式NonterminalExpression
/**
* 非终结符表达式:实现非终结操作
*
* @author huaisf
*
*/
public class NonterminalExpression implements Expression {
private Expression terminalExpression = null;
public NonterminalExpression(Expression terminalExpression) {
this.terminalExpression = terminalExpression;
}
public boolean interpret(String value) {
// 没有非终结符,直接调用终结符表达式
return terminalExpression.interpret(value);
}
}
新建终结符表达式TerminalExpression
/**
* 终结符表达式:实现终结操作
*
* @author huaisf
*
*/
public class TerminalExpression implements Expression {
private Map map = new HashMap();
public TerminalExpression(String rule) {
int index = rule.indexOf(":");
String city = rule.substring(0, index);
String scenery = rule.substring(index + 1, rule.length());
map.put(city, scenery);
}
@Override
public boolean interpret(String value) {
int index = value.indexOf(":");
String city = value.substring(0, index);
String scenery = value.substring(index + 1, value.length());
String existScenery = map.get(city);
if (existScenery != null && existScenery.equals(scenery)) {
return true;
}
return false;
}
}
新建上下文Context
/**
* 上下文:完成终结表达式初始化,并对要解释的数据进行中转
*
* @author huaisf
*
*/
public class Context {
private Expression nonterminalExpression;
public Context(String rule) {
Expression terminalExpression = new TerminalExpression(rule);
nonterminalExpression = new NonterminalExpression(terminalExpression);
}
public void matches(String value) {
boolean isExist = nonterminalExpression.interpret(value);
if (isExist) {
System.out.println("城市与景区对应成功");
return;
}
System.out.println("城市与景区对应失败");
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
// 定义规则为"杭州:西湖"进行验证
Context context = new Context("杭州:西湖");
context.matches("杭州:西湖");
context.matches("苏州:西湖");
context.matches("苏州:太湖");
}
}
public class Test {
public static void main(String[] args) {
// 生产公牛插排
BullSocket bullSocket = new BullSocket();
bullSocket.create();
// 生成tcl插排
TCLSocket tclSocket = new TCLSocket();
tclSocket.create();
}
}
/**
* 具体处理者:对抽象处理者定义的接口进行实现,如果无法处理则将请求传递给下一个处理者
*
* @author huaisf
*
*/
public class OriginalPriceSocketHandler extends AbstractSocketHandler {
@Override
void buySocket(Integer count) {
if (count <= 500) {
System.out.println("原价购买插排");
return;
}
this.getNextHandler().buySocket(count);
}
}
public class NineDiscountSocketHandler extends AbstractSocketHandler {
@Override
void buySocket(Integer count) {
if (count > 500 && count <= 5000) {
System.out.println("九折购买插排");
return;
}
this.getNextHandler().buySocket(count);
}
}
public class EightDiscountSocketHandler extends AbstractSocketHandler {
@Override
void buySocket(Integer count) {
if (count > 5000) {
System.out.println("八折购买插排");
return;
}
this.getNextHandler().buySocket(count);
}
}
新建测试类Test组装调用
public class Test {
public static void main(String[] args) {
// 组装责任链
OriginalPriceSocketHandler originalPriceSocket = new OriginalPriceSocketHandler();
NineDiscountSocketHandler nineDiscountSocket = new NineDiscountSocketHandler();
originalPriceSocket.setNextHandler(nineDiscountSocket);
nineDiscountSocket.setNextHandler(new EightDiscountSocketHandler());
originalPriceSocket.buySocket(50000);
}
}
/**
* 具体命令:对定义的抽象命令实现
*
* @author huaisf
*
*/
public class GroupChatCommand implements Command {
private ChatRecord chatRecord;
public GroupChatCommand() {
chatRecord = new ChatRecord();
}
@Override
public void execute() {
System.out.println("群聊消息发送给房间内所有人");
chatRecord.save();
}
}
public class PrivateChatCommand implements Command {
private ChatRecord chatRecord;
public PrivateChatCommand() {
chatRecord = new ChatRecord();
}
@Override
public void execute() {
System.out.println("私聊记录发送到对应人");
chatRecord.save();
}
}
新建接受者ChatRecord
/**
* 接收者:对真正业务进行处理
*
* @author huaisf
*
*/
public class ChatRecord {
public void save() {
System.out.println("保存聊天记录成功");
}
}
新建调用者CommandManager
/**
* 调用者:调用具体命令进行处理
*
* @author huaisf
*
*/
public class CommandManager {
public static HashMap map = new HashMap<>();
static {
map.put(ChatTypeEnum.GROUP_CHAT.name(), new GroupChatCommand());
map.put(ChatTypeEnum.PRIVATE_CHAT.name(), new PrivateChatCommand());
}
public void saveRecord(ChatTypeEnum chatTypeEnum) {
Command command = map.get(chatTypeEnum.name());
command.execute();
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
CommandManager commandManager = new CommandManager();
// 群聊消息
commandManager.saveRecord(ChatTypeEnum.GROUP_CHAT);
// 私聊消息
commandManager.saveRecord(ChatTypeEnum.PRIVATE_CHAT);
}
}
/**
* 具体容器:抽象容器的实现,对数据进行存储,本容器为数组容器
*
* @author huaisf
*
*/
public class ArrayContainer implements AbstractContainer {
private static Object[] array = new Object[0];
private int size = 0;
@Override
public void add(Object object) {
Object[] temp = new Object[size + 1];
System.arraycopy(array, 0, temp, 0, size);
temp[size] = object;
array = temp;
size += 1;
}
@Override
public void delete(int index) {
Object[] temp = new Object[size - 1];
System.arraycopy(array, 0, temp, 0, index);
System.arraycopy(array, index + 1, temp, index, size - index - 1);
array = temp;
size -= 1;
}
@Override
public AbstractIterator iterator() {
return new ArrayIterator(array);
}
}
/**
* 具体容器:抽象容器的实现,对数据进行存储,本容器为集合容器
*
* @author huaisf
*
*/
public class ListContainer implements AbstractContainer {
List list = new ArrayList();
@Override
public void add(Object object) {
list.add(object);
}
@Override
public void delete(int index) {
list.remove(index);
}
@Override
public AbstractIterator iterator() {
return new ListIterator(list);
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
// 使用list迭代器
ListContainer listContainer = new ListContainer();
for (int i = 0; i < 5; i++) {
listContainer.add(i);
}
AbstractIterator listIterator = listContainer.iterator();
while (listIterator.hashNext()) {
System.out.println(listIterator.next());
}
// 使用数组迭代器
ArrayContainer arrayContainer = new ArrayContainer();
for (int i = 5; i < 10; i++) {
arrayContainer.add(i);
}
AbstractIterator arrayIterator = arrayContainer.iterator();
while (arrayIterator.hashNext()) {
System.out.println(arrayIterator.next());
}
}
}
/**
* 具体中介者:实现抽象中介者定义方法,对同事请求进行处理
*
* @author huaisf
*
*/
public class HouseMediator extends AbstractMediator {
@Override
public void contact(OperationTypeEnum type) {
if (OperationTypeEnum.BUY == type) {
super.sellHouseUser.receive();
} else if (OperationTypeEnum.SELL == type) {
super.buyHouseUser.receive();
}
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
AbstractMediator mediator = new HouseMediator();
BuyHouseUser buyHouseUser = new BuyHouseUser(mediator);
SellHouseUser sellHouseUser = new SellHouseUser(mediator);
mediator.setBuyHouseUser(buyHouseUser);
mediator.setSellHouseUser(sellHouseUser);
buyHouseUser.send();
sellHouseUser.send();
}
}
/**
* 具体主题:对抽象观察者定义进行实现
*
* @author huaisf
*
*/
public class ConcreteSubject implements AbstractSubject {
List list = new ArrayList<>();
@Override
public void addObserver(AbstractObserver abstractObserver) {
list.add(abstractObserver);
}
@Override
public void noticeObserver() {
for (AbstractObserver abstractObserver : list) {
abstractObserver.update();
}
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
AbstractObserver observerA = new ConcreteObserverA();
AbstractObserver observerB = new ConcreteObserverB();
AbstractSubject subject = new ConcreteSubject();
// 添加观察者
subject.addObserver(observerA);
subject.addObserver(observerB);
// 对观察者进行通知
subject.noticeObserver();
}
}
/**
* 上下文:定义外部调用方法,存储当前状态
*
* @author huaisf
*
*/
public class Context {
private DoorState doorState;
public Context(DoorState doorState) {
this.doorState = doorState;
}
public DoorState getDoorState() {
return doorState;
}
public void setDoorState(DoorState doorState) {
this.doorState = doorState;
}
public void switchOperation() {
doorState.switchOperation(this);
}
}
新建抽象状态DoorState
/**
* 抽象状态:定义不同状态的操作
*
* @author huaisf
*
*/
public abstract class DoorState {
/**
* 开关操作
*/
public abstract void switchOperation(Context context);
}
新建具体状态CloseDoorState与OpenDoorState
/**
* 具体状态:对抽象状态进行实现,在需要时进行状态切换
*
* @author huaisf
*
*/
public class CloseDoorState extends DoorState {
@Override
public void switchOperation(Context context) {
System.out.println("关门");
// 设置下一次状态为开门
context.setDoorState(new OpenDoorState());
}
}
public class OpenDoorState extends DoorState {
@Override
public void switchOperation(Context context) {
System.out.println("开门");
// 设置下一次状态为关门
context.setDoorState(new CloseDoorState());
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
Context context = new Context(new OpenDoorState());
context.switchOperation();
context.switchOperation();
context.switchOperation();
context.switchOperation();
}
}
/**
* 具体策略:对抽象策略进行实现
*
* @author huaisf
*
*/
public class OriginalPriceStrategy implements DiscountStrategy {
@Override
public void buy() {
System.out.println("原价购买");
}
}
public class EightDiscountStrategy implements DiscountStrategy {
@Override
public void buy() {
System.out.println("八折购买");
}
}
新建上下文Context
/**
* 上下文:定义外部调用方法
*
* @author huaisf
*
*/
public class Context {
private DiscountStrategy discountStrategy;
public Context(DiscountStrategy discountStrategy) {
this.discountStrategy = discountStrategy;
}
public void buy() {
discountStrategy.buy();
}
}
新建测试类Test
public class Test {
public static void main(String[] args) {
// 没有促销互动,原价购买商品
Context originalContext = new Context(new OriginalPriceStrategy());
originalContext.buy();
// 厂家促销,商品八折
Context eightContext = new Context(new EightDiscountStrategy());
eightContext.buy();
}
}
1.java.util.Timer.schedule(TimerTask task, long delay):多长时间(毫秒)后执行任务
2.java.util.Timer.schedule(TimerTask task, Date time):设定某个时间执行任务
3.java.util.Timer.schedule(TimerTask task, long delay,longperiod
java.lang.UnsupportedClassVersionError: cn/support/cache/CacheType : Unsupported major.minor version 51.0 (unable to load class cn.support.cache.CacheType)
at org.apache.catalina.loader.WebappClassL
昨天发了一个提问,启动5个线程将一个List中的内容,然后将5个线程的内容拼接起来,由于时间比较急迫,自己就写了一个Demo,希望对菜鸟有参考意义。。
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public c
select list.listname, list.createtime,listcount from dream_list as list , (select listid,count(listid) as listcount from dream_list_user group by listid order by count(
此文转自IBM.
Apache 服务简介
Web 服务器也称为 WWW 服务器或 HTTP 服务器 (HTTP Server),它是 Internet 上最常见也是使用最频繁的服务器之一,Web 服务器能够为用户提供网页浏览、论坛访问等等服务。
由于用户在通过 Web 浏览器访问信息资源的过程中,无须再关心一些技术性的细节,而且界面非常友好,因而 Web 在 Internet 上一推出就得到
1) I love you not because of who you are, but because of who I am when I am with you. 我爱你,不是因为你是一个怎样的人,而是因为我喜欢与你在一起时的感觉。 2) No man or woman is worth your tears, and the one who is, won‘t