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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:44:55+00:00 2026-06-04T15:44:55+00:00

I have two algorithms that I’m implementing: AlgorithmA which works with Vector values and

  • 0

I have two algorithms that I’m implementing:

  • AlgorithmA which works with Vector values and
  • AlgorithmB which works with Matrix values

What the algorithms have in common:

  • Both are “summation” algorithms that are supplied with the same
    sequence of inputs. Algorithms vary slightly on whether to account
    for a particular value or not. They also vary in the computation to
    be performed per value of the sequence.
  • Both algorithms are referenced by the same object (e.g. an ‘Antenna‘ that uses an algorithm for transmission or receiving).
  • In both cases, I want to be able to serialize the Vector or Matrix result. In addition, I should be able to initialize any of the algorithms with (deserialized) vector/matrix values computed from an earlier generaion.

I first tried implementing the above using the Strategy pattern, but soon I realized that the strategy pattern might not be the best due to the varying types/values. And to further complicate things, my ‘Antenna‘ object could use any of the algorithms in either direction:

class Antenna
{
    private AlgorithmParams _algorithm;
}

class AlgorithmParams
{
     private IAlgorithm _transmit;
     private IAlgorithm _receive;
}   

which I feel that it duplicates the notion of “transmit” and “receive” multiple times (because AlgorithmA, which implements IAlgorithm, itself has derived types ‘AlgorithmATransmit‘ and ‘AlgorithmAReceive‘ i.e. slight variations within the same algorithm depending on direction).

I would also like to have a cleaner separation between the algorithm logic and the serialzed data.

I would be happy to hear your opinions on this. Thanks !

  • 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-06-04T15:44:57+00:00Added an answer on June 4, 2026 at 3:44 pm

    The Strategy pattern to me is nothing more than just using object composition to allow a bunch of different strategies can be used in a class AND interchanged in run-time. In your case, you CAN use the Strategy pattern, if you want the Antenna class to change its behavior (the algorithm) in run-time, based on the input values. If this is the case, in the Antenna class, you have an instance variable pointing to a AlgorithmInterface, which is derived by 4 classes: AlgoATransmit, AlgoBTransmit, AlgoAReceive, and AlgoBReceive. Each of these 4 classes would define the real algorithms. Then, you need a client class that checks input value types, and set the Antenna to use the appropriate algorithm.

    While I don’t see how Command Pattern can be applied, your problem might be also be a good case for the Template Method pattern and you can use it in complement with the Strategy. What you can do is to have an abstract class, let’s call it AbstractAlgorithm, which has a “template method” that define the common flow of the algorithms by calling separate functions. These functions will be overriden in the subclasses, e.g. AlgorithmA, AlgorithmB.

    The direction of Antenna can be solved by using “hooks” inside the template method. Hooks are basically functions that are optional to override in subclasses.

    Here is a simple sample code. Antenna makes use of Object Composition and Strategy, has a variable pointing to the abstract algorithm, which is derived by 2 algorithms. This way, the outline of the algorithms can be specified in one place, and each concrete step is defined in the subclasses. Hope it helps and I hope I understood your problem right.

    class Antenna {
        private AbstractAlgorithm algo;
        void SetAlgo(AbstractAlgorithm newAlgo) {
            algo = newAlgo;
        }
    }
    
    class AbstractAlgorithm {
    
        //this is the template method
        void runAlgo() {   
              step1();        //step1 and step2 are two common steps for both algorithms
              step2();
              if (isMatrixValue())
                 step3();            //a hook, only AlgoB will override it.
              if (isTransmitting())
                 step4();            //a hook, use for transmit
              else if (isReceiving())
                 step5();            //a hook, use for receive
    
        }
    
        abstract void step1();
        abstract void step2();
        boolean isMatrixValue() {
             return false;         //default value, to not run step3
        }
    
    }
    
    class AlgorithmA {
    
        void step1() {
             //some implementation
        }
        void step2() {
             //some implementation
        }
    
        //use the default false value for isMatrixValue(), not to run step3
    
    
    }
    
    
    class AlgorithmB {
    
        void step1() {
             //some implementation
        }
        void step2() {
             //some implementation
        }
        void step3() {
             //some implementation
        }
        boolean isMatrixValue() {
             return true;         //return true and override step3
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two matrices x and y, both are results from different algorithms/routines that
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have two algorithms, that detects AR markers (ARToolKit and Infi). I have them
I have a two part question Best-Practice I have an algorithm that performs some
I have two methods that draw a rotated rectangle on the screen. RenderMethod1 renders
I have two classes that extend the activity class. Each class has it's own
I have two algorithms with time complexities of O(n log n) and O(n log3
Do the two algorithms have the same theta characterization of Θ(n^2)? int sum =
I have what I think is an interesting question. I have two arrays which
I have two algorithms of finding primes, in Python. The inner loop of each

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.