What is the design template? Unable to recognize

I have a situation where I must first call my order methods. This has appeared in several places, so I wonder if there is some kind of pattern that I don't see.

Right now, in each such case, I have a stage where I execute some code based on prerequisites , an action stage (where I change my data) and save a scene where I save it in db, I now have this:

accessRightsService.Shift(document, userRole);
updateService.ApplyChanges(document, newData);
documentRepository.Update(document);

I was thinking of something like myService.WrapOperation(doc, d => {})that would first invoke preparation, then execute an action, and then store the results in a database.

So this is a template - and if so, which one?

Not like template method or decorator for me

+4
source share
1 answer

This is very similar to the Builder pattern . Although the linker template indicates that it is used to instantiate the class, this can also be used for method calls.

http://www.blackwasp.co.uk/Builder.aspx

public class Director
{
    public void Construct(Builder builder)
    {
        builder.BuildPart1();
        builder.BuildPart2();
        builder.BuildPart3();
    }
}


public abstract class Builder
{
    public abstract void BuildPart1();
    public abstract void BuildPart2();
    public abstract void BuildPart3();
    public abstract Product GetProduct();
}


public class ConcreteBuilder : Builder
{
    private Product _product = new Product();

    public override void BuildPart1()
    {
        _product.Part1 = "Part 1";
    }

    public override void BuildPart2()
    {
        _product.Part2 = "Part 2";
    }

    public override void BuildPart3()
    {
        _product.Part3 = "Part 3";
    }

    public override Product GetProduct()
    {
        return _product;
    }
}


public class Product
{
    public string Part1 { get; set; }
    public string Part2 { get; set; }
    public string Part3 { get; set; }
}
+1
source

All Articles