帮助我在两种设计之间进行选择

发布于 2024-09-02 21:09:30 字数 2612 浏览 4 评论 0原文

// 愚蠢的标题,但我想不出有什么更聪明的东西,

我有一个代码(见下文,抱歉代码很长,但是它非常非常简单):

namespace Option1
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;
        public AuxClass1 AuxClass
        {
            get
            {
                return _auxClass;
            }
            set
            {
                _auxClass = value;
            }
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

namespace Option2
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;

        public string Field1
        {
            get
            {
                return _auxClass.Field1;
            }
            set
            {
                _auxClass.Field1 = value;
            }
        }

        public void Method1()
        {
            _auxClass.Method1();
        }

        public void Method2()
        {
            _auxClass.Method2();
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Option1
        Option1.MainClass mainClass1 = new Option1.MainClass();
        mainClass1.AuxClass.Field1 = "string1";
        mainClass1.AuxClass.Method1();
        mainClass1.AuxClass.Method2();

        // Option2
        Option2.MainClass mainClass2 = new Option2.MainClass();
        mainClass2.Field1 = "string2";
        mainClass2.Method1();
        mainClass2.Method2();

        Console.ReadKey();
    }
}

您喜欢什么选项(选项1或选项2)?在什么情况下我应该使用 option1 或 option2 ? option1 或 option2 (组合、聚合)有什么特殊名称吗?

// stupid title, but I could not think anything smarter

