The difference between class duplication and class extension

Duplicate or Extend? What is the difference?

There is a fundamental difference between duplicating a class and extending a class (Blocks, Models, Functions, Types, Enumerations are all types of Class).

Duplicating:

When we duplicate a class we effectively create a separate instance or copy of that class. Any changes to the original class or duplicate class are local changes.

Extending:

When we extend a class we are effectively adding a layer to the class we extend from. This means that all parameter and variable declarations as well as equations and algorithms are inherited (they filter through and are accessible to the upper layers). Modification of the lower layers or levels is prohibited from this class. These lower levels will need to be modified locally. We can however add detail in the extended class and edit the parameter values of the lower levels.

One advantage of extending is that we maintain single instances of the building blocks of our extended class. These building blocks can be reused to create classes with different functionality and detail. If we make any changes or improvements to the classes we have extended from, these will automatically filter through to (or be inherited by) the extended classes. Another advantage of creating a class from layers is that we can keep the complexity of each layer low when building complex classes. This, in turn, facilitates debugging.

For example: if we were to create a rotational damper, we might extend from a building block (known as base class or partial class) which includes a right and a left rotational flange. We could re-use the two flange partial model and extend from it to create the model of a rotational spring damper which also requires the same two rotational flange connectors. Below we can see that both the damper and spring damper are extended from PartialCompliantWithRelativeStates. This partial model will include the two rotational flanges which Damper and SpringDamper are built upon. Any improvements to the PartialCompliantWithRelativeStates will automatically filter through to Damper and SpringDamper.

Damper model text:

model Damper "Linear 1D rotational damper"
  extends  Interfaces.PartialCompliantWithRelativeStates;
  parameter SI.RotationalDampingConstant d(final min=0, start=0)
    "Damping constant";
equation 
  tau = d*w_rel;
end Damper;

Spring Damper model text:

model SpringDamper "Linear 1D rotational damper"
  extends  Interfaces.PartialCompliantWithRelativeStates;
  parameter SI.RotationalSpringConstant c(final min=0, start=1.0e5) 
    "Spring constant";
  parameter SI.RotationalDampingConstant d(final min=0, start=0) 
    "Damping constant";
  parameter SI.Angle phi_rel0=0 "Unstretched spring angle";
equation 
  tau = d*w_rel; 
end Damper;

 

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2017 Claytex Services Ltd All Rights Reserved. | Designed by CMA Marketing

Log in with your credentials

Forgot your details?