jeudi 29 août 2019

What's the benefit of using a template method in the wikipedia example for Factory Method pattern?

The Wikipedia article about factory method pattern

contains this example:

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct();

    public IProduct GetObject() // Implementation of Factory Method.
    {
        return this.MakeProduct();
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct()
    {
        IProduct product = new Phone();
        //Do something with the object after you get the object. 
        product.SetPrice(20.30);
        return product;
    }
}

where the template method pattern is used too (GetObject calls abstract MakeProduct). In this (specific) case, I wouldn't to it that way, because the template method doesn't contain any "surrounding" code and making GetObject abstract with overrides in derived classes would be sufficient.

Do I oversee something here? Or I'm right with the assumption, that this example is not as simple as possible as it could be for the demonstration?

Aucun commentaire:

Enregistrer un commentaire