由逻辑绑定的类属性的正确OOP实践 [英] Correct OOP Practice for class properties tied by logic

查看:107
本文介绍了由逻辑绑定的类属性的正确OOP实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为documentsection的类定义了一个文档的一部分,然后在HTML中汇编。其中有属性,哪些值是必需的(或有效的)依赖于彼此。例如:

I have a class called 'documentsection' which defines part of a document that later gets assembled in HTML. There are properties within it, and which values are required (or valid) depend on one another. For example:

a)有一个名为'section type'的枚举,它可以是'view'(在这种情况下,另一个属性'content'包含路径/要读取的文件的文件名)。或者可以是文本,其中内容属性文本本身放置在文档中。

a) There is an enumeration called 'section type' which can be 'view' (in this case, another property called 'content' contains the path/filename of a file to read in). Or it can be 'text' where the 'content' property text is itself placed in the document.

b)还有一个名为Action的枚举类型,可以是'append','prepend'或'replacebytag'。在最后一种情况下,另一个属性变得相关,称为tagtoreplace。如果我们添加/添加,这个tagtoreplace属性可以为空。

b) There is another enumeration called 'Action' which can be of type 'append', 'prepend' or 'replacebytag'. In the last case, another property becomes relevant, called 'tagtoreplace'. If we are appending/prepending, this tagtoreplace property can be empty.

表示这种相互依赖性的最佳实践方法是什么?有几种方式我可以想到,其中没有一个是美丽的:

What is the best practice way to represent such interdependencies? There are several ways I can think of, none of which reek with beauty:


  • 当'生成文档'

  • When the method to 'generate the document' is called, go through the properties to ensure they conform to this logic.

将检查放在get / set方法中。一个问题是,当我设置我的部分类型为'视图'我不能设置我的'内容'属性,直到一行或以后 - 所以你不能拒绝设置它的请求在那一点。

Put checks in the get/set methods. One issue with this is that when I set my section type to 'view' I may not set my 'content' property until a line or so afterwards - so you can't reject the request to set it at that point.

使用单独的属性以某种方式分割用途,例如我上面第一个例子中的'content'不应该用于一个文件路径和一堆HTML内容在另一个。

Use separate properties somehow to partition the uses - eg 'content' in my first example above shouldn't be used for a filepath in one case and a bunch of HTML content in another. This doesn't smell right to me, but having separate properties for each seems excessive.

继承子类,每个子类都有不同的附加所需属性集合。因为可以有节类型和动作类型的各种组合,我不能想到一个优雅的方式来烘烤所有这些逻辑到这样的结构。

Inherit subclasses, each with different sets of the additional needed properties. Since there can be various combinations of section type and action type, I can't think of an elegant way to bake all this logic into such a structure. But I'm no OOP guru!

有关最佳方法的任何想法吗?

Any thoughts on the best approach?

谢谢!

推荐答案

我会用以下方法:

interface ISection 
{
    void Render(); // or String Render() if you want to return a string
}

class ViewSection : ISection
{
    public String Filename { get; set; }

    public void Render()
    {
        // do stuff with Filename and/or return the content of the file
    }
}

class TextSection : ISection
{
    public String Text { get; set; }

    public void Render()
    {
        // do stuff with Text and/or return it
    }
}

class DocumentSection
{
    ISection _section;

    public void Render()
    {
        _section.Render();
    }
}

然后,您可以轻松地创建实现 ISection ,提供额外的部分。对于动作也是如此,用 Render 定义 IAction PerformAction 方法。

You can then easily create new classes implementing ISection that supply additional Sections. Same goes for the "actions", define an IAction interface with a kind of Render or PerformAction method which you call.

这篇关于由逻辑绑定的类属性的正确OOP实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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