重构之switch惊悚现身

未重构之前的代码

public class Employee

{

private int _type;

static final int ENGINEER = 0;

static final int SALESMAN = 1;

static final int MANAGER = 2

Employee(int type)

{

_type = type;

}

int payAmout()

{

switch (_type){

  case ENGINEER:

  return 1;

  case SALESMAN:

  return 2;

  case MANAGER:

  return 3;

  default:

  throw new RuntimeException("error");

}

}

}

第一步,我们进行自封装字段,代码变为:

public class Employee

{

private int _type;

static final int ENGINEER = 0;

static final int SALESMAN = 1;

static final int MANAGER = 2;

Employee(int type)

{

setType(type);

}

int payAmout()

{

switch (getType()){

  case ENGINEER:

  return 1;

  case SALESMAN:

  return 2;

  case MANAGER:

  return 3;

  default:

  throw new RuntimeException("error");

}

}

void setType(int type)

{

_type = type;

}

int getType()

{

return _type;

}

}

第二步,我们增加间接层

public abstract class EmployeeType {

abstract int getTypeCode();

}

第三部,创建间接层子类

public class Engineer extends EmployeeType {

 

int getTypeCode(){

return Employee.ENGINEER;

}

}

public class Manager extends EmployeeType {

 

int getTypeCode() {

return Employee.MANAGER;

}

 

}

public class SalesMan extends EmployeeType

{

       int getTypeCode()

       {

          return Employee.SALESMAN;

       }

}

第四步,用声明的间接层去替换Employee中的类型码成员变量

 

public class Employee

{

EmployeeType _type;

static final int ENGINEER = 0;

static final int SALESMAN = 1;

static final int MANAGER = 2;

Employee(int arg)

{

setType(arg);

}

int payAmout(int arg)

{

switch (arg){

  case ENGINEER:

  return 1;

  case SALESMAN:

  return 2;

  case MANAGER:

  return 3;

  default:

  throw new RuntimeException("error");

}

}

void setType(int arg)

{

switch(arg){

case ENGINEER:

_type = new Engineer();

break;

case MANAGER:

_type = new Manager();

break;

case SALESMAN:

_type = new SalesMan();

break;

default:

throw new RuntimeException("code error");

}

}

int getType()

{

return _type.getTypeCode();

}

}

第五步,建立一个工厂函数,来生产EmployeeType实例

public abstract class EmployeeType {

abstract int getTypeCode();

static final int ENGINEER = 0;

static final int SALESMAN = 1;

static final int MANAGER = 2;

static EmployeeType newType(int code)

{

switch(code){

case ENGINEER:

   return new Engineer();

case SALESMAN:

return new SalesMan();

case MANAGER:

return new Manager();

default:

throw new RuntimeException("wrongcode");

}

}

}

第六步,去掉Employee当中的类型码

public class Employee

{

EmployeeType _type;

Employee(int arg)

{

setType(arg);

}

int payAmout()

{

switch (getType()){

  case EmployeeType.ENGINEER:

  return 1;

  case EmployeeType.MANAGER:

  return 2;

  case EmployeeType.SALESMAN:

  return 3;

  default:

  throw new RuntimeException("error");

}

}

void setType(int arg)

{

_type = EmployeeType.newType(arg);

}

int getType()

{

return _type.getTypeCode();

}

}

第七步,将payAmout函数提到它应该在的地方

public abstract class EmployeeType {

abstract int getTypeCode();

static final int ENGINEER = 0;

static final int SALESMAN = 1;

static final int MANAGER = 2;

static EmployeeType newType(int code)

{

switch(code){

case ENGINEER:

   return new Engineer();

case SALESMAN:

return new SalesMan();

case MANAGER:

return new Manager();

default:

throw new RuntimeException("wrongcode");

}

}

int payAmout()

{

switch (getTypeCode()){

  case ENGINEER:

  return 1;

  case MANAGER:

  return 2;

  case SALESMAN:

  return 3;

  default:

  throw new RuntimeException("error");

}

}

}

第八步,将payAccout声明为abstract,然后各子类分别实现这个方法

public class Engineer extends EmployeeType {

int getTypeCode(){

return ENGINEER;

}

int payAccount(){

return 2;

}

}

public class Manager extends EmployeeType {

int getTypeCode() {

return MANAGER;

}

int payAccount()

{

return 3;

}

}

public class SalesMan extends EmployeeType {

int getTypeCode() {

return SALESMAN;

}

int payAccount()

{

return 1;

}

}

第九步,修改Employee的payAccount函数

public class Employee

{

EmployeeType _type;

Employee(int arg)

{

setType(arg);

}

int payAmout()

{

return _type.payAccount();

}

void setType(int arg)

{

_type = EmployeeType.newType(arg);

}

int getType()

{

return _type.getTypeCode();

}

}

猜你喜欢

转载自kedazhaoguowei.iteye.com/blog/1972290