Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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. </p> <p>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.</p> <p>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. </p> <pre><code>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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload