它是不好的形式,试图比主/其它类Form1中互相交流? [英] Is it bad form to try and have classes other than main/form1 interact with each other?

查看:95
本文介绍了它是不好的形式,试图比主/其它类Form1中互相交流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习我的code使用类正确的方式,当它不是像明显一组客户,犬动物等继承

I'm trying to learn the correct way to use classes in my code, when it's not something obvious like a set of customers, dogs inheriting from animals, etc.

我已经解除code大段成特色,如 Installer.cs Downloader.cs UiManager.cs 。我能找到有这些类与对方的属性​​和方法进行交互的唯一方法是使他们所有静态的,这是我一直在说另外一个问题是错误的路要走。

I've broken off large sections of code into "features" such as Installer.cs, Downloader.cs, UiManager.cs. The only way I can find to have those classes interact with each others' properties and methods is to make them all static, which I've been told in another question is the wrong way to go.

所以我的问题是三件事之一:

So my problem is one of 3 things:


  1. 有一种方法可以让类相互交谈,我只是不明白呢。

  1. There is a way to make classes talk to each other that I just don't understand yet.

类不应该试图说服对方,而是进行一次性操作,然后返回一些回主/ Form1中,其中主要的类可以然后用它来传递到另一个类的一次性行动。

Classes should never try to talk to each other, but perform one-off actions and then return something back to main/form1, which the main class can then use to pass into another class for a one-off action.

类是真的只为赚很多情况下有用,并且有一些其他的结构完全是我需要了解从主类抽象的功能大块了。

Classes are really only useful for making lots of instances, and there's some other structure entirely I need to learn about for abstracting large chunks of functionality out from the main class.

所有的教程我能找到和讲座我看似乎只告诉你如何类工作的,而不是何时以及如何在实际产品中使用它们。

All the tutorials I can find and lectures I watch seem to only tell you how classes work, but not when and how to use them in a real product.

编辑 - 更具体的例子:

EDIT - A more specific example:

说我有一个字符串,它是中央的整个应用程序,并且需要看到和/或每类潜在的修改。我怎么走动的code的信息,而无论其在一个课堂上的一切,或使其静态的?

Say I have one string that is central to the entire app, and needs to be seen and/or modified potentially by every class. How do I move that information around the code without either having everything in one class or making it static?

我看不到的方式来让该字符串生活在 Form1中不使其静态的(因为所有形式的活动和功能需要能够看到它将它传递给一个类)。

I can't see a way to let that string live in Form1 without making it static (because all the form events and functions would need to be able to see it to pass it to a class).

我看不到的方式把字符串转换成另一个类,而无需使字符串和全班静态的,因此其他类可以看进去。

I can't see a way to put the string into another class without having to make the string and the whole class static, so other classes can see into it.

也许有什么我缺少有关实际实例化类,使物体互相交流。

Maybe there's something I'm missing about actually instantiating the classes, and making objects interact with each other.

推荐答案

我觉得所有的直觉是对的。

I think all your intuitions are right.


  1. 没有,有没有。静态或实例。

  1. No, there's not. Static or instance.

这是一个设计选择(而且有很多在那里)。我是一个务实的,所以我认为,产生spaguethi code一个不好的设计模式选择的设计模式。但是,对于一个项目一个糟糕的设计模式,可以是另一个项目的一个很好的设计模式。尝试读取深入浅出设计模式的书。

It's a design choice (and there's a lot out there). I'm a pragmatic, so I consider a design pattern that generates spaguethi code a bad design pattern choice. But a bad design pattern for a project can be a good design pattern for another project. Try to read the Head First Design Pattern book.

是的,有接口和抽象类。

Yes, there are interfaces and abstract classes.

一些更多的想法:

我不认为一定要避免使用静态方法或类。什么必须避免的是思念使用静态方法或类,就像一个语言里面的小姐用应有尽有。但是很难界定什么是思念使用静态的,因为静态方法或类是格外危险的,人们喜欢说,避免static关键字都没有。静态方法会在内存中,除非你结束你的应用程序,所以如果你不处理静态方法内部的连接,你将有一个非常糟糕的一天。