I have a code (see below, sorry for long code but it's very-very simple):

namespace Option1
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;
        public AuxClass1 AuxClass
        {
            get
            {
                return _auxClass;
            }
            set
            {
                _auxClass = value;
            }
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

namespace Option2
{
    class AuxClass1
    {
        string _field1;
        public string Field1
        {
            get
            {
                return _field1;
            }
            set
            {
                _field1 = value;
            }
        }

        // another fields. maybe many fields maybe several properties

        public void Method1()
        {
            // some action
        }

        public void Method2()
        {
            // some action 2
        }
    }

    class MainClass
    {
        AuxClass1 _auxClass;

        public string Field1
        {
            get
            {
                return _auxClass.Field1;
            }
            set
            {
                _auxClass.Field1 = value;
            }
        }

        public void Method1()
        {
            _auxClass.Method1();
        }

        public void Method2()
        {
            _auxClass.Method2();
        }

        public MainClass()
        {
            _auxClass = new AuxClass1();
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Option1
        Option1.MainClass mainClass1 = new Option1.MainClass();
        mainClass1.AuxClass.Field1 = "string1";
        mainClass1.AuxClass.Method1();
        mainClass1.AuxClass.Method2();

        // Option2
        Option2.MainClass mainClass2 = new Option2.MainClass();
        mainClass2.Field1 = "string2";
        mainClass2.Method1();
        mainClass2.Method2();

        Console.ReadKey();
    }
}

What option (option1 or option2) do you prefer ? In which cases should I use option1 or option2 ? Is there any special name for option1 or option2 (composition, aggregation) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩套路吗 2024-09-09 21:09:30

根据德米特定律,选项2。这样你就可以自由地改变MainClass的实现,你不必担心调用依赖于AuxClass1细节的代码,并且如果需要的话确实可以完全删除它。

According to Law of Demeter, Option2. That way you can freely change the implementation of MainClass, You don't have to worry about calling code relying on details of AuxClass1, and indeed can remove it entirely if needed.

禾厶谷欠 2024-09-09 21:09:30

编辑

interface IAuxClass1
{
    string Field1 { get; set; }
    void Method1();
    void Method2();
}

class AuxClass1 : IAuxClass1
{
    string _field1;
    public string Field1
    {
        get
        {
            return _field1;
        }
        set
        {
            _field1 = value;
        }
    }

    // another fields. maybe many fields maybe several properties 

    public void Method1()
    {
        // some action 
    }

    public void Method2()
    {
        // some action 2 
    }
}

public class MyClass : ServiceContainer
{
    public MyClass()
    {
        this.AddService(typeof(IAuxClass1), new AuxClass1());
    }

    public MyClass(IAuxClass1 auxClassInstance)
    {
        this.AddService(typeof(IAuxClass1), auxClassInstance);
    }

    public IAuxClass1 AuxClass
    {
        get
        {
            return (this.GetService(typeof(IAuxClass1)) as IAuxClass1);
        }
    }
}

原始

我认为 MainClass 应该从 AuxClass 派生..

class MainClass : AuxClass1
{ 

}

EDIT

interface IAuxClass1
{
    string Field1 { get; set; }
    void Method1();
    void Method2();
}

class AuxClass1 : IAuxClass1
{
    string _field1;
    public string Field1
    {
        get
        {
            return _field1;
        }
        set
        {
            _field1 = value;
        }
    }

    // another fields. maybe many fields maybe several properties 

    public void Method1()
    {
        // some action 
    }

    public void Method2()
    {
        // some action 2 
    }
}

public class MyClass : ServiceContainer
{
    public MyClass()
    {
        this.AddService(typeof(IAuxClass1), new AuxClass1());
    }

    public MyClass(IAuxClass1 auxClassInstance)
    {
        this.AddService(typeof(IAuxClass1), auxClassInstance);
    }

    public IAuxClass1 AuxClass
    {
        get
        {
            return (this.GetService(typeof(IAuxClass1)) as IAuxClass1);
        }
    }
}

Original

I tihnk MainClass should derive from AuxClass..

class MainClass : AuxClass1
{ 

}
少女净妖师 2024-09-09 21:09:30

我将从实现 C# 的一个很好的功能(称为“自动属性”)开始。 而不是编写

private ThisType _myThing;
public ThisType MyThing 
{ 
    get { return _myThing; } 
    set { _myThing = value; }
}

您可以编写

public ThisType MyThing { get; set; }

,编译器将生成完全相同的 IL。除此之外,您可以添加一些选项,例如将 setter 设置为私有:

public ThisType MyThing { get; private set; }

在您的情况下,我会选择选项 3:

namespace Option3
{
    public AuxClass
    {
        public string Field1 { get; set; }
        public Method1() { ... }
        public Method1() { ... }
    }

    public MainClass
    {
        public AuxClass Aux { get; private set; }
        public MainClass(AuxClass aux)
        {
            this.Aux = aux;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Option3.AuxClass = auxClass3 = new Option3.AuxClass();
        Option3.MainClass mainClass3 = new Option3.MainClass(auxClass3);
        mainClass3.Aux.Field1 = "string2";
        mainClass3.Aux.Method1();
        mainClass3.Aux.Method2();
    }
}

这样,您可以在设置后锁定 AuxClass 引用(如选项 2 所示),而不锁定请自行了解 AuxClass 接口的更改(如选项 1)。

I would start with implementing a nice feature of C# called "automatic properties". Instead of writing

private ThisType _myThing;
public ThisType MyThing 
{ 
    get { return _myThing; } 
    set { _myThing = value; }
}

you can write

public ThisType MyThing { get; set; }

and the compiler will generate the exact same IL. On top of this, you can add some options, for example making the setter private:

public ThisType MyThing { get; private set; }

In your case, I would go for option 3:

namespace Option3
{
    public AuxClass
    {
        public string Field1 { get; set; }
        public Method1() { ... }
        public Method1() { ... }
    }

    public MainClass
    {
        public AuxClass Aux { get; private set; }
        public MainClass(AuxClass aux)
        {
            this.Aux = aux;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Option3.AuxClass = auxClass3 = new Option3.AuxClass();
        Option3.MainClass mainClass3 = new Option3.MainClass(auxClass3);
        mainClass3.Aux.Field1 = "string2";
        mainClass3.Aux.Method1();
        mainClass3.Aux.Method2();
    }
}

This way, you lock the AuxClass reference once it's set (like in Option 2) while not locking up yourself for changes in the AuxClass interface (like in Option 1).

拒绝两难 2024-09-09 21:09:30

选择设计的决定基于不同的因素,

  1. 较短的代码=>选项 1
  2. 监视每个功能和每个访问的活动 =>选项 2,但是使用 linq 和表达式,您可以编写一个甚至可以与选项 1 一起使用的通用代码,但这太复杂了,无法在这里讨论。

Decision of choosing design is based on different factors,

  1. Shorter code => Option 1
  2. Monitor activity of each functionality and every access => Option 2, however using linq and expressions, you can write a generalized code that can work with even option 1, but thats too complicated to discuss here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文