2

Currently, I am dealing with multiple layers of composition in my application. We read data from database for ProductLocations and put them into a Solver object. In another query we read multiple SalesActivities connected to product locations which needs to be placed in an object deep within the correct ProductLocation object. I end up with chained AddSalesActivity methods as in the following code.

Over the years I have seen this kind of a structure in many of the applications I worked. Sometimes even with longer chains. I was thinking about it today and this smells like a subtle repetition. Any ideas on a different way of handling this? Is there a way to come up with a better design?

class Solver{
    List<ProductLocation> productLocations;
    public void ImportSalesActivities{
        //foreach sales activity read find the correct productLocation
        //Call productLocation.AddSalesActivity)
    }
}

class ProductLocation{
    Forecaster forecaster;
    public void AddSalesActivity(SalesActivity activity){
        forecaster.AddSalesActivity(activity);
    }
}

class Forecaster{
    SalesActivityResolver resolver;
    public void AddSalesActivity(SalesActivity activity){
        resolver.AddSalesActivity(activity);
    }
}

class SalesActivityResolver{
    List<SalesActivity> resolvedActivities;
    public AddSalesActivity(activity){
        //update resolved activities based on complicated criteria.
    }
}
4

1 回答 1

1

以您在此处描述的方式,可以只删除类ProductLocationForecaster,但我猜它们会执行此处未显示的其他事情,因此无法删除。如果没有更好地理解这些对象和类的语义,我将无法再为您提供帮助。我的一般建议是,您应该尝试为简单的场景(不要太多对象)创建应用程序的UML-Object-Diagram 。这可以帮助您发现模式并更好地理解对象的角色。

于 2010-04-13T18:29:16.877 回答