I don't think the use of static methods or classes must be avoided. What must be avoided is the miss use of a static method or class, like the miss use of everything inside a language. But is very hard to define what's a miss use of a static, and because static methods or classes are particulary dangerous, people like to say to avoid the static keyword at all. A static method will be in memory unless you end your application, so if you don't dispose a connection inside a static method, you will have a very bad day.

我有一个实用工具项目和公用事业项目中我有一个数据类。数据类提供对数据库的访问。这是一个静态类。为什么呢?

I have a utility project, and inside the utility project I have a data class. The data class provides access to the database. It's a static class. Why?

嗯,首先,它是静态的,因为连接字符串来自webconfig。所以我有一个静态构造函数(运行一次应用程序启动时和类被提及)谁读webconfig并存储在一个静态私有成员变量的字符串。我觉得这是一个很大优于阅读webconfig文件,并创建一个范围变量10 bilion次一天。的方法是静态的,因为它们是足够简单的,这意味着它们不需要很多的配置工作,他们只需要几个参数,它们仅用于数据访问的项目。我所有的网站的用户所使用的方法(静态的)的同一个实例,但每个人都使用不同的参数静态方法,因此他们得到它(它们共享管道,但他们喝的水不同),不同的反应。它是只需要额外照顾的方法内清洁的一切(每处理范围的实例),因为如果你不自己将留在内存中。最后,我的经营业务是关于数据处理,非静态数据类意味着更多的内存使用比静态的(CPU使用率几乎是相同的两个图案)。

Well, first of all, it is static because the connection string comes from the webconfig. So I have a static constructor (runs once when the application starts and the class is mentioned) who reads the webconfig and store the string in a static private member variable. I think it's a lot better than read the webconfig file and create a scoped variable 10 bilion times day. The methods are static because they are simple enough, meaning that they don't need a lot of configuration to work, they just need a few parameters and they are used only in the data access project. All my website users are using the same instance of the method (the static one), but everyone uses the static method with different parameters, so they get different responses from it (they share the pipe, but they drink different water). It's only necessary extra care inside the method to clean everything (dispose every scoped instance), because if you don't they will stay in memory. And finally, my bussiness is about data manipulation, a non static data class means a lot more memory usage than a static one (the cpu usage is almost the same in both patterns).

public abstract class Data
{

    [...]

    static Data()
    {
        #if DEBUG
            _Connection = ConfigurationManager.AppSettings["debug"];
        #endif

        #if RELEASE
            _Connection = ConfigurationManager.AppSettings["release"];
        #endif

        [...]
    }

    [...]

}

在这一天结束时,我使用静态时:

In the end of the day I use static when:


  1. 如果它很简单(我可以控制每一个环节);

  2. 如果它足够小(我使用扩展方法来验证,他们是静态的)和;

  3. 如果使用它很重。

除此之外,我使用层来组织我的项目(​​POCO +工厂模式)。我有一个实用的项目,那么实体样板工程,然后访问的项目,则经营业务逻辑的项目,然后一个网站,一个API,经理,等等。在公用事业项目的类不相互影响,但实体模型项目中的类做(一类可以有它里面另一个类的实例)。实体模型的项目不与公用事业项目交互,因为它们具有相同的水平,他们相互影响在另一个层面上,在接入项目,但它是在数据操作的项目更直观。

Besides that, I use layers to organize my project (poco + factory pattern). I have a utility project, then a entity model project, then a access project, then a bussiness logic project, then a website, a api, a manager, and so on. The classes in the utility project don't interact each other, but the classes inside the entity model project do (a class can have a instance of another class inside it). The entity model project don't interact with the utility project because they have the same level, they interact each other in another level, in the access project, but it's more intuitive in a data manipulation project.

这篇关于它是不好的形式,试图比主/其它类Form1中互相交流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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