> For the complete documentation index, see [llms.txt](https://andfood.gitbook.io/side-project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://andfood.gitbook.io/side-project/yuan-jian-she-ji/chang-yong-design-patten-shi-zuo.md).

# 常用Design Patten實作

### 多工類

AlwAysAliveWorkPool：常駐工人池

```java

@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：堵塞池

```java
@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：工人門閂

```java

@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：動態多工路由器

```java
@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

### 其他

&#x20;StatusMachine 有限狀態機+整合命令模型

```java
@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：觀察者模型

```java
@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());
}

```

```xml
<dependency>
  <groupId>io.github.h8000572003</groupId>
  <artifactId>commons-design</artifactId>
  <version>0.0.5</version>
</dependency>
```
