4 Replies - 389 Views - Last Post: 29 March 2013 - 04:30 PM Rate Topic: -----

#1 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Create a Property to restrict access to a List

Posted 29 March 2013 - 02:31 PM

So, for a project I'm participating in, we're creating a stoichiometry calculator. Right now, I have a couple classes and an interface salient to my question:

  • IParsableSymbols: Any classes implementing this interface must return a List<string> of chemical symbols. So Hydrogen returns a List<string> with one member ("H") and salt returns a List with two members("Na" and "Cl").
  • ElementGroup: Inherits from List<IParsableSymbols> and implements IParsableSymbols itself.
  • ChemicalEquation: Holds two ElementGroups: one called Products and one called Reactants.


So, the code for ChemicalEquation used to look like this:

public ElementGroup reactants = new ElementGroup("reactants");
public ElementGroup products = new ElementGroup("products");

...



And as I was looking it over today I thought, "You know, that's not very good encapsulation. I only want one class - the parser, which builds the ChemicalEquation object out of a user-input text string - to be able to modify it. Once it goes to the "Balancer" class, it shouldn't be changable."

So, I changed it to this:

public ElementGroup Reactants { get; private set; }
public ElementGroup Products { get; private set; }

public ChemicalEquation()
        {
            Reactants = new ElementGroup("reactants");
            Products = new ElementGroup("products");
        }
        


So, I figured now that I have private ElementGroups I'd need to create a method that allowed the Parser object to make changes... only, I actually didn't. For some reason, I'm still able to call the Add() method on my Products and Reactants just like when they were public (ie, myChemicalEquation.Reactants.Add() works). So... what's up with that???

And if you care to directly look at the code, it's here.

Thanks in advance!

This post has been edited by pharylon: 29 March 2013 - 02:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Create a Property to restrict access to a List

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Create a Property to restrict access to a List

Posted 29 March 2013 - 02:49 PM

Stop and think about.

Of course you can still call .Add you are returning a reference to an instance of a list. If you want to return read-only view of a list, return an IEnumerable(Of T) where T is the type. Eg IEnumerable(Of String)

This post has been edited by AdamSpeight2008: 29 March 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Create a Property to restrict access to a List

Posted 29 March 2013 - 02:53 PM

View PostAdamSpeight2008, on 29 March 2013 - 02:49 PM, said:

Stop and think about.

Of course you can still call .Add you are return a reference to an instance of a list. If you want to return read-only view of a list, return an IEnumerable(Of T) where T is the type. Eg IEnumerable(Of String)


Huh. I guess I never really thought about it that way. I just think of {get;} as only allowing the object to be set, but in this case I suppose it is returning the whole freakin' object.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Create a Property to restrict access to a List

Posted 29 March 2013 - 03:26 PM

Encapsulation is about separation of concerns.
An element is not ChemicalEquation or Parser.

For example take a URL it can be represent by the type string, but not every possible string is also a valid representation of URL. So have a type that represents a URL.

Rather than use a String represent Chemicals, define a type (internally you could use string.). I really like using wrappers around the basic types eg integer, double and decimal. So I can capture the semantic meaning of what this value represents. Eg 2124.4 is that meters or barrels of monkeys.

By define a specific type I can exploit the type-checker and get compile error checking, and thus minimize errors caused by use the wrong data.
Example(UnitOfMeasure.Distance.Metres d) 


The above example can only accept Metres.

By thinking a about separation and maybe inheritance.
Hydrogn <: ChemicalElement
Helium  <: ChemicalElement
Lithium <: ChemicalElement
Oxygen  <: ChemicalElement


Using Inheritance you can place most of the code common to all ChemicalsElements, even if you're feeling adventurous operators.
Now you can of specific atoms, or Isotopes.
Eg
Water = (Atom.H * 2) + Atom.O



Is a Chemical Compound or Molecule an Atom? No so define a type that encapsulates those semantic meaning.
Another object that encapsulates a Chemical Equation.
Another object that encapsulate the evaluation of a Chemical Equation.

See how you now create relatively simple type, that are focus on their specific purpose and encapsulation. You really don't want on giant monolithic god object that does everything.

This post has been edited by AdamSpeight2008: 29 March 2013 - 03:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Create a Property to restrict access to a List

Posted 29 March 2013 - 04:30 PM

VB.net Tutorial Example cover separation of concerns (Encapsulation)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1