cm007
cm007

Reputation: 1392

What is the difference between factory and pipeline design patterns?

What is the difference between factory and pipeline design patterns?

I am asking because I need making classes, each of which has a method that will transform textual data in a certain way.

I have other classes whose data needs to be transformed. However, the order and selection of the transformations depends on (and only on) which base class from which these classes inherit.

Is this somehow related pipeline and/or a factory pattern?

Upvotes: 2

Views: 3367

Answers (2)

user338195
user338195

Reputation:

Factory is responsible for creating objects:

ICar volvo = CarFactory.BuildVolvo();
ICar bmw = CarFactory.BuildBMW();

IBook pdfBook = BookFactory.CreatePDFBook();
IBook htmlBook = BookFactory.CreateHTMLBook();

Pipeline will help you to separate processing into smaller tasks:

var searchQuery = new SearchQuery();

searchQuery.FilterByCategories(categoryCriteria);
searchQuery.FilterByDate(dateCriteria);
searchQuery.FilterByAuthor(authorCriteria);

There is also a linear pipeline and non-linear pipeline. Linear pipeline would require us to filter by category, then by date and then by author. Non-linear pipeline would allow us to run these simultaneously or in any order.

This article explains it quite well:

http://www.cise.ufl.edu/research/ParallelPatterns/PatternLanguage/AlgorithmStructure/Pipeline.htm

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Factory creates objects without exposing the instantiation logic to the client and refers to the newly created object through a common interface. So, goal is to make client completely unaware of what concrete type of product it uses and how that instance created.

public interface IFactory // used by clients
{
   IProduct CreateProduct();
}

public class FooFactory : IFactory
{
    public IProduct CreateProduct()
    {
        // create new instance of FooProduct
        // setup something
        // setup something else
        // return it
    }
}

All creation details are encapsulated. You can create instance via new() call. Or you can clone some existing sample FooProduct. You can skip setup. Or you can read some data from database before. Anything.

Here we go to Pipeline. Pipeline purpose is to divide a larger processing task into a sequence of smaller, independent processing steps (Filters). If creation of your objects is a large task AND setup steps are independent, you can use pipeline for setup inside factory. But instantiation step definitely not independent in this case. It mast occur prior to other steps.

So, you can provide Filters (i.e. Pipeline) to setup your product:

public class BarFilter : IFilter
{
    private IFilter _next;

    public IProduct Setup(IProduct product)
    {
        // do Bar setup

        if (_next == null)
            return product;

        return _next.Setup(product);
    }
}

public abstract class ProductFactory : IProductFactory
{
    protected IFilter _filter;

    public IProduct CreateProduct()
    {
        IProduct product = InstantiateProduct();
        if (_filter == null)
            return product;

        return _filter.Setup(product);
    }

    protected abstract IProduct InstantiateProduct();
}

And in concrete factories you can setup custom set of filters for your setup pipeline.

Upvotes: 3

Related Questions