http://www.jianshu.com/p/f8cb51bc8e19
http://blog.csdn.net/lovelion/article/details/17970147
http://www.cnblogs.com/draem0507/p/4942939.html
AS 重构
http://www.jianshu.com/p/f8cb51bc8e19
1.封装集合
public static void main(String[] args) {
Day1Test day1Test = new Day1Test();
//获取到了内部对象
List<String> list = day1Test.getList();
//肆无忌惮的操作
list.add("a");
day1Test.iterator();
//正确的做法
Day1Test2 day1Test2 = new Day1Test2();
//获取到了内部对象
List<String> list2 = day1Test2.getList();
//肆无忌惮的操作
list2.add("a");
day1Test2.iterator();
}
static class Day1Test {
private List<String> list = new ArrayList<String>();
public List getList() {
return list;
}
//模拟不暴露给外部
protected void add(String value) {
list.add(value);
}
protected void remove(String value) {
list.remove(value);
}
public void iterator() {
for (String str : list) {
System.out.println(str);
}
}
}
static class Day1Test2 {
private List<String> list = new ArrayList<String>();
public List getList() {
return new ArrayList(list);
}
//模拟不暴露给外部
protected void add(String value) {
list.add(value);
}
protected void remove(String value) {
list.remove(value);
}
public void iterator() {
for (String str : list) {
System.out.println(str);
}
}
}
2.移动方法
class BankAccount {
public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest) {
AccountAge = accountAge;
CreditScore = creditScore;
AccountInterest = accountInterest;
}
public int AccountAge;
public int CreditScore;
public AccountInterest AccountInterest;
//这个方法跟BankAccount没有直接关系
public double CalculateInterestRate() {
if (CreditScore > 800)
return 0.02;
if (AccountAge > 10)
return 0.03;
return 0.05;
}
}
class AccountInterest {
public BankAccount Account;
public AccountInterest(BankAccount account) {
Account = account;
}
public double InterestRate() {
return Account.CalculateInterestRate();
}
public boolean IntroductoryRate() {
{
return Account.CalculateInterestRate() < 0.05;
}
}
}
class BankAccount1
{
public BankAccount1(int accountAge, int creditScore, AccountInterest1 accountInterest)
{
AccountAge = accountAge;
CreditScore = creditScore;
AccountInterest1 = accountInterest;
}
public int AccountAge ;
public int CreditScore;
public AccountInterest1 AccountInterest1 ;
}
class AccountInterest1
{
public BankAccount1 Account ;
public AccountInterest1(BankAccount1 account)
{
Account = account;
}
public double InterestRate()
{
return CalculateInterestRate();
}
public boolean IntroductoryRate()
{
return CalculateInterestRate() < 0.05;
}
public double CalculateInterestRate()
{
if (Account.CreditScore > 800)
return 0.02;
if (Account.AccountAge > 10)
return 0.03;
return 0.05;
}
}
3.提升方法
简单点说,如果子类都有相同的方法,那就应该将方法提上到父类层
abstract class Vehicle {
// other methods
}
class Car extends Vehicle {
public void Turn(String str) {
// code here
}
}
public class Motorcycle extends Vehicle {
public void Turn(String str) {
// code here
}
}
提升后的结构
abstract class Vehicle1 {
public void Turn(String str) {
// code here
}
}
class Car1 extends Vehicle1 {
}
public class Motorcycle1 extends Vehicle1 {
}
4.下移方法
与第三个上升方法相比,有时候,父类的方法,随着业务的变化,只适合部分子类的时候,则需要将父类的方法下移到具体需要的子类中,这样才符合接口最小原则^^
abstract class Animal {
//狗吠
public void Bark() {
// code to bark
}
}
class Dog extends Animal {
}
class Cat extends Animal {
}
正常小猫是不会狗吠的,当然,有些接口可能当初定义的时候,有些子类还未出现,因此不会有这样的问题。随着业务的增加,这样的问题出现了,那么,我们就要及时的将接口下移
abstract class Animal1 {
}
class Dog1 extends Animal1 {
//狗吠
public void Bark() {
// code to bark
}
}
class Cat1 extends Animal1 {
}
5.提升字段 6.下移字段 同方法
7.重命名(类、方法、参数)
demo就不上,只提一点,命名规则不要担心太长,而选择简写,这样反而为后期的维护带来麻烦。
网友评论