面向对象思想的头脑风暴(三)-使用接口进行解耦

接口有一个非常重要的功能就是解除耦合,我们这里使用一个简单的代码发展过程来帮大家理解接口的这个功能。

首先我们先写一段简单的代码

 

代码
 class Employee
    {
        
public string Name { getset; }
        
public int Sex { getset; }
    }
static void Main(string[] args)
        {
            Employee employee 
= new Employee() { Name = "lfm", Sex = 1 };
            Console.WriteLine(
"姓名:{0},性别:{1}",employee.Name,employee.Sex);
        }

 

 

结果为:姓名:lfm,性别:1

我们认为Main函数是界面端,而Employee类是业务逻辑端,简单的代码这样写看起来问题不大,基本可以符合功能要求。

但是假如界面端程序员觉得性别不应该是0,1整数类型,而应该是一个枚举,因为枚举可以更直观,而且有智能感知帮助录入,显然界面端程序员的要求是合理的,但问题是如果业务层是用Linq to sql生成的,而且数据库中就是使用整型存储的,同时底层程序员也不想对数据库进行修改,这时我们可以使用如下方式解决:

 

代码
 enum SexEnum
    {
        男,
        女
    }
    
interface IEmployee
    {
        
string Name { getset; }
        SexEnum Sex { 
getset; }
    }

    
partial class Employee
    {
        
public string Name { getset; }
        
public int Sex { getset; }
    }
    
partial class Employee : IEmployee
    {

        SexEnum IEmployee.Sex
        {
            
get
            {
                
                
return (SexEnum)this.Sex;
            }
            
set
            {
                
this.Sex=(int)value;
            }
        }
    }
//客户端程序
 class Program
    {
        
static void Main(string[] args)
        {
            IEmployee employee 
= Factory("lfm",SexEnum.男);
            Console.WriteLine(
"姓名:{0},性别:{1}",employee.Name,employee.Sex);
        }
        
static IEmployee Factory(string name, SexEnum sex)
        {
            
return new Employee() { Name = name, Sex = (int)sex };
        }
    }

 类结构图如下:

这样就使用接口成功的将界面层和业务层分隔开来做到了业务层和界面层的解耦。同时这样还给我们带来了一个额外的好处,那就是当业务层修改的时候不会影响界面层代码,比方说当数据库中的name改成了EmployeeName,界面端程序员觉得这个更改不合理,所以界面端不想修改,我们可以做如下修改:

 

代码
partial class Employee : IEmployee
    {

        SexEnum IEmployee.Sex
        {
            
get
            {
                
                
return (SexEnum)this.Sex;
            }
            
set
            {
                
this.Sex=(int)value;
            }
        }
        
public string Name
        {
            
get
            {
                
return this.EmployeeName;
            }
            
set
            {
                
this.EmployeeName=value;
            }
        }
    }
partial class Employee
    {
        
public string EmployeeName { getset; }
        
public int Sex { getset; }
    }

 

 

而前端不用做任何修改。

需求进一步修改:员工表需要存储在数据库中分成两张表,Employee1,Employee2,名字字段在Employee1中,性别字段在Employee2中,修改代码如下:

 

代码
 class Employee1
    {
        
public string Name { getset; }
    }
    
class Employee2
    {
        
public int Sex { getset; }
    }
    
enum SexEnum
    {
        男,
        女
    }
    
interface IEmployee
    {
        
string Name { getset; }
        SexEnum Sex { 
getset; }
    }
    
class Employee : IEmployee
    {
        Employee1 emp1 
= new Employee1();
        Employee2 emp2 
= new Employee2();
        
public string Name
        {
            
get
            {
                
return emp1.Name;
            }
            
set
            {
                emp1.Name
=value;
            }
        }

        
public SexEnum Sex
        {
            
get
            {
                
return (SexEnum)emp2.Sex;
            }
            
set
            {
                emp2.Sex
=(int)value;
            }
        }
    }
    
class Program
    {
        
static void Main(string[] args)
        {
            IEmployee employee 
= Factory("lfm",SexEnum.男);
            Console.WriteLine(
"姓名:{0},性别:{1}",employee.Name,employee.Sex);
        }
        
static IEmployee Factory(string name, SexEnum sex)
        {
            
return new Employee() { Name = name, Sex = sex };
        }
    }

 

 类结构图如下:

 

除了工厂稍微做一点修改外,前端不用更改任何代码。

其实这里的例子基本算是适配器模式的一种应用。

 

 

posted @ 2010-05-12 17:27  你听海是不是在笑  阅读(4372)  评论(6编辑  收藏  举报