常用Design Patten實作
常用pattern不分類
多工類
AlwAysAliveWorkPool:常駐工人池
@Test
void testExecuteWhen2() {
OrderQueue<BlockItem> blockQueue = new OrderQueue<>();
IWorkPool pool = new AlwAysAliveWorkPool<AlwAysAliveWorkPoolTest.BlockItem>(3, "Pool", blockQueue, this, this);
List<BlockItem> collect = IntStream.range(0, 10).mapToObj(i -> new BlockItem(i)).collect(Collectors.toList());
for (BlockItem item : collect) {
blockQueue.add(item);
}
pool.start();
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("close");
pool.close();
log.info("end");
}
BlockQueue:堵塞池
@Test
void test() throws InterruptedException {
final List<String> getDatas = Lists.newCopyOnWriteArrayList();//tak
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < size; i++) {
try {
String take = blockQueue.take();
log.info("task:{}", take);
getDatas.add(take);
TimeUnit.SECONDS.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
log.info("InterruptedException");
}
}
}
});
thread.start();
thread.join();
log.info("end ");
assertThat(getDatas).contains(lines.toArray(new String[]{}));
assertThat(getDatas).hasSize(size);
}
CountDownLatchWorkPool:倒數門閂
WorkLatchService:工人門閂
@Test
void giveInteruptWhenExecuteThenClosePoool() throws InterruptedException {
Thread thread = new Thread(() -> execute(3));
thread.start();
TimeUnit.MILLISECONDS.sleep(100);
log.info("call interrupt");
thread.interrupt();
thread.join();
log.info("end");
assertThat(value).isGreaterThan(0);
assertThat(value).isLessThan(100);
// TimeUnit.SECONDS.sleep(3);
}
Event Looop
DynamicRouter:動態多工路由器
@Test
void test() {
try (final AsynDynamicRouter dynamicRouter = new AsynDynamicRouter("Test", 3)) {
final SampleInputChannel sampleChannel = new SampleInputChannel(dynamicRouter);
final SampleOutputChannel sampleOutputChannel = new SampleOutputChannel();
dynamicRouter.registerChannel(SampleInputMessage.class, sampleChannel);
dynamicRouter.registerChannel(SampleOutputMessage.class, sampleOutputChannel);
final SampleInputMessage sampleInputMessage = new SampleInputMessage(1, 3);
for (int i = 0; i < 10; i++) {
dynamicRouter.dispatch(sampleInputMessage);
}
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e1) {
log.info("timeout", e1);
}
log.info("end job");
}
EventBus
其他
StatusMachine 有限狀態機+整合命令模型
@Test
void test_MaryFeel_IsSand_to_walk_then_feel_happy() {
// GIVE
NoBody nobody = new NoBody("MARK", FeelCodes.SAD.name());
// WHEN
this.moodbehavior.run(ActionCodes.WALK, nobody);
// THEN
assertThat(nobody.getStatus()).isEqualTo(FeelCodes.HAPPY.name());
}
Observable:觀察者模型
@Test
public void test_goIn_then_student_update() {
this.teacher.rollCall();
Mockito.verify(tom, Mockito.times(1)).update(Mockito.any());
Mockito.verify(mark, Mockito.times(1)).update(Mockito.any());
}
<dependency>
<groupId>io.github.h8000572003</groupId>
<artifactId>commons-design</artifactId>
<version>0.0.5</version>
</dependency>
Last updated
Was this helpful?