码迷,mamicode.com
首页 > 其他好文 > 详细

重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)

时间:2014-09-13 10:43:45      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   os   使用   java   ar   

场景:


你有一个类型码,它会影响类的行为,但你无法通过继承手法来消除它
,可以使用状态对象取代类型码


类图:


bubuko.com,布布扣


修改前:


Student

/**
 * @file Student.java
 * 
 * 
 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.refactor.state.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.demo.refactor.state.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;
	private StudentType type ;
	public Student(int id, String name,StudentType 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.getType();
	}
	
	public void setType(int type) {
		//调用工厂方法
		this.type = StudentType.create(type);
	}
	
}

StudentType

package com.demo.refactor.state.after;

public abstract class StudentType {
	public abstract int getType();
	public static StudentType create(int type){
		switch (type) {
		case Student.A:
			return new TypeA();
		case Student.B:
			return new TypeB();
		default:
			throw new IllegalArgumentException();
		}
	}
}

TypeA

/**
 * 
 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.refactor.state.after;

/**
 * @author wumingkun
 *
 */
public class TypeA extends StudentType {



	/* (non-Javadoc)
	 * @see com.refractor.subcode.after.StudentManagement#getType()
	 */
	@Override
	public int getType() {
		return Student.A;
	}

	@Override
	public String toString() {
		return "TypeA [type=" + getType() + "]";
	}
	
}

TypeB

</pre><pre name="code" class="java">/**
 * 
 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.refactor.state.after;

/**
 * @author wumingkun
 *
 */
public class TypeB extends StudentType {



	/* (non-Javadoc)
	 * @see com.refractor.subcode.after.StudentManagement#getType()
	 */
	@Override
	public int getType() {
		return Student.B;
	}

	@Override
	public String toString() {
		return "TypeB [type=" + getType() + "]";
	}
	
}



重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)

标签:des   style   blog   http   io   os   使用   java   ar   

原文地址:http://blog.csdn.net/wobendiankun/article/details/39249219

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!