logo
down
shadow

Template design pattern for operations with parameters


Template design pattern for operations with parameters

By : user2952599
Date : November 19 2020, 01:01 AM
Any of those help In a previous project, we used the Template pattern with a context object as follows:
code :
abstract class AbstractInvoiceSweep<T extends SweepContext>
{
    public void doSweep(T context);
    protected void calculateTaxes(T context);
    protected void processLines(T context);
    protected void processMemos(T context);
}

class NormalInvoiceSweep extends AbstractInvoiceSweep<SweepContext>
{
    .... 
}

class PrepadiInvoiceSweep extends AbstractInvoiceSweep<PrepaidSweepContext>
{
    ...
}

class SCRInvoiceSweep extends AbstractInvoiceSweep<SCRSweepContext>
{
    ...
}


Share : facebook icon twitter icon
Logging all operations: the good design pattern?

Logging all operations: the good design pattern?


By : Mohammed Amin Errahm
Date : March 29 2020, 07:55 AM
I hope this helps you . In some cases you can use a proxy class that wraps your objects and allows you to forward function calls using __call. It would look something like this:
code :
class Proxy
{
    private $_target;
    public function __construct($target)
    {
        $this->_target = $target;
    }

    public function __call($name, $params)
    {
        $callable = array($this->_target, $name);
        if (!is_callable($callable)) {
            trigger_error('Call to undefined method '.
                           get_class($this->_target).'::'.$name, E_USER_ERROR);
        }

        return $this->dispatch($callable, $params);
    }

    protected function dispatch($callable, $params)
    {
        return call_user_func_array($callable, $params);
    }
}
class LoggingProxy extends Proxy
{
    protected function dispatch($callable, $params)
    {
        echo "Before calling ".get_class($callable[0]).'::'.$callable[1]."\n";
        $return = parent::dispatch($callable, $params);
        echo "After calling ".get_class($callable[0]).'::'.$callable[1]."\n";
        return $return;
    }
}
$proxy = new LoggingProxy(new Phone);
$proxy->addnewnumber(1, 2);
Does my example for using the Strategy design pattern with the Template Method design pattern make sense?

Does my example for using the Strategy design pattern with the Template Method design pattern make sense?


By : Shivam Rathore
Date : March 29 2020, 07:55 AM
around this issue As Per my Understanding your example does't fit into the Strategy and Template, the Scenario best fits into Abstract Factory and May be Prototype (Depends upon full requirement). The basic difference between strategy and Template is Inheritance v/s Delegation.
If you are Searching for Strategy and template Example i would suggest you to go with some game application where the Full game can fit into template function like end of game, start the game,Winner of the game looser of the game and the Rules to play the can be fitted into Strategy like when to move, what to do with some moves.
Which Design Pattern to use to Encapsulate different operations on same class

Which Design Pattern to use to Encapsulate different operations on same class


By : Mateus Costa
Date : March 29 2020, 07:55 AM
will be helpful for those in need What you need is an Observer pattern. The simplest solution though would be a static variable in A to hold both the last value and the mod value.
code :
class A{
 static int value;
 static int mod;
}
package test;


import java.util.Observable;
import java.util.Observer;

public class Test {

public static void main(String[] args) {
    A obj1=new A();
    A obj2=new A();
    A obj3=new A();

    A superSet=new A();

    obj1.addObserver(superSet);
    obj2.addObserver(superSet);
    obj3.addObserver(superSet);


    obj1.changeValue("change1");
    obj2.changeValue("change1");
    obj3.changeValue("change1");

    System.out.println(superSet.mod);
    System.out.println(superSet.value);
    }
}

class A extends Observable implements Observer{

String value;
int mod;

@Override
public void update(Observable o, Object arg) {
    A updatedValue = (A) arg;
    this.value=updatedValue.value;
    mod++;
}

public void changeValue(String newValue) {
    this.value = newValue;
    setChanged();
    notifyObservers(this);
    }
}
3
change1
How to add different set of parameters to factory method design pattern to achieve proper design

How to add different set of parameters to factory method design pattern to achieve proper design


By : inewcos
Date : March 29 2020, 07:55 AM
seems to work fine Considering that the client will have to know the type of the output to know what parameters to pass in, and that you have no re-usable code between the two types, I see no value from a "generic" factory pattern here. I would either have two factories (if you even need a factory for either type), or let the client call the type constructor directly.
Supported operations design pattern

Supported operations design pattern


By : Narayn
Date : March 29 2020, 07:55 AM
it helps some times Note that I've edited my answer completely dropping my previous approach using a precondition with code contracts.
I've been thinking about a solution that should be more powerful.
code :
public abstract class Calculation
{
    public Calculation()
    {
        // I store all overridable methods in a dictionary where keys
        // are the method names
        OverridableMethods = new Dictionary<string, Func<double[]>>()
        {
            { nameof(CalculateScaled), CalculateScaled },
            { nameof(CalculateNonScaled), CalculateNonScaled },
            { nameof(CalculateConstantLength), CalculateConstantLength }
        };


        // Then we get the first overriden method. If there're more
        // than one, it will pick the first occurence and others will be
        // discarded. It's up to the developer to override just one.
        MethodInfo overridenMethod = OverridableMethods.Keys
                .Select(methodName => GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic))
                .FirstOrDefault(method => method.DeclaringType != typeof(Calculation));

        Contract.Assert(overridenMethod != null, "No calculation operation implementation has been provided");

        // So some method was overriden and I store the method name
        OverridenMethodName = overridenMethod.Name;
    }

    private string OverridenMethodName { get; }
    private Dictionary<string, Func<double[]>> OverridableMethods { get; }

    protected virtual double[] CalculateScaled() 
    {
        throw new NotImplementedException();
    }

    protected virtual double[] CalculateNonScaled() 
    {
        throw new NotImplementedException();
    }

    protected virtual double[] CalculateConstantLength()
    {
        throw new NotImplementedException();
    }

    // Calculate just access the overridable method dictionary
    // to get the overriden method encapsulated by a Func<double[]>
    // and I immediately call it!
    public double[] Calculate() => OverridableMethods[OverridenMethodName]();
}

public class SomeCalculation : Calculation
{
    protected override double[] CalculateScaled()
    {
        return new double[] { 22 };
    }
}
    Calculation calculation = new SomeCalculation();

    // Prints out 22!
    Console.WriteLine(calculation.Calculate()[0]);
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org