سؤال

In my application, I need to load data from a DB in a certain sequence of steps, example load all customers, then load all orders and load products etc. However, in one or two cases, the order is different and also need to load additional data.

abstract Model
{
   public void load(Configuration config) {
      loadCustomers(config);
      loadOrders(config);
      loadProducts(config);
   }
}

ConcereteModel1 extends Model {
   doesn't overload load
}

ConcreteModel2 extends Model {
   public void load(Configuration config) {
      loadProducts(config);
      loadOrders(config);
      loadCustomers(config);
      loadAdditional(config);
   }
}

My initial thought is if I could maybe use Command pattern (with a load command method) for the actual loading of customers, orders or products and use chain of responsibility to tie up the commands. Is this a good approach or am I over engineering things here?

Any sample code would be very helpful. Thanks for any suggestions

هل كانت مفيدة؟

المحلول

It sounds like the right choice for your task. Alternatively you need to make a base class for your data load steps, and implement it differently for the special cases.

It all depends on what the easiest way to determine that you have a special case is.

See this description of Chain of Responbsibility for code sample.

نصائح أخرى

I think you have to pay attention:

you are using a technical way (e.g. the Command pattern) to hide a domain specification ("... However, in one or two cases...").

I suggest to develop a solution that arise the second one.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top