2

I have a hard time to find an ideal concept / design and would appreciate your thoughts on this.

I have 16 "tissues" that I need to keep track on. Each tissue holds a pressure that need to be calculated depending on the last value.

So I was going through few thoughts how to organise / structure it.

1. Use array of tissues

and and passing the previous value to a function to return the new calculated value.

As an example:

load_tissue = function(value, tissue) {
  return { pressure: tissue.pressure + value };
}

tissue = { pressure: 1 }

newTissue = load_tissue(2,tissue);
// newTissue = { pressure: 3 }

Obviously this is a very simplified function that does nothing except returning an new object with the added value. I would do this for each 16 compartments.

This version works but does not seem to be very intuitive and object oriented.

2. Create a Tissue object

Next version would be to have a Tissue object that holds the data and has a function to calculate. This version sounds very intuitive to me because in real world it is the same.

Tissue = function() {
  this.pressure = 1;
}

Tissue.prototype.load_tissue(value) {
  this.pressure = this.pressure + value;
}

tissue = new Tissue();
newTissue = tissue.load_tissue(2);

// newTissue.pressure = 3;

The problem I have with this is that I can not keep the last state due to referencing.

tissue = new Tissue();
tissue1 = tissue.load_tissue(2); // tissue1 = 3;
tissue2 = tissue.load_tissue(3); // tissue2 = 6 BUT tissue1 = 6 as well.

Although I like that the Tissue object knows it state, in some circumstances I need to be able to "predict" a value without saving it to the object.

Now I am a bit confused what the ideal solution to this is. I was thinking of cloning the object..

The real Tissue() class is a bit more complex and looks like this

Tissue = function(n2_half_time, he_half_time, n2_A, n2_B, he_A, he_B, p_surface) {

   this.startPN2 = 0.7902;
   this.startPHE = 0.0;

   this.half_time = {
       n2: n2_half_time,
       he: he_half_time
   };

   this.k_decay = {
       n2: Math.LN2 / n2_half_time,
       he: Math.LN2 / he_half_time
   };

   this.A = {
       n2: n2_A,                                                                                                                                                          he: he_A
   };

   this.B = {
       n2: n2_B,
       he: he_B
   };

   this.pN2;
   this.pHe;
                                                                                                                                                                      this.init(p_surface);
}

Tissue.prototype.init = function(p_surface) {
   var ambient_pressure = (p_surface == false) ? PRESSURE_AMBIENT_SEA_LEVEL : p_surface;
   this.pN2 = this.startPN2 * (ambient_pressure - PRESSURE_WATER_VAPOUR);
   this.pHe = this.startPHE;
}

Tissue.prototype.load_tissue = function(p_abs, gas, rate, time) {
    var kN2 = this.k_decay.n2;
    var p_alv_n2 = (gas.n2 / 100) * (p_abs - PRESSURE_WATER_VAPOUR);
    var R_n2 = (gas.n2 / 100) * rate;

    var kHe = this.k_decay.he;
    var p_alv_he = (gas.he / 100) * (p_abs - PRESSURE_WATER_VAPOUR);
    var R_he = (gas.he / 100) * rate;

    this.pN2 = this.schreiner_equation(p_alv_n2, R_n2, time, kN2, this.pN2);
    this.pHe = this.schreiner_equation(p_alv_he, R_he, time, kHe, this.pHe);
}
2

Your confusion around

tissue = new Tissue();
tissue1 = tissue.load_tissue(2); // tissue1 = 3;
tissue2 = tissue.load_tissue(3); // tissue2 = 6 BUT tissue1 = 6 as well.

derives from confusion value and object-oriented semantics in your tissue abstraction. This is called 'aliasing'.

To find out which one you should use, ask the following question "If two Tissues have the same pressure, are they always the same thing?". If you say YES, then the very first approach (returning a new object when you change the value) is the correct one. If you say NO (i.e. there can be two different tissues that happen to have the same pressure), then your second approach is correct.

If an object-oriented tissue must know the history of its values for later processing, then the object must maintain that list, like the following:

Tissue = function() {
  this.history = [];
  this.pressure = 1;
}

Tissue.prototype.load_tissue(value) {
  this.history.push(this.pressure);
  this.pressure = this.pressure + value;
}
  • Thanks for the clear explanation. The tissues are never the same as for instance blood or fat but they very much can have the same pressure. Maybe at different timings though. I looked into exactly your solution by having the Tissue to keep track of the history. The difficulty that I had was that I may just want to preview what the new load_tissue value would be. If I save the value to the history I may have to add other values between the last and the second last one depending on the values. Which will affect all the values coming after the changed one. – Chris Jul 2 '15 at 13:05
  • Probably I just need to delete the history entries that are after the new entry or recalculate them. – Chris Jul 2 '15 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.