c# - OOP: composition samples please suggest which is better and why -


Please disregard the language syntax, I just want to discuss here Oh.


I will present here two code snippets, each of which is a sample of a structure (if I am not mistaken).

Problem Statement : I have an object that holds an entity's stats in the system. Give those statistics:

  1. Name
  2. Last name
  3. Pay

* Whatever the fields are Can I just take these for example? So please do not think in terms of whether it is necessary for the field or not. Suppose all three are necessary.

I created a square that matches these areas:

  public class stats {name of field; Region LatsName; Field salary; }  

Now I came in a situation where I should get information about a person. It is necessary to look at a point in the workflow system to present information about a person for all three steps.

  1. When she was a child.
  2. When he was young
  3. When he retired.

Note here that name and lastname will not change, only because salary can change, because I thought I could probably create a class that can use the current object 'statistics'.

I am going to present two solutions, Please suggest which is better and why .

Code sample 1

  public class CompositeStats {// adding three properties and hinding a private objStats already present = new data ( ); Public area first name {{return objStats.Name;} set {objStats.Name = value;}} Public field last name {{return returned objStats.LastName;} Set {objStats.LastName = value;}} Public area SalaryChild { Get; Set;} Public Area SalaryYoung {Get; Set;} Public Area SalaryRetired {Get; Set;}}  

In the above sample code, I did not disclose the original area of ​​salary, but has 3 newly created for the period

< Strong> Code sample 2

  public class CompositeStats {Private objStatsChild = new stats () .; Private objStatsYoung = new figures (); Private objStatsRetired = new figures (); Public area first name {{return objStatsChild.Name;} set {objStatsChild.Name = value;}} Public field last name {{return returned objStatsChild.LastName;} Set {objStatsChild.LastName = value;}} Public area SalaryChild { {Get back objStatsChild.Salary;} set {objStatsChild.Salary = value;}} Public area SalaryYoung {{get back objStatsYoung.LastName;} set {objStatsYoung.LastName = value;}} Public Area SalaryRetired {{Mill Returned objStatsRetired.LastName ;} Set {objStatsRetired.LastName = value}}}}  

I think that Something like that will happen It shall be further carried around you can build steps to new life without changing your object model.

In addition, if a person has not yet reached a platform, then no information will be stored for him. Can choose the latest step in a property list named "Current Stage".

  public square stage {object bugrestage; // Probably a date time or similar class. Object endage; // Probably a date time or similar class. Commodity pay; } Public class statistics {item name; Item last name; IEnumerable & LT; Stage & gt; Steps; }  

You can also use this variation.

  Public Enum Stage {Child, Youth, Retired, Public Class StageShallery {Stage Stage; Commodity pay; } Public class statistics {item name; Item last name; & Lt; StageInfo & gt; Steps; }  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -