PostIT应用程序的类设计 [英] Class design for PostIT application

查看:93
本文介绍了PostIT应用程序的类设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.为了帮助我学习,我正在做一个创建PostIT note应用程序的项目.

我想我需要创建两个类,一个类是实际笔记,然后另一个类来存储和管理打开的笔记列表.它不容易显示,但是像这样;

类-注释/属性-IDnum,xpos,ypos,高度,宽度,标题栏,内容
类-NoteHandler/属性-帖子列表/方法-CreateNew,Delete,SaveChanges

我该如何为NoteHandler创建列表?我在考虑创建一个新的Note对象,然后为其分配一个ID号,然后将其存储在NoteHandler的数组中.然后,以后可以使用该ID号.

我敢肯定,还有另一种方法可以通过创建某种对象集合来实现.我希望没有人会花时间训练我:),尽管那样很好.如果您能指出我要进行研究以使其发挥作用的话,那就太好了.

在此先感谢您.

Hi, I am new to programming. To help me learn, I am doing a project to create a PostIT note application.

I think I need to create two classes, one which is the actual note, then another to store and manage a list of open notes. It''s not easy to show but something like this;

Class - Note / Properties - IDnum, xpos, ypos, height, width, titlebar, content
Class - NoteHandler /Properties - Postit list / Methods - CreateNew, Delete, SaveChanges

How do I go about creating the list for the NoteHandler? I am thinking that I create a new Note object, then assign it an ID number which I then store in an array in the NoteHandler. I can then refer to that ID number later on when I need it.

I''m certain though that there is another way of doing this by creating some kind of collection of objects. I do not expect anybody to spend any time training me :) although that would be nice. If you could point me at what I need to research to make it work that would be great.

Thanks in advance.

推荐答案

很好地阅读了一个寻求学习指南的问题,而不仅仅是请求代码.您已经在寻找解决方案了,因为您已经定义了一个清晰而明确的对象模型,这非常好.

要在.NET中创建集合,您有很多选择:您的选择(几乎)肯定会在两个.NET库中找到:
Nice to read a question that is seeking guidelines for learning, not just a request for code. You are on-track for a solution because you have already defined a clear and unambiguous object model, which is excellent.

For creating a collection in .NET, you have many choices: your choice is (almost) certainly going to be found in the two .NET libraries:
using System.Collections;
using System.Collections.Generic;

在我看来,选择集合类型时要问自己的关键问题是:

1.我将存储的对象类型是否总是相同类型?对于这种情况,我想答案是是".

2.我是否需要从一开始就需要IEnumerable的集合,才能使我使用LINQ为集合上的所有类型的操作提供的精美工具? [1]

一个.我需要能够轻松地对收集对象进行排序吗?

b.我是否需要花哨的方法来查找对象或集合中的对象,然后将其复制到其他对象等? [2]

3.鉴于我需要存储多少个对象以及每件物品的存储成本,我是否在选择收集类型时受到数量的限制,还是受访问时间和集合操作时间的性能方面的限制?注意:这种考虑在几年前更为重要!

4.高级:线程安全性,线程之间或进程之间的通信,序列化和反序列化的考虑?
快速浏览您选择的一些:...假设"Note"是体现" Note对象的类的名称:

在System.Collections中:

1.使用Array或Arraylist:

In my humble opinion, the key questions to ask yourself in choosing a Collection type are:

1. is the type of object I will store always the same type ? For this case, the answer would be "yes," I think.

2. does the collection I need have to be IEnumerable, from the beginning, in order for me to use the fancy tools that LINQ provides for all types of operations on collections ? [1]

a. do I need to be able to easily sort the collection objects ?

b. do I need fancy ways to find an object, or objects in the collection, to copy them to other objects, etc. ? [2]

3. given how many objects I need to store, and the cost of storage per item, am I constrained in my choice of collection types by quantity, or constrained by performance aspects of access time, and collection manipulation time ? Note: this type of consideration was much more important several years ago !

4. advanced: considerations of thread safety, communication between threads, or processes, serialization and de-serialization ?

