views:

111

answers:

2

I am looking to learn OOP design and development. I would like to know if there are any real world tutorials out there (I googled but didnt find what I was looking for).

For example I am developing an inhome application (something like an HR application)
So I would have a person object, that would be inherited to become an employee, and contractor object. The employee would have a role of a regular employee (who reports to a manager) a Manager, a Director, a Vice President and a President.

I am looking for assistance (c# code samples) in how to create objects that would reflect the above scenario.

+3  A: 
// Gender.cs
public enum Gender
{
     Male,
     Female,
}
// Person.cs
public class Person
{
      public string name;
      public DateTime birthday;
      public Gender gender;
      public bool married;
      public Person spouse;
      public double money;

      public Person(string name, DateTime birthday)
      {
           this.name = name;
           this.birthday = birthday;
      }

      public Person(string name, DateTime birthday, Person spouse) : this(name, birthday)
      {
           this.married = true;
           this.spouse = spouse;
      }
}
// Employee.cs
public class Employee : Person
{
      public Manager manager;

      public Employee(string name, DateTime birthday, Person spouse, Manager manager) : base(name, birthday, spouse)
      {
           this.manager = manager;
      }

      public void Pay(double amount)
      {
           this.money += amount;
      }
}
// Manager.cs
public class Manager : Employee
{
      public Director director;

      public Manager(string name, DateTime birthday, Person spouse, Director director) : base(name, birthday, this, director)
      {
            this.director = director;
      }

      public void Bonus(double amount)
      {
            // a 25% bonus
            Pay(amount + (0.25 * amount);
      }
}
// Director.cs
public class Director : Manager
{
      public VP vp;

      public Director(string name, DateTime birthday, Person spouse, VP president) : base(name, birthday, spouse, this, vp)
      {
           this.vp = vp;
      }

      public string Report()
      {
           // get todays report
           return "Today's report is: Foo";
      }
}
// VP.cs
public class VP : Director
{
    public President president;

    public VP(string name, DateTime birthday, Person spouse, President president) : base(name, birthday, spouse, president)
    {
          this.president = president;
    }

    public void CallThePresident()
    {
          Console.Writeline(president.Call());
    }
}
// President.cs
public class President : VP
{
      public string companyName;

      public President(string name, DateTime birthday, Person spouse, string companyName) : base(name, birthday, spouse, this)
      {
             this.companyName = companyName;
      }

      public string Call()
      {
            return "Hello, this had better be an emergency!";
      }
}

I hope that This is what you are looking for, as I tried to make this a straight forward as possible.

Richard J. Ross III
Richard, I am curious, what does the line "public Manager manager;" mean?
user279521
It is a reference to the employee's manager, so that the manager can keep track of him (another way would be to have the manager have an array or list of empoyees, and keep track of them this way, but for the sake of simplicity, I chose this route).
Richard J. Ross III
You should add a `Salary` property to the `Employee` class and remove the `money` field from the `Person` class. Also consider having a `MaritalStatus` enum instead of the `married` field, as a `Person` can be Divorced, Widow etc.
devnull
Yes, probably in a real program you would, but I just wanted to make it as straightforward as possible.
Richard J. Ross III
@Richard - "real world example" is what the OP needs :)
devnull
Yes, but the way I understood it was that he didn't know much (if anything) about object-oriented programming, and I thought that adding too many classes would confuse him, so I made it simple.
Richard J. Ross III
+1  A: 

The employee would have a role of a regular employee (who reports to a manager)

Oh, relations. This isn't the best OOP practice, but:

public class EmployeeEntity {
  private Relations: EmployeeRelations;
  private Person: Human;

  public GetMyBoss {
   return this.Relations.FirstBoss;
  }
  public GetMyName {
   return this.Person.Name
  }
}
public class Human {
}
public class EmployeeRelations {
}

The bright side of this kind-of-Delegate-pattern is that:

  • a class can be easily read/filled from the database.
  • you can have a Manager who pays to a Manager without getting an OOP headache

The dark side is:

  • no way to access Human from EmployeeRelations instance and vice versa
  • no adequate way to iterate over instances = non-iterable
  • no adequate way to update database.
  • no sense at all, this is a bad pattern.

i.e., Richard's way is better

mhambra