✨ 委派者模式
parent
7486f84265
commit
49899fac8f
@ -0,0 +1,14 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:28
|
||||
*/
|
||||
public class CodeEmployee implements Employee<CodingTask> {
|
||||
|
||||
|
||||
@Override
|
||||
public void doAction(CodingTask task) {
|
||||
System.out.println("码农开始:" + task.getTask());
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:27
|
||||
*/
|
||||
public class CodingTask implements Task<String> {
|
||||
|
||||
|
||||
@Override
|
||||
public String getTask() {
|
||||
return "实现需求";
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 委派者
|
||||
*
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:23
|
||||
*/
|
||||
public class Delegate implements Employee<Task<?>> {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static final Map<Class<?>,Employee> MAP = new HashMap<>();
|
||||
|
||||
static {
|
||||
Employee<TestingTask> testEmployee = new TestEmployee();
|
||||
MAP.put(TestingTask.class,testEmployee);
|
||||
MAP.put(CodingTask.class,new CodeEmployee());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void doAction(Task<?> task) {
|
||||
MAP.get(task.getClass()).doAction(task);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* 行为
|
||||
*
|
||||
* @author robin
|
||||
* @date 2022/5/9 16:26
|
||||
*/
|
||||
public interface Employee<T extends Task<?>> {
|
||||
|
||||
/**
|
||||
* 干活
|
||||
*
|
||||
* @param task 任务
|
||||
*/
|
||||
void doAction(T task);
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:46
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Delegate delegate = new Delegate();
|
||||
CodingTask codingTask = new CodingTask();
|
||||
delegate.doAction(codingTask);
|
||||
delegate.doAction(new TestingTask());
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* 委派模式
|
||||
*
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:23
|
||||
*/
|
||||
public class PackageInfo {
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:24
|
||||
*/
|
||||
public interface Task<T> {
|
||||
|
||||
/**
|
||||
* task 任务
|
||||
*
|
||||
* @return 任务
|
||||
*/
|
||||
T getTask();
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:31
|
||||
*/
|
||||
public class TestEmployee implements Employee<TestingTask>{
|
||||
@Override
|
||||
public void doAction(TestingTask task) {
|
||||
System.out.println("测试:"+task.getTask());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.alis.smallcc.designpatterns.behavior.delegate;
|
||||
|
||||
/**
|
||||
* @author lc
|
||||
* @date 2022/5/9 16:28
|
||||
*/
|
||||
public class TestingTask implements Task<String> {
|
||||
@Override
|
||||
public String getTask() {
|
||||
return "完成测试";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue