且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在c#中应用单一责任和开放式原则是否正确?

更新时间:2023-11-28 11:42:46

嗨girishmeena,



当然这些原则应该适用于你的C#代码(对于任何其他语言 - 在C#之前使用了哪种语言?)。



但是你的问题似乎针对特定的抽象问题。 />
对不起,如果我真的没有得到你想要的东西。但只是从命名它似乎你错过了一些OOP?名为 CreateSecondRow 的类对我来说似乎不是自然的(这应该是方法或函数的名称)。所以我建议使用一个RowFactory类 - 使它成为一个基类(可能是抽象的),并通过继承和覆盖所需的Create ...方法来创建不同的实现。 (护目镜:工厂模式)

作为替代解决方案,您还可以创建一个接口,让独立的工厂类(它们之间没有继承)实现它(但是使用此解决方案代码重复可能很难避免)。

但是,再次,从您的类和方法命名和您的示例代码中,很难得到您真正的目标,也许如果您能描述您的最终目标,我们可以提出更好的解决方案吗?



这是正确的方法来分离c#中不同类别中的每一个和所有内容吗?

当然不是,正常的OOP原则适用于C#以及我所知道的其他所有OOP语言。属于一起的东西应该一起实施,数据和作用于数据的操作(方法)...



亲切的问候



Johannes
Hi girishmeena,

Of course these prinicple should be applied to your C# code (as for any other language - what language did you use "before" C#?).

But your question seems to target a specific abstraction problem.
Sorry if I don't really get what you want here. But only from naming it seems you miss some OOP? A class with name CreateSecondRow seems not "natural" for me (this should be the name of a method or function). So I would suggest a "RowFactory" class - make it a baseclass (maybe abstract) and create different implementations by inheriting and overwriting of the needed "Create..." methods. (goggle: Factory pattern)
As alternative solution you could also create an Interface and let independend factory classes (no inheritance between them) implement it (but with this solution code duplication may be difficult to avoid).
But again, from your class and methods naming and your example code it's hard to get what is your real target here, maybe if you can describe your ultimate goal, we can come up with a better solution?

Is this correct approach to separate each and everything in different classes in c# ?
Of course not, normal OOP principles apply to C# as to every other OOP language I know. Things that belong together should be implemented together, the data and the operations (methods) that act on the data...

Kind regards

Johannes


以下任何代码.....





Any though on below code .....


// Note : no need to modify existing code, only available for extension.
// right now implemented for questionnaire/ intial bid export.

// Interface for exported sheet start Row and column hard values
interface IRowColumnStart
{
    int startRowPos { get; set; }
    int startColPos { get; set; }
    int endColPos { get; set; }
}

interface unSupportedQ
{
    List<int> unSupportedQ { get; set; }
}

// IRowColumnStart extended class for initial bid export
public class startRowsColumn : IRowColumnStart
{
    public int startRowPos { get; set; }
    public int startColPos { get; set; }
    public int endColPos { get; set; }

    public startRowsColumn(int _startRowPos, int _startColPos, int _endColPos)
    {
        startRowPos = _startRowPos;
        startColPos = _startColPos;
        endColPos = _endColPos;
    }
}

// IRowColumnStart extended class for questionnaire export
public class startRowColumnQ : IRowColumnStart, unSupportedQ
{
    public int startRowPos { get; set; }
    public int startColPos { get; set; }
    public int endColPos { get; set; }
    public List<int> unSupportedQ { get; set; }

    public startRowColumnQ(int _startRowPos, int _startColPos, int _endColPos, List<int> _unSupportedQ)
    {
        startRowPos = _startRowPos;
        startColPos = _startColPos;
        endColPos = _endColPos;
        unSupportedQ = _unSupportedQ;
    }
}










// Note : no need to modify existing code, only available for extension.

// Main Interface for processing export request
interface IProcessExport<t,>
{
    DataRow ProRow(T param);
    void ProWsheet(T1 param);
}

// IProcessExport<t,> extended class for questionnaire export
internal class ProcessExportQ : IProcessExport<rowqparam,>
{
    private IProcessRow<rowqparam> IProcessRow;
    private IProcessWorksheet<worksheetqparam,> IProcessDoc;

    public ProcessExportQ(IProcessRow<rowqparam> i, IProcessWorksheet<worksheetqparam,> j)
    {
        IProcessRow = i;
        IProcessDoc = j;
    }

    public DataRow ProRow(RowQParam qParam)
    {
        return IProcessRow.ProRow(qParam);
    }

    public void ProWsheet(WorksheetQParam qParam)
    {
        IProcessDoc.ProcessWorksheet(qParam);
    }
}

// IProcessExport<t,> extended class for initial bid export
internal class ProcessExportIBid : IProcessExport<rowibidparam,>
{
    private startRowsColumn objRow;
    private IProcessRow<rowibidparam> IProcessRow;
    private IProcessWorksheet<worksheetibidparam,> IProcessDoc;

    public ProcessExportIBid(IProcessRow<rowibidparam> i, IProcessWorksheet<worksheetibidparam,> j)
    {
        IProcessRow = i;
        IProcessDoc = j;
}

    public DataRow ProRow(RowIBidParam iBParam)
{
        return IProcessRow.ProRow(iBParam);
    }

    public void ProWsheet(WorksheetIBidParam iBParam)
    {
        IProcessDoc.ProcessWorksheet(iBParam);
    }
}