English 中文(简体)
用于整个方案的公共静态数据
原标题:Public static data used throughout program

守则的例子有C#,但这是一个总的问题。

我知道,根据业务处的规则,应当尽量减小轮.,只要有可能,成员就应当保持非公开。

Consider this example:

你们正在撰写一套分门别类的数据集(不谈论System.Data.DataSet)的校外程序,在节目的字面上使用。 事实上,该方案基本上只是装上、显示、操纵和节省数据集。 此外,任何时候都只能安装一个数据集,并在方案开放时装上。

如果我们严格遵守办事处规则,我们本会这样做。

public void ShowSomeGraphs(IData data)
{
  // do stuff with data which implements IData
}

然而,我们可以在<条码>中储存一个<条码>的公开静态数据成员,例如:

public void ShowSomeGraphs()
{
  // do stuff with Program.Data
}

一方面,我们交换了稍微短的功能签名,用于大增的类别政变。 另一方面,我们已不再通过一个数据参数,实际上达到every功能,every where

right>。 答案可能是:尽可能避免交火。 当地数据变数只是点数,因此,记忆管理可忽略不计,而且由于这些类别被分解,以后可在其他地方使用。

尽管现实地说,在不同应用中,数据类别的结构可能有很大的不同,因此,它并不像你那样只是从该方案中提取一个类别,而在其他地方却没有任何麻烦。 需要更多的时间和努力,以便用can 仅仅放弃可能难以为利益攸关方辩护。

I m working on this sort of program now, and I have used the OO-canon approach: Data parameters are passed around where needed I have minimised class coupling with an IData interface to generalise the data set for future code re-use. Given the application, I m almost certain this code won t ever be re-used. Without these extra interfaces and abstraction, the program would have worked exactly the same as far as the end user is concerned, but would have represented significantly less headaches and development time for me.

你们对此有何想法? 您是否认为,有必要花一切额外时间来撰写接口和一般化,以确保课堂尽可能脱节,特别是当你发现别处使用的课堂时?

最佳回答

Don t agonise over it. Seriously.

有软件范式/主子帮助我们,而不是以礼貌的方式加以遵循。

你在你们的提问中明确表示,你认为 coup然的争 coup是多余的,你可以说明原因。 因此,不使用。

问题回答

如何使用singleton patterns,以提供获得IData接口的方法或阅读财产? 这样,你只与一个非常小的单一吨级相加,而你与数据集的所有互动都是通过IData接口进行的。

(I would definitely avoid the tight coupling. Even if you don t plan to do much with this app chances are that you will run into an issue during development which will force you to touch significantly more code than if you accessed the data via an interface.)

Code sample of the singleton solution proposed above:

using System;

public class MyClass {
    public static void Main() {
        // simple usage:
        Console.WriteLine("From Main: " + Singleton.Instance.IMyData.GetData());
        // client code from another type:
        new ClientObj().DoWork();
        Console.ReadKey();
    }
}

public sealed class Singleton {
    // standard singleton stuff:
    private static readonly Singleton _instance = new Singleton();
    private Singleton(){}
    public static Singleton Instance {get { return _instance; }}
    // data interface stuff:
    private MyData _myData = new MyData();
    public IData IMyData {get { return _myData; }}
}

// the interface:
public interface IData {
    string GetData();
}

// concrete implementation of the data class
public class MyData : IData {
    public string GetData() {return "Hello World!";}
}

// example of a type using the singleton and the IData interface
public class ClientObj {
    public void DoWork() {
        IData data = Singleton.Instance.IMyData;
        string str = data.GetData();
        Console.WriteLine("From other obj: " + str);
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 一些珊瑚礁: 上述代码样本被完全删除,以显示单一州和共享接口的概念。 没有安装安全线,也没有数据标的初始化。

Well, there s one big assumption in your text: There will always only be one data set in the program. Are you sure that condition will hold for all time? There was a time where word processors could only hold one text at a time. Today it s standard to be able to have several files open at once. I d also not be surprised if the first web browsers could only open one web page at a time. Today nobody would use a web browser which could not have several pages open at the same time. I think the sort of object where you can say there will be for certain only one of it in the program, ever, is quite rare. Indeed, the only thing which I would make a global object or singleton would be object factories.

另一方面,通过每项功能的标语似乎对我来说也是多余的。 因此,我要谈谈中间点: “全球”目标是什么,因此你只得通过建筑商通过。 这限制了每个目标对一个数据标的,但是如果你决定,仍然允许你很容易地将几个数据标列入你的方案。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...