首页 > 代码库 > 重构之3.Replace Type Code with Subclasses(以子类取代类型码)
重构之3.Replace Type Code with Subclasses(以子类取代类型码)
场景:
通常我们会在业务层判断类型码,执行不同的方法,可以使用子类来取代类型码
前提:
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() */ @Override public int getType() { return A; } @Override public 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() */ @Override public 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); } }
重构之3.Replace Type Code with Subclasses(以子类取代类型码)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。