首页 > 代码库 > 重构之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(以子类取代类型码)