态度决定高度、企图决定版图、格局决定结局

导航

Refactoring to Patterns-----Creation Method

理解:

在一个类中,经常出现构造函数重载的情况.如果一个类有10个field,那么它可能就会有很多不同的构造器,这些构造器在一定程度上提供了方便,比如可以有选择的选用你需要的构造器去构造对象。随便造个例子,感觉不太合适:)

 

public class People
{
private string name;
private string sex;
private int age;
private string address;
private double weight;

public People(string _name,string _sex,int _age,string _address,double _weight)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
this.address = _address;
this.weight = _weight;
}

public People(string _name)

{

this.name = _name;

}

public People(string _name,int _age)

{

this.name = _name;

this.age   = _age;

}

.......
}

 

有这样一堆构造器,对于客户程序员来说,这些方法用起来,很难把握用哪一个。也许这还不太坏,最关键的:

public People(string _name,string _sex)和public People(string _name,string address)不能并存。

这样就不太好了吧,我运用最多的就是这两个构造器阿,怎么不可以并存呢?----你也许要发怒了!

基于以上理由,我要refactoring了!

方法:将所有构造器提取出来,作为一个stati方法存在,方法是可以起很容易理解的名字的!

public static People CreatePeopleBySex(string _name,string _sex);

public static People CreatePeopleByAddress(string _name,string _address)

......

最后要注意一点,你一定要提供一个包括全部filed的方法:

public static People CreatePeople(string _name,string _sex,int _age,string _address,double _weight);

基本完工了。

这其中有个跳跃,其实包含全部filed的构造器,应该不被删除。而是改变它的访问级别到private或者protected。

我们看到新的people类:

public class People
{
private string name;
private string sex;
private int age;
private string address;
private double weight;

public People(string _name,string _sex,int _age,string _address,double _weight)
{
this.name = _name;
this.sex = _sex;
this.age = _age;
this.address = _address;
this.weight = _weight;
}

public static People CreatePeople(string _name,string _sex,int _age,string _address,double _weight)
{
return new People(_name, _sex, _age, _address, _weight);
}
public static People CreatePeopleByName(string _name)
{
return new People(_name,"boy",0,"",0.0);
}

public static People CreatePeopelBySex(string _name,string _sex)
{
return new People(_name,_sex,0,"",0.0);
}
public static People CreatePeopleByWeight(string _name,string _sex, double _weight)
{
return new People(_name,_sex,0,"",_weight);
}

public void DoWork()

{

//做工作

}
}

///这时提醒你要思考个问题:这个类的核心工作是啥?

应该是做工作,要做事情啊。但是黑亚亚的static构造方法代替了它的明星地位!

所以,我们需要将这种create行为再次转移!通过一个Factory来封装这么一组创建行为。但是这个Factory又有别于GOF的factory.非常非常的simply的factory.

public class PeopleFactory
{
public static People CreatePeople(string _name,string _sex,int _age,string _address,double _weight)
{
return new People(_name, _sex, _age, _address, _weight);
}
public static People CreatePeopleByName(string _name)
{
return new People(_name,"boy",0,"",0.0);
}

public static People CreatePeopelBySex(string _name,string _sex)
{
return new People(_name,_sex,0,"",0.0);
}
public static People CreatePeopleByWeight(string _name,string _sex, double _weight)
{
return new People(_name,_sex,0,"",_weight);
}
}

这样好了!客户代码这样调用我:

public static void Main()
{
People me = PeopleFactory.CreatePeopleByName("Flyingchen");

me.DoWork();
}

 

//07/01/15 22:58

posted on 2007-01-15 22:59  flyingchen  阅读(298)  评论(1编辑  收藏  举报