Search code examples
c#oopencapsulationdata-hiding

Encapsulation in OOP


I am studying about OOP concepts. As I understood from the documents I have read, I wrote an example program for encapsulation concept in OOP. I pasted my code below. Is my concept about encapsulation is correct ?.

Default.aspx

<asp:Button ID="showBtn" Text="Show employee details." runat="server"/>

Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
Employee emp;

protected void Page_Load(object sender, EventArgs e)
{
    emp = new Employee();
    emp.SetEmployeeID(001);
    emp.SetEmployeeSalary(5000);
    emp.EmployeeName = "Rob";
    emp.EmployeeAge = 26;

    showBtn.Click += new EventHandler(showBtn_Click);
}

void showBtn_Click(object sender, EventArgs e)
{
    emp.ShowEmployeeDetails();
}
}

Class Employee

class Employee
{
private int empId;
private int empSalary;
private string empName;
private int empAge;

public void SetEmployeeID(int id)
{
    empId = id; //Mutator
}

public void SetEmployeeSalary(int sal)
{
    empSalary = sal;  //Mutator
}

public int GetEmployeeID()
{
    return empId;  //Accessor
}

public int GetEmployeeSalary()
{
    return empSalary;  //Accessor
}

public string EmployeeName
{
    get { return empName; }   //Accessor
    set { empName = value; }  //Mutator
}

public int EmployeeAge
{
    get { return empAge; }  //Accessor
    set { empAge = value; } //Mutator
}

private void ShowDetails()
{
    HttpContext.Current.Response.Write(this.GetEmployeeID() + " : " + this.EmployeeName + " : " + this.EmployeeAge + " : " + this.GetEmployeeSalary());
}

public void ShowEmployeeDetails()
{
    ShowDetails();
}
}

My main doubt is about the way I called the ShowDetails() method in the Employee. Is this a good way to hide the method ShowDetails() ?.


Solution

  • From an OO perspective, your ShowDetails method is doing two very different things.

    • Creating a string that represents the object
    • Outputting the string to the HttpResponse.

    Now The first task does belong to the class Employee, you need to know what an employee is to be able to create a string that is representative of the object. In fact in .net this is such a common thing, there is in fact a "overridable" or "virtual" function called Object.ToString().

    The second task has absolutely nothing to do with the class Employee, and A LOT to do with strings and HttpResponses (and in this case how we get the HttpResponse, which is to get it from the HttpContext, which means we MUST be on a webserver in a HttpRequest). With all these assumptions, it is extremely unsafe in an all purpose "data" or "domain" class.

    This is how I would refactor this.

    class Employee
    {
        private int empId;
        private int empSalary;
        private string empName;
        private int empAge;
    
        public void SetEmployeeID(int id)
        {
            empId = id; //Mutator
        }
    
        public void SetEmployeeSalary(int sal)
        {
            empSalary = sal;  //Mutator
        }
    
        public int GetEmployeeID()
        {
            return empId;  //Accessor
        }
    
        public int GetEmployeeSalary()
        {
            return empSalary;  //Accessor
        }
    
        public string EmployeeName
        {
            get { return empName; }   //Accessor
            set { empName = value; }  //Mutator
        }
    
        public int EmployeeAge
        {
            get { return empAge; }  //Accessor
            set { empAge = value; } //Mutator
        }
    
        public override string ToString()
        {
            return this.GetEmployeeID() + " : " + 
                this.EmployeeName + " : " + 
                this.EmployeeAge + " : " + 
                this.GetEmployeeSalary();
        }
    
    
    }
    

    and

    public partial class _Default : System.Web.UI.Page
    {
        Employee emp;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            emp = new Employee();
            emp.SetEmployeeID(001);
            emp.SetEmployeeSalary(5000);
            emp.EmployeeName = "Rob";
            emp.EmployeeAge = 26;
    
            showBtn.Click += new EventHandler(showBtn_Click);
        }
    
        void showBtn_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write(emp.ToString());
        }
    }
    

    Since we KNOW for sure that there is a valid HttpContext.Current inside the webpage. Thus Employees don't need to know about the internet, and would equally be able to work on a WinForm application.