lundi 15 mai 2017

'Visitor D.Pattern vs. dynamic keyword' performance

What is the better way for resolving parameter type of a method i.e. double dispatching
(using > .NET 4.0 )?

class Customer {
    void Process(IProduct product) { ... } 
    void Process(ProductA product) { ... } 
}

Example with dynamic:

interface IProduct { 

}

class Product..A..B..etc. : IProduct { 

}

//------------------------------------

var customer = new Customer();
IProduct product = new ProductA();

customer.Process((dynamic)product);

Visitor:

interface IProduct {
    void Accept(Customer customer)
}

class Product..A..B..etc. : IProduct {
    void Accept(Customer customer) {
        customer.Process(this);
    }
}

//------------------------------------

var customer = new Customer();
IProduct product = new ProductA();

product.Accept(customer);

Do we still need the visitor pattern when we can achieve the same thing with 'dynamic' ?

Aucun commentaire:

Enregistrer un commentaire