重构之3.Replace Type Code with Subclasses(以子类取代类型码)
来源:互联网 发布:人工智能人才培养 编辑:程序博客网 时间:2024/06/05 08:43
场景:
通常我们会在业务层判断类型码,执行不同的方法,可以使用子类来取代类型码
前提:
1.类型码不会被改变
2.类型码所属的类没有子类
修改前:
Student:
/** * @file Student.java * * * @author wumingkun * @version 1.0.0 * @Description */package com.refractor.subcode.before;/** * @author wumingkun * */public class Student {private int id;private String name;private int type;public static final int A =1;public static final int B =2;public Student(int id, String name, int type) {super();this.id = id;this.name = name;this.type = type;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getType() {return type;}public void setType(int type) {this.type = type;}}
修改后:
Student:
/** * @file Student.java * * * @author wumingkun * @version 1.0.0 * @Description */package com.refractor.subcode.after;/** * @author wumingkun * */public abstract class Student {private int id;private String name;public static final int A =1;public static final int B =2;public Student(int id, String name) {super();this.id = id;this.name = name;}public static Student create(int type){switch (type) {case A:return new TypeA(1,"张三");case B:return new TypeB(2,"李四");default:throw new IllegalArgumentException();}}public abstract int getType();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
TypeA:/** * * @author wumingkun * @version 1.0.0 * @Description */package com.refractor.subcode.after;/** * @author wumingkun * */public class TypeA extends Student {/** * @param id * @param name */public TypeA(int id, String name) {super(id, name);}/* (non-Javadoc) * @see com.refractor.subcode.after.StudentManagement#getType() */@Overridepublic int getType() {return A;}@Overridepublic String toString() {return "TypeA [type=" + getType() + ", id=" + getId()+ ", name=" + getName() + "]";}}
TypeB:/** * * @author wumingkun * @version 1.0.0 * @Description */package com.refractor.subcode.after;/** * @author wumingkun * */public class TypeB extends Student {/** * @param id * @param name */public TypeB(int id, String name) {super(id, name);}/* (non-Javadoc) * @see com.refractor.subcode.after.StudentManagement#getType() */@Overridepublic int getType() {return B;}}StudentTest:
/** * @file StudentTest.java * * @author wumingkun * @version 1.0.0 * @Description */package com.refractor.subcode.after;/** * @author wumingkun * */public class StudentTest {/** * @param args */public static void main(String[] args) {Student student=Student.create(Student.A);System.out.println(student);}}
1 0
- 重构之3.Replace Type Code with Subclasses(以子类取代类型码)
- 8.14 replace type code with subclasses (以子类取代类型码)
- Replace Type Code with Subclasses(以子类取代型别码)
- 重新组织数据之十四 :Replace Type Code with Subclasses(以子类取代型别码)
- 重构手法31:Replace Type Code with Subclass (以子类取代类型码)
- 重构之2.Replace Type Code with Class(以类取代类型码)
- 重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)
- 重构手法30:Replace Type Code with Class (以类取代类型码)
- 8.13 replace type code with class(以类取代类型码)
- 8.15 replace type code with state/strategy (以state/strategy取代类型码)
- Replace Type Code with Class(以类取代型别码)
- 重新组织数据之十三 :Replace Type Code with Class(以类取代型别码)
- 重构 -- Replace Subclass with Fields(以值域取代子类)
- replace type code with class(重构)用类替换类型码
- 重新组织数据之十五 :Replace Type Code with State/Strategy(以State/strategy 取代型别码)
- Replace Type Code with State/Strategy(以State/Strategy取代型别码)
- 重构-代替类型代码几中方式的比较(Replace type code with ...)
- 重构-代替类型代码几中方式的比较(Replace type code with ...)
- 使用动画和fragment改善Android表单
- 点分十进制子网掩码和CIDR的相互转换
- java.系统自动生成一个1-100之间的整数,请猜出这个数
- SpringMVC框架搭建
- matlab 声卡 信号源
- 重构之3.Replace Type Code with Subclasses(以子类取代类型码)
- 判断一个IP是否在指定的网络中
- Spring3.x--SpringMVC
- ThoughtWorks开源了Go持续交付平台
- Struts常见问题
- n对括号匹配的种类
- java 锁的类型和性质(二):自旋锁的其它种类
- 通过HttpURLConnection模拟post表单提交
- 用输出半角和全角字符