當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)


問題描述

當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)

I have SaveManager Abstract class and my concrete classes TVSaveManager, DataSaveManager and VoiceSaveManager implementing SaveManager Abstract class.

List<SaveManager> lstPrdSaveManager;

        public SaveResponseModel SaveProducts(SaveProductsRequest objSaveProductsRequest)
        {
            SaveResponseModel saveResponseModel = new SaveResponseModel();

            lstPrdSaveManager = SaveManagerFactory.GetSaveManagers(objSaveProductsRequest, saveResponseModel);

            lstPrdSaveManager.ForEach(saveManager =>
                {
                    saveResponseModel = saveManager.MapAndSaveProduct();
                });


            return saveResponseModel;
        }

Factory class will decide which manager to create and send us the list. I will loop thru the list and invoke the common interface 'MapAndSaveProduct' that every concrete classes will adhere. I guess more or like a strategy pattern.

But the thing is all the concrete savemanage's MapAndSaveProduct method return type is different. TVResponse for TvSaveManager and DataResponse for DataSaveManager and so on. So i created SaveResponseModel class to club all the return types (I am passing SaveResponseModel to factory so that it will get passed to all concrete savemanager class's constructor. Individual class will set the desired property., like TvSaveManager -> saveResponseModel.TvResponse). I get desired result and code looks clean.

Questions are, 1) Is it the correct way to use this pattern when we have different type? 2) If concrete class have different types, should not we use strategy pattern? 3) Should I approach to different design pattern in this case. if yes which one?


參考解法

方法 1:

You've got a combination of Strategy and Visitor in a single group of methods; this is absolutely OK. You could separate them out by giving the responses a common interface, and adding a visitor to it for harvesting the right response. This would apply two patterns in sequence, rather than applying both at the same time.

interface IResponseVisitor {
    void VisitTvResponse(TvResponse r);
    void VisitDataResponse(DataResponse r);
}
interface IResponse {
    void Accept(IResponseVisitor v);
}
class TvResponse : IResponse {
    public void Accept(IResponseVisitor v) {
        v.VisitTvResponse(this);
    }
}
class DataResponse : IResponse {
    public void Accept(IResponseVisitor v) {
        v.VisitDataResponse(this);
    }
}

Now all your MapAndSaveProduct implementations could return the common IResponse. You could collect them all, and then go through them with an implementation of IResponseVisitor, and do what you need for each type inside the corresponding Accept method.

(by Al.Sergey Kalinichenko)

參考文件

  1. Achieve strategy pattern when we have different return type (CC BY-SA 3.0/4.0)

#ooad #OOP #wcf #design-patterns #C#






相關問題

接口實現是否應該獨立 (Should Interface implementations be independent)

當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)

抽象VS信息隱藏VS封裝 (Abstraction VS Information Hiding VS Encapsulation)

應用程序中jQuery表單綁定代碼的最佳實踐 (Best practices with jQuery form binding code in an application)

耦合與內聚 (Coupling and cohesion)

什麼時候應該在C ++中使用類與結構? (When should you use a class vs a struct in C++?)

What is an anti-pattern? (What is an anti-pattern?)

如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)

協調 MVP 三元組 (Coordinating MVP triads)

裝飾器和虛擬方法 (Decorators and Virtual Methods)

設計和建築有什麼區別? (What is the difference between Design and Architecture?)

特定時間段有效的業務規則——如何有序管理 (Business rules that are valid for specific time span – how to manage in an orderly manner)







留言討論