A quick look at some of your choices: ... assuming that ''Note'' is the name of your class that ''embodies'' the Note object:

in System.Collections:

1. use of an Array or Arraylist:

Note[] NoteArray = new Note[10]; // fixed size, explicitly typed
ArrayList NoteArrayList = new ArrayList(); // dynamic size, untyped

建议您学习并可能实施System.Collections中的其他集合类型:Stack,Queue,SortedList,HashTable.

在System.Collections.Generic中:建议您从基本的Generic List开始,然后研究和/或实现Generic Dictionary的使用:

1.使用简单的通用集合:

Suggest you study and, possibly, implement the other collection types found in System.Collections: Stack, Queue, SortedList, HashTable.

in System.Collections.Generic: Suggest you start with the basic Generic List, then study and/or implement use of a Generic Dictionary:

1. use of a simple generic collection:

List<Note> GenericNoteList = new List<Note>();

2.使用字典:如果您想通过匹配的字符串快速访问给定的Note,也许是Note标题:

2. use of a dictionary: what if you wanted to quickly access a given Note by a matching string, perhaps the Note title:

Dictionary<string, Note> GenericNoteDictionary = new Dictionary<string, Note>();

现在……最后……一个示例解决方案:在VS Studio 2010 Pro中进行了测试.仅限NET FrameWork 4.0客户端目标:

And now ... at last ... a sample solution: tested in VS Studio 2010 Pro with .NET FrameWork 4.0 client target only:

// declared in the body of Form1''s class definition
private Form2 f2;
private Form3 f3;

private void Form1_Load(object sender, EventArgs e)
{
    // instantiate the two other Forms
    f2 = new Form2();
    f3 = new Form3();

    // assign Form2''s button a Click Handler
    // we access the private Form2 Button
    // exposed by a public property which is auto-intialized
    // in Form2''s constructor just after the call to
    // InitializeComponent();
    f2.f2Button.Click += new EventHandler(f2Button_Click);

    // same technique for Form3''s TextBox
    // exposed by a public Property
    f3.f3TextBox.TextChanged += new EventHandler(f3TextBox_TextChanged);
}

要实现的左边是Form1中的f2Button_Click和f3TextBox_TextChanged事件处理程序以及必需的公共属性及其初始化在Form2和Form3中的实现.

在制定好收集策略并对其进行测试之后,也许可以考虑在注释"样式中添加一些附加":每个注释"的日期时间"字段...创建的日期...修改的日期,以及为颜色选择的颜色注意文字和/或背景?

bon apetit,比尔

...编辑#1:少量清理在代码块中混乱的某些标签...
...编辑#2:恢复在编辑#1中意外删除的示例代码;简化了一点...

[1]是的,您可以将某些集合类型转换为以IEnumerable开头的IEnumerable,但这种转换会产生费用".

[2]这听起来可能有点抽象",但确实很实用:例如:将来,您是否希望Post-It类型应用程序的用户轻松找到某种颜色的所有Node,或者在两个日期之间创建? IEnumerable的通用集合使此操作相对容易.

Left for you to implement are the f2Button_Click, and f3TextBox_TextChanged event handlers in Form1, and the implementation in Form2, and Form3, of the necessary public properties and their intialization.

After you get your collection strategy worked out and tested, perhaps consider adding some ''extras'' to the Note style: date-time field for each Note ... date created ... date modified, and a choice of colors for the Note text and/or background ?

bon apetit, Bill

... edit #1: minor clean-up of some tags that got scrambled in the code-blocks ...
... edit #2: restore sample code accidentally deleted in edit #1; simplified it a bit ...

[1] yes, you can convert certain collection types to IEnumerable that are not IEnumerable to start with, but there is a "cost" to that conversion.

[2] this may sound a bit "abstract," but it''s really practical: for example: in the future, do you want users of your Post-It type application to easily find all the Nodes of a certain color, or created between two dates ? Generic Collections which are IEnumerable make this relatively easy.


您可以尝试使用List<Note>集合,并使用扩展方法从列表中查询Note对象.
You can try using a List<Note> collection and use the extension methods to query Note objects from the List.


这篇关于PostIT应用程序的类设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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