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 4617086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:04:31+00:00 2026-05-22T02:04:31+00:00

I am attempting to apply the Strategy pattern to a particular situation, but am

  • 0

I am attempting to apply the Strategy pattern to a particular situation, but am having an issue with how to avoid coupling each concrete strategy to the context object providing data for it. The following is a simplified case of a pattern that occurs a few different ways, but should be handled in a similar way.

We have an object Acquisition that provides data relevant to a specific frame of time – basically a bunch of external data collected using different pieces of hardware. It’s already too large because of the amount of data it contains, so I don’t want to give it any further responsibility. We now need to take some of this data, and based on some configuration send a corresponding voltage to a piece of hardware.

So, imagine the following (much simplified) classes:

class Acquisition
{
    public Int32 IntegrationTime { get; set; }
    public Double Battery { get; set; }
    public Double Signal { get; set; }
}

interface IAnalogOutputter
{
    double getVoltage(Acquisition acq);
}

class BatteryAnalogOutputter : IAnalogOutputter
{
    double getVoltage(Acquisition acq)
    {
        return acq.Battery;
    }
}

Now, every concrete strategy class has to be coupled to my Acquisition class, which is also one of the most likely classes to be modified since it’s core to our application. This is still an improvement over the old design, which was a giant switch statement inside the Acquisition class. Each type of data may have a different conversion method (while Battery is a simple pass-through, others are not at all that simple), so I feel Strategy pattern or similar should be the way to go.

I will also note that in the final implementation, IAnalogOutputter would be an abstract class instead of an interface. These classes will be in a list that is configurable by the user and serialized to an XML file. The list must be editable at runtime and remembered, so Serializable must be part of our final solution. In case it makes a difference.

How can I ensure each implementation class gets the data it needs to work, without tying it to one of my most important classes? Or am I approaching this sort of problem in the completely wrong manner?

  • 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-22T02:04:31+00:00Added an answer on May 22, 2026 at 2:04 am

    Ok, I hate to not give someone else the credit here, but I found a hybrid solution that works very well for my purposes. It serializes perfectly, and greatly simplifies the addition of new output types. The key was a single interface, IOutputValueProvider. Also note how easily this pattern handles the retrieval of varying ways of storing the data (such as a Dictionary instead of a parameter).

    interface IOutputValueProvider
    {
        Double GetBattery();
        Double GetSignal();
        Int32 GetIntegrationTime();
        Double GetDictionaryValue(String key);
    }
    
    interface IAnalogOutputter
    {
        double getVoltage(IOutputValueProvider provider);
    }
    
    class BatteryAnalogOutputter : IAnalogOutputter
    {
        double getVoltage(IOutputValueProvider provider)
        {
            return provider.GetBattery();
        }
    }
    
    class DictionaryValueOutputter : IAnalogOutputter
    {
        public String DictionaryKey { get; set; }
        public double getVoltage(IOutputValueProvider provider)
        {
            return provider.GetDictionaryValue(DictionaryKey);
        }
    }
    

    So then, I just need to ensure Acquisition implements the interface:

    class Acquisition : IOutputValueProvider
    {
        public Int32 IntegrationTime { get; set; }
        public Double Battery { get; set; }
        public Double Signal { get; set; }
        public Dictionary<String, Double> DictionaryValues;
    
        public double GetBattery() { return Battery;}
        public double GetSignal() { return Signal; }
        public int GetIntegrationTime() { return IntegrationTime; }
        public double GetDictionaryValue(String key) 
        {
            Double d = 0.0;
            return DictionaryValues.TryGetValue(key, out d) ? d : 0.0;
        }
    }
    

    This isn’t perfect, since now there’s a gigantic interface that must be maintained and some duplicate code in Acquisition, but there’s a heck of a lot less risk of something being changed affecting the other parts of my application. It also allows me to start subclassing Acquisition without having to change some of these external pieces. I hope this will help some others in similar situations.

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

Sidebar

Related Questions

I'm attempting to apply an XSL transform to an XML file inside an SSIS
I'm attempting to apply custom rights to a folder as part of a WiX
I'm attempting to apply a stylesheet to an XML document using Saxon. Given an
I am attempting to apply an XSLT transformation to a batch of XML documents.
I have a model that I am attempting to apply optimistic locking to. If
I have an image in OpenGL that I am attempting to apply a simple
I am attempting to take an image captured from the rear camera, apply a
I'm attempting to get a CSS style to apply to the top list item
I'm attempting to add this .click function to each image I'm sorting with the
Attempting to insert an escape character into a table results in a warning. For

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.