我有一点精神障碍试图找出这个程序的最佳架构并需要一些帮助。
我正在编写一个C#应用程序来创建我们称之为" Views"。视图基本上是一个对象,它接受一些数据作为输入,并将另一个对象作为输出吐出。
以下是我设想的代码:
ViewResponse response;
ViewRequest request;
request = new CustomerViewRequest();
request.Translations = GetTranslations(); // generic to Request
request.CustomerViewAsOfDate = new DateTime(2014,1,1); // specific to CustomerViewRequest
response = ViewCreator.CreateView(request);
Console.WriteLine(response.ViewCreatedSuccessfully); // generic to Response
Console.WriteLine((CustomerViewResponse)response.SomeCustomerViewSpecificProperty); // specific to CustomerViewResponse
request = new BKLedgerViewRequest();
request.Translations = GetTranslations(); // generic to Request
request.EAAnalysisData = GetEAAnalysisData(); // specific to BKLedgerViewRequest
response = ViewCreator.CreateView(bkRequest);
Console.WriteLine(response.ViewCreatedSuccessfully); // generic to Response
Console.WriteLine((BKLedgerViewResponse)response.SomeBKLedgerViewSpecificProperty); // specific to BKLedgerViewResponse
答案 0 :(得分:3)
您永远不需要帮助决定设计模式。
原因是,设计模式并不意味着是一个千篇一律,或者一种模式适合所有人。应该实施一种模式来解决特定问题。它不应该用作原始架构,而是用来帮助解决特定问题。
一旦引入模式,它将引入自己的复杂性和局限性。这可能实际上阻碍了你的目标。
真正的问题应该是,如何重构此应用程序以优化性能,捕获应用程序目标并保持可读性/可维护性?
根据提供的信息,我们甚至无法推荐一种模式,因为我们无法了解您的应用目标,问题和限制。这就是为什么在您以业务,用户和开发人员的方式理解应用程序时,通常由创建者来找到最佳解决方案。
答案 1 :(得分:0)
当我查看你的代码时,似乎完成你想要的只是理解面向对象的编码,继承,协方差和逆变。我对下面的代码进行了重新编译,编译并基于使用ViewRequest和ViewResponse的基类来完成您正在寻找的内容。
当您实例化对象时,使用更具体的CustomerViewRequest类型(例如)会为您提供一个对象,您可以将其视为ViewRequest或CustomerViewRequest,具体取决于您的需求。
class Class6
{
public object GetTranslations() { return null; }
public object GetEAAnalysisData() { return null; }
public void DoStuff()
{
CustomerViewRequest cvRequest = new CustomerViewRequest();
cvRequest.Translations = GetTranslations(); // generic to Request
cvRequest.CustomerViewAsOfDate = new DateTime(2014, 1, 1); // specific to CustomerViewRequest
CustomerViewResponse cvResponse = ViewCreator.CreateViewResponse<CustomerViewResponse>(cvRequest);
Console.WriteLine(cvResponse.ViewResponseCreatedSuccessfully); // generic to Response
Console.WriteLine(cvResponse.SomeCustomerViewSpecificProperty); // specific to CustomerViewResponse
BKLedgerViewRequest bkRequest = new BKLedgerViewRequest();
bkRequest.Translations = GetTranslations(); // generic to Request
bkRequest.EAAnalysisData = GetEAAnalysisData(); // specific to BKLedgerViewRequest
BKLedgerViewResponse bkResponse = ViewCreator.CreateViewResponse<BKLedgerViewResponse>(bkRequest);
Console.WriteLine(bkResponse.ViewResponseCreatedSuccessfully); // generic to Response
Console.WriteLine(bkResponse.SomeBKLedgerViewSpecificProperty); // specific to BKLedgerViewResponse
}
}
class ViewRequest
{
public object Translations { get; set; }
}
class ViewResponse
{
public bool ViewResponseCreatedSuccessfully { get; set; }
}
class CustomerViewRequest : ViewRequest
{
public DateTime CustomerViewAsOfDate { get; set; }
}
class CustomerViewResponse : ViewResponse
{
public string SomeCustomerViewSpecificProperty { get; set; }
}
static class ViewCreator
{
public static T CreateViewResponse<T>(ViewRequest request)
where T : ViewResponse, new()
{
return new T();
}
}
class BKLedgerViewResponse : ViewResponse
{
public int SomeBKLedgerViewSpecificProperty { get; set; }
}
class BKLedgerViewRequest : ViewRequest
{
public object EAAnalysisData { get; set; }
}