public class littlePenguin {
public void everyDay(){
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("用小翅膀打豆豆");}}
public class middlePenguin {
public void everyDay(){
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("用圆圆的肚子打豆豆");}}
public class bigPenguin {
public void everyDay(){
System.out.println("吃饭");
System.out.println("睡觉");
System.out.println("拿鸡毛掸子打豆豆");}}
public class test {
public static void main(String[] args){
System.out.println("littlePenguin:");
littlePenguin penguin_1 = new littlePenguin();
penguin_1.everyDay();
System.out.println("middlePenguin:");
middlePenguin penguin_2 = new middlePenguin();
penguin_2.everyDay();
System.out.println("bigPenguin:");
bigPenguin penguin_3 = new bigPenguin();
penguin_3.everyDay();}}
public class littlePenguin {
public void eating(){
System.out.println("吃饭");}
public void sleeping(){
System.out.println("睡觉");}
public void beating(){
System.out.println("用小翅膀打豆豆");}}
public class middlePenguin {
public void eating(){
System.out.println("吃饭");}
public void sleeping(){
System.out.println("睡觉");}
public void beating(){
System.out.println("用圆圆的肚子打豆豆");}}// bigPenguin相同,省略...
public class test {
public static void main(String[] args){
System.out.println("littlePenguin:");
littlePenguin penguin_1 = new littlePenguin();
penguin_1.eating();
penguin_1.sleeping();
penguin_1.beating();// 下同,省略...
}}
public abstract class penguin {
public void eating(){
System.out.println("吃饭");}
public void sleeping(){
System.out.println("睡觉");}
public abstract void beating();
public void everyDay(){
this.eating();
this.sleeping();
this.beating();}}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
每只企鹅单独实现自己 “打豆豆” 的方式:
public class littlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用小翅膀打豆豆");}}
public class middlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用圆圆的肚子打豆豆");}}
public class bigPenguin extends penguin {
@Override
public void beating(){
System.out.println("拿鸡毛掸子打豆豆");}}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
最后看调用方式:
public class test {
public static void main(String[] args){
System.out.println("littlePenguin:");
littlePenguin penguin1 = new littlePenguin();
penguin1.everyDay();
System.out.println("middlePenguin:");
middlePenguin penguin2 = new middlePenguin();
penguin2.everyDay();
System.out.println("bigPenguin:");
bigPenguin penguin3 = new bigPenguin();
penguin3.everyDay();}}
public class littlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用小翅膀打豆豆");}}
public class middlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用圆圆的肚子打豆豆");}}
public class bigPenguin extends penguin {
@Override
public void beating(){
System.out.println("拿鸡毛掸子打豆豆");}}
public class behaviorContext {
private penguin _penguin;
public behaviorContext(penguin newPenguin){
_penguin = newPenguin;}
public void setPenguin(penguin newPenguin){
_penguin = newPenguin;}
public void everyDay(){
_penguin.eating();
_penguin.sleeping();
_penguin.beating();}}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
最后看调用方式:
public class test {
public static void main(String[] args){
behaviorContext behavior = new behaviorContext(new littlePenguin());
behavior.everyDay();
behavior.setPenguin(new middlePenguin());
behavior.everyDay();
behavior.setPenguin(new bigPenguin());
behavior.everyDay();}}
public abstract class penguin {
public void eating(){
System.out.println("吃饭");}
public void sleeping(){
System.out.println("睡觉");}
public abstract void beating();
public void everyDay(){
this.eating();
this.sleeping();
this.beating();}}
public class littlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用小翅膀打豆豆");}}
public class middlePenguin extends penguin {
@Override
public void beating(){
System.out.println("用圆圆的肚子打豆豆");}}
public class bigPenguin extends penguin {
@Override
public void beating(){
System.out.println("拿鸡毛掸子打豆豆");}}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
这里是工厂方法的重点,需要构建一个工厂,专门用来拿企鹅:
public class penguinFactory {
private static final Map<String, penguin> map = new HashMap<>();
static {
map.put("littlePenguin", new littlePenguin());
map.put("middlePenguin", new middlePenguin());
map.put("bigPenguin", new bigPenguin());}// 获取企鹅
public static penguin getPenguin(String name){
return map.get(name);}}
public class TaskFactory {
@Autowired
public static List<AbstractTask> taskList;
private static final Map<String, AbstractTask> map = new HashMap<>();
static {// 存放任务映射关系
map.put(AbstractTask.OPERATOR_TYPE_FROZEN, new BatchFrozenTask());
map.put(AbstractTask.OPERATOR_TYPE_REJECT, new BatchRejectTask());
map.put(AbstractTask.OPERATOR_TYPE_CANCEL, new BatchCancelTask());}
public static void main(String[] args){
String operatorType = AbstractTask.OPERATOR_TYPE_REJECT;
AbstractTask task = TaskFactory.map.get(operatorType);
ParamWrapper<CancelParams> params = new ParamWrapper<CancelParams>();
params.rowKey=11111111;
params.data= new CancelParams();
OcApiServerResponse res = task.execute(params);
System.out.println(res.toString());
return;}}