如何删除c#类中的重复代码

时间:2013-10-04 12:26:23

标签: c# class refactoring

请帮我重新分解以下代码方案

我有两个班级

class Example1
{
    public string username {private get;set;}
    public string password {private get;set;}
    public  obj[] callfunction(string value) {//code}
}
class Example2
{
    public string UserName {private get;set;}
    public string Password {private get;set;}
    public List<ObjectDefinition> function1(string option) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(option)
        //Other codes
    }
    public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(other)
        //Other codes
    }
    public List<ObjectDefinition> function3(string option) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(option)
        //Other codes
    }
    //so on
}

所以我的问题是哪个是声明这个重复对象声明的最佳方法。

2 个答案:

答案 0 :(得分:2)

添加功能以创建Example1

class Example2
{
    public string UserName {private get;set;}
    public string Password{private get;set;}
    public List<ObjectDefinition> function1(string option) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    public List<ObjectDefinition> function3(string option) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    //so on

    public Example1 CreateExample1(string option)
    {
        Example1 obj=new Example1();
        obj.username =this.UserName ;
        obj.password=this.Password;
        return obj;     
    }
}

答案 1 :(得分:1)

如何定义一个函数,该函数将返回带有所需用户名和密码的Example1实例:

class Example1
{
   public string username {private get;set;}
   public string password{private get;set;}
   public  obj[] callfunction(string value){//code}
}
class Example2
{
   public string UserName {private get;set;}
   public string Password{private get;set;}
   public List<ObjectDefinition> function1(string option) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(option)
    //Other codes
   }
   public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(other)
    //Other codes
   }
   public List<ObjectDefinition> function3(string option) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(option)
    //Other codes
   }

   private Example1 GetExample1Instance()
   {
    return new Example1 { username = this.UserName, password = this.Password }
   }
   //so on
}

或者您可以考虑将Example1的实例作为Example2中的成员(假设所需的功能支持此功能)。