Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 321001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:48:19+00:00 2026-05-12T08:48:19+00:00

Hej! Caution this is a long post — if you are easily annoyed better

  • 0

Hej!

Caution this is a long post — if you are easily annoyed better skip it. 😉

The Project I am working on currently is about reading voltage measurements of different sensors and calculating the respective physical value. There are a number of channels each of which can have a different sensor attached. Basicly the kind of sensor is known but the details like sensitivity and bias can vary widely. While there is a parametrized class for each type of sensor, the parameters are pulled from config files.

Suppose there is the following class hierachy:

class Sensor
{
public:
  virtual double Calculate(const double &arg)=0;
};
class PTCResistor
{
  PTCResistor(XMLNode &node);
  double Calculate(const double &arg);
};
class Thermocouple
{
  Thermocouple(XMLNode &node);
  double Calculate(const double &arg);
};

The main config file looks like this:

<channels>
  <TurbineInletTemp sensor="Thermocouple.TypeK.xml" />
  <CylinderHeadTemp sensor="PTCResistor.PT500.xml" />
  ...
</channels>

As you see each channel tag has a attribute sensor specifying some ohter XML-File which contians parameter information and the kind of sensor. It looks something like this:

<sensor class="PTCResistor">
  <param Rref="500" Tref="0">
  ...
</sensor>

The format of these reference XML-files can vary but is is understood by the constructor of each derived class.

Upon program start the main config file is parsed and a factory class resolves the link to the XMLs containing the sensor specific information, examines the class tag and calls the respective constructors. Like this:

string chnTITFile = rootNode.GetNode("TurbineInletTemp").GetAttribute("sensor");
XMLNode chnTITNode = XMLNode.parseFile(RootPath + chnTITFile,"sensor")
string className = chnTITNode .GetAttribute("class");

if(className == "Thermocouple") {
  Sensor* sensorTIT = new Thermocouple(chnTITNode);
}
else if(className = "PTCResistor") {
  Sensor* sensorTIT = new PTCResistor(chnTITNode);
}

This is the leanest solution I came up with in the sense that there are only few places to alter when a new derived class is added. Basicly one writes a new derived class and the adds another if-branch in the factory. Unfortunately this involves a lot of string comparisons. Since the number of classes and channels can be rather large (and the target system is low-powered) I am concerned.

Another possibility would be hash the class names. This would probably look like this:

string chnTITFile = rootNode.GetNode("TurbineInletTemp).GetAttribute("sensor");
XMLNode chnTITNode = XMLNode.parseFile(RootPath + chnTITFile,"sensor")
string className = chnTITNode .GetAttribute("class");  

switch(hash(className)) {
case ThermocoupleHash: ...
case PTCResistorHash: ...
...
}

Problem here: need to maintain the enum containing the hash-values.

As I am writing this code I feel that the process of adding a new sensor can be rather tedious. Any ideas how to reduce the mess? Or is this already the least of evil one can possibly get?

Thanks for reading!
Arne

EDIT1

As suggested by Neil Butterworth I consider to extend each class by a function like

  static Sensor* Thermocouple::Create(XMLNode &node) {
     return new Thermocouple(node);     
  }

and create a hash table that relates the class name string to a function pointer to this static function:

typedef Sensor* (*CreateFunct)(XMLNode &node);

class SensorFactory 
{
public:
  SensorFactory() {
    classNameMap["Thermocouple"] = Thermocouple::Create;
    classNameMap["PTCResistor"] = PTCResistor::Create;
  };

  Sensor* ChannelByName(string chnName) {
    string chnFile= rootNode.GetNode(chnName).GetAttribute("sensor");
    XMLNode chnSensorNode = XMLNode.parseFile(RootPath + chnFile,"sensor")
    string className = chnSensorNode.GetAttribute("class");
    map<string, CreateFunct>::iterator iterat = classNameMap.find(className);
    if(iterat != classNameMap.end()) {
       CreateFunct f = iterat->second;
       return f(chnTITNode);
    }
  };

private:
  map<string, CreateFunct> classNameMap;
}

This allows to create the matching sensor object for a channel identified by name. The lookup is accomplished by hash-comparision and one only needs to maintain map initialization in the constructor of the factory class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:48:20+00:00Added an answer on May 12, 2026 at 8:48 am

    This is the Factory pattern – there’s a Wikipedia article here. Thre are a zillion ways of implementing this, but one that I often use is:

    • create an object hierarchy – each class has a static Create
      function, which can create an instance of the class from its
      parameters – the parameters must be common to all Create functions in the hierarchy
    • create a map of class name to Create function for that class
    • every time you add a new class register the class name string
      and the Create function with the map
    • when parsing the XML,get the class name and then use it to look
      up the Create function for that class
    • call the Create function to create an object of the required class

    The Create functions can usefully take the root of the XML entity describing the class as one of its parameters. They can then further parse the XML to
    get values needed for specific object construction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.