chester89
chester89

Reputation: 8617

Which design pattern is it? Can't recognize

I have a situation, where I have to call my methods in particular order. This came up in multiple places, so I wonder if there's some pattern I can't see.

Right now in every such case, I have prepare stage where I execute some code based on preconditions, an act stage (where I modify my data) and save stage where I save it to the db. I now have this:

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

I was thinking about something like myService.WrapOperation(doc, d => {}) that would call prepare first, then execute the action, then save results to the database.

So, is it a pattern - and if it is, which one?

Doesn't look like template method or decorator to me

Upvotes: 4

Views: 133

Answers (1)

James Dev
James Dev

Reputation: 3009

This closely resembles the Builder pattern. Even though the builder pattern states that it is used for class instantiation this can also be applied 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; }
}

Upvotes: 1

Related Questions