Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it design pattern ? chain

How call that class\structure ? Maybe is it design pattern ?

Task:
I have a collection which I need search item.
I have 3 levels for searching.
I create a Matcher chain and search in collection on each levels and pass the collection to next level if doesn't find.

I'm looking for what is design pattern.

class Matcher
{
    private readonly Matcher _nextMatcher;
    private readonly Func<lambda, bool> _predicate;

    public Matcher(
        Matcher nextMatcher,
        Func<lambda, bool> predicate)
    {
        _nextMatcher = nextMatcher;
        _predicate = predicate;
    }

    public bool Match(Collection<SomeObject> someObjects,
        IEnumerable<SomeObject> sourceObjects)
    {
        if (!_predicate(someObjects, sourceObjects))
        {
            if (_nextMatcher == null)
                return false;

            return _nextMatcher.Match(someObjects, sourceObjects);
        }

        return true;
    }
}
like image 531
Mediator Avatar asked Jul 06 '26 00:07

Mediator


1 Answers

From the looks of it, this looks like (a correct) implementation of the Chain of Responsibility design pattern.

Here's another, more .NET-centric example: http://www.dofactory.com/Patterns/PatternChain.aspx

like image 149
Igal Tabachnik Avatar answered Jul 08 '26 14:07

Igal Tabachnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!