设计问题-策略模式对这种情况有帮助吗? [英] design issue - Is strategy pattern helpful for this scenario?

查看:75
本文介绍了设计问题-策略模式对这种情况有帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班A和A. B和两个类都实现接口ISomeInterface.但是,A级和A级都不需要某些属性. B.但是在客户端应用程序中,我正在调用相同的ISomeInterface来调用这两个类.我的问题是我不希望Dictionary< string,> & TypedDataSet,IList< record>.同一界面中的属性.但是客户端需要使用此IsomeInterface.

实际上,DataValues()属性仅适用于类A.类似地,MetaData()和RecordCollection()属性也适用于类B.此外,如果将来我引入一个新的类C,并且需要像这样的单独属性,那么我的代码将看起来很丑,我不想.因此,有什么办法可以让我仍然在客户端应用程序中使用相同的IsomeInterface并在相应的类中具有适当的属性?我认为我需要使用策略设计"模式,但对如何实现相同的模式感到困惑.如果我错了,请纠正我吗?

见下文:

I have two classes A & B and both class implements the interface ISomeInterface. But some properties are not required for both class A & B. But in the client app I am calling the same ISomeInterface to invoke both the classes. The problem which I have is I don''t want Dictionary<string,> & TypedDataSet, IList<record> properties in the same interface. But the clients needs to use this IsomeInterface.

Actually DataValues() property is only applicable to class A. Similarly MetaData() and RecordCollection() properties are applicable to class B. Also, if I introduce a new class C in future and that needs a separate property like this then my code will look ugly which I don''t want. So, Is there any way I can still use the same IsomeInterface in my client app and have the appropriate properties in the corresponding classes? I think I need to use Strategy Design pattern but got confused on how to implement the same. correct me if I am wrong?

See below:

interface ISomeInterface
{
 string Id{get; set;}
 void Display();
 Dictionary<string, string> DataValues{get;};
 TypedDataSet MetaData{get; }
 IList<Record> RecordCollection{get; }
}

public class A: ISomeInterface
{
public string Id
{
        return "A1";
}

void Display()
{
    Console.Writeline("class A");
}

public Dictionary<string, string> DataValues()
{
    return new Dictionary<string, string>();
}


public TypedDataSet MetaData()
{
    //I dont want this method for class A
    throw new NotImplementedException();
}

public IList<Record> RecordCollection()
{
    //I dont want this method for class A
    throw new NotImplementedException();
}
}


public class B: ISomeInterface
{
public string Id
{
        return "B1";
}

void Display()
{
    Console.Writeline("class B");
}

public Dictionary<string, string> DataValues()
{
    //I dont want this method for class B
    throw new NotImplementedException();
}

public TypedDataSet MetaData()
{
    return new TypedDataSet();
}

public IList<Record> RecordCollection()
{
    IList<Record> rc = null;

    //do something
    return rc;
}
 }





Client App -

Main()
{
ISomeInterface a = new A();
a.Display();
Dictionary<string, string> data = a.DataValues();

ISomeInterface b = new B();
b.Display();
TypedDataSet data = b.MetaData();
IList<Record> rc = b.RecordCollection();
}

推荐答案

您正在朝正确的方向前进.
尽管这不是理想的方案\示例,可以将其称为策略模式的实现,但基于对接口ISomeInterface的使用的限制,这可以完成工作而不会弄乱代码.
You are going in the right direction.
Even though it''s not the ideal scenario\example which can be called as an implementation of strategy pattern but based on the limitation on the usage of the interface ISomeInterface, this would do the job without messing up the code.


这篇关于设计问题-策略模式对这种情况有帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