重构之在对象之间搬移特性

/**
 * 7. 在对象之间搬移特性 类往往会因为承担过多的责任 而变得臃肿不堪 
 * 7.1 Move Method(搬移函数) 1.
 * 在该函数最常引用的类中建立一个有着类似行为的新函数,将旧函数变成一个单纯的委托函数,或者将旧函数 完全移除。 2.搬移函数是
 * 重构理论的支柱,或者如果一个类与另一个类有太多合作而形成高度耦合,我就会搬移函数。
 *  7.2 Move Field(搬移字段)
 * 在目标类新建一个字段,修改源字段所有用户,令它们改用新字段。
 * 1.如果我发现,对于一个字段,在其所驻类之外得另一个类中有更多函数使用了它,我就会考虑搬移这个字段。
 *  7.3 Extract Class(提炼类)
 * 建立一个新类,将相关的字段和函数从旧类搬移到新类。 1.你也许听过类似这样得教诲,一个类应该是一个清楚得抽象,处理一些明确得责任。 2.及类 拆分为 2个
 * 类,执行不同得责任。(不同的类,含有不同得字段,函数。)
 *  7.4 Inline Class(将类内联化) 与7.3完全相反 
 *  7.5 HideDelegate(隐藏“委托关系”) 
 *   客户通过一个 委托类 来调用另一个对象。在服务类上建立客户所需得所有函数,用以隐藏委托关系。
 * 如果某个客户先通过服务对象得字段得到另一个对象,然后调用后者得函数,那么客户就必须知晓
 * 这一层委托关系,万一委托关系发生变化,客户也得相应变化。可以在服务对象上放置一个简单得委托函数, 将委托关系隐藏起来,从而去除这种依赖。
 * 7.6 Remove Middle Man (移除中间人)
 *  某个类做了过多得简单委托动作,让客户直接调用受托类。
 *   和 7.5 恰好相反。
 * 7.7 Introduce Foreign Method 引入外加函数
 *  你需要 为提供服务得类增加一个函数,但你无法修改这个类。在客户类中建立一个函数,并以第一参数形式传入一个服务类实例。
 * 7.8 Introduce Local Extension(引入本地扩展) 
 * 你需要为服务类提供一些额外函数,但你无法修改这个类。
 * 建立一个新类,使它包含这些额外函数,让这个扩展品成为源类得子类或包装类。
 *  即将 外加函数 封装到一个类中
 * 
 *  
 *    
 * 
 * 
 * 
 * 
 */
class Test {
	// ********************搬移函数***************************
	int gamma(int code1, int code2, int code3) {
		int value1 = (code1 * code2) + delta();
		int value2 = (code1 + code2) + 100;
		if ((code3 - value1) > 100) {
			value2 = 20;
			int value3 = value2 * 7;
			return value3 - 2 * value1;
		}
		return value2;

	}

	int delta() {
		return 1;
	}

}

// 将 Account 函数 搬移到 Test
class Account {
	// 重构前
	int gamma(int code1, int code2, int code3) {
		int value1 = (code1 * code2) + delta();
		int value2 = (code1 + code2) + 100;
		if ((code3 - value1) > 100) {
			value2 = 20;
			int value3 = value2 * 7;
			return value3 - 2 * value1;
		}
		return value2;

	}

	int delta() {
		return 1;
	}

	// 重构后
	void nnn() {
		new Test().gamma(1, 2, 3);

	}
	// *************************隐藏“委托关系”****************************
}

// 部门类
class Department {
	private String code;
	private Person manager;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Person getManager() {
		return manager;
	}

	public void setManager(Person manager) {
		this.manager = manager;
	}
}

// 人
class Person {
	Department department;

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	// 隐藏委托关系
	public Person getManagerYY() {
		return department.getManager();
	}

	//
	public static void main(String[] args) {
		// 客户 希望知道某人得经理是谁,必须这样调用,但是客户是不需要知道工作原理得。,我们可以隐藏委托关系。
		Person manager = new Person();
		manager.getDepartment().getManager();
		// 重构后
		manager.getManagerYY();
//		***************引入外加函数*****************
		int year = 0;
		int month = 0;
		int date1 = 0;
		Date date = new Date( year,  month,  date1);
//		***************引入外加函数:重构后*****************	
		Date date2=newDay(year, month, date1);
	}

	private static Date newDay(int year, int month, int date) {
		
		return new Date( year,  month,  date);
	}
//	***************引入本地扩展*****************
	class myDdate extends Date {
		public Date newDate(int year, int month, int date) {
			return new Date( year,  month,  date);	
		}
	}
}

猜你喜欢

转载自blog.csdn.net/listening_nq/article/details/83991274