diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodeEmployee.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodeEmployee.java new file mode 100644 index 0000000..e3b014d --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodeEmployee.java @@ -0,0 +1,14 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * @author lc + * @date 2022/5/9 16:28 + */ +public class CodeEmployee implements Employee { + + + @Override + public void doAction(CodingTask task) { + System.out.println("码农开始:" + task.getTask()); + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodingTask.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodingTask.java new file mode 100644 index 0000000..cffa528 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/CodingTask.java @@ -0,0 +1,14 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * @author lc + * @date 2022/5/9 16:27 + */ +public class CodingTask implements Task { + + + @Override + public String getTask() { + return "实现需求"; + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Delegate.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Delegate.java new file mode 100644 index 0000000..262a77a --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Delegate.java @@ -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> { + + @SuppressWarnings("rawtypes") + private static final Map,Employee> MAP = new HashMap<>(); + + static { + Employee 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); + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Employee.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Employee.java new file mode 100644 index 0000000..fead268 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Employee.java @@ -0,0 +1,18 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * 行为 + * + * @author robin + * @date 2022/5/9 16:26 + */ +public interface Employee> { + + /** + * 干活 + * + * @param task 任务 + */ + void doAction(T task); + +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Main.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Main.java new file mode 100644 index 0000000..3435166 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Main.java @@ -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()); + + + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/PackageInfo.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/PackageInfo.java new file mode 100644 index 0000000..12fe5a9 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/PackageInfo.java @@ -0,0 +1,10 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * 委派模式 + * + * @author lc + * @date 2022/5/9 16:23 + */ +public class PackageInfo { +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Task.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Task.java new file mode 100644 index 0000000..a67b306 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/Task.java @@ -0,0 +1,15 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * @author lc + * @date 2022/5/9 16:24 + */ +public interface Task { + + /** + * task 任务 + * + * @return 任务 + */ + T getTask(); +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestEmployee.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestEmployee.java new file mode 100644 index 0000000..68c89d3 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestEmployee.java @@ -0,0 +1,13 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * @author lc + * @date 2022/5/9 16:31 + */ +public class TestEmployee implements Employee{ + @Override + public void doAction(TestingTask task) { + System.out.println("测试:"+task.getTask()); + } + +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestingTask.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestingTask.java new file mode 100644 index 0000000..6898e37 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/behavior/delegate/TestingTask.java @@ -0,0 +1,12 @@ +package org.alis.smallcc.designpatterns.behavior.delegate; + +/** + * @author lc + * @date 2022/5/9 16:28 + */ +public class TestingTask implements Task { + @Override + public String getTask() { + return "完成测试"; + } +}