C# 课堂上应该包括什么方法

C# 课堂上应该包括什么方法,c#,design-patterns,code-structure,C#,Design Patterns,Code Structure,我不知道类中应该包含什么类型的方法,服务类中应该编写什么类型的方法 这是我的设想: 我正在编写一个音乐商店应用程序,我的模型设计如下 public class Album { private string title; public string Title { get { return title; } set { title = value; } } private double price; publi

我不知道类中应该包含什么类型的方法,服务类中应该编写什么类型的方法

这是我的设想:

我正在编写一个音乐商店应用程序,我的模型设计如下

 public class Album
{
    private string title;

    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    private double price;

    public double Price
    {
        get { return price; }
        set { price = value; }
    }

    private List<Music> musicFiles;

    public List<Music> MusicFiles
    {
        get { return musicFiles; }
        set { musicFiles = value; }
    }


}

public class Music
{
    private string title;

    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    private string duration;

    public string Duration
    {
        get { return duration; }
        set { duration = value; }
    }

}
哪种解决方案是合理的,或者还有其他解决方案吗


谢谢

我认为最干净的解决方案应该是确定的服务类别,例如“Downloader”。如果下载是一个经常使用的操作,您可以在music file类或其基类上引入facade,以提高代码的可理解性

您的问题的答案是将下载方法放在接口中还是放在抽象基类中,取决于您认为这些操作将如何使用。例如,如果您访问下载操作主要是作为一种能力,例如,您想下载很多东西,但并不真正关心这些项目是什么,那么界面是最佳选择。这是因为接口不限制继承层次结构,而抽象基类限制继承层次结构

如果可以跨多个文件共享操作的实现,那么抽象基类是很好的。因此,如果下载相册的代码与下载音乐文件的代码相同,则使用共享实现的抽象类更合适


通常,您使用对象是基于它们执行某些任务的能力,而这些任务的实现实际上是共享的。在这种情况下,最好的方法是使用一个接口和一个包含共享代码的单独抽象基类。通过这种方式,您可以利用接口和抽象基类的优点。如果您查看BCL,例如在ADO.NET中,许多概念都是以这种方式实现的。

调用您的音乐工件下载也更好,因此您可以在不更改下载调用方接口的情况下更改或添加新工件。这是我对这个问题的理解

请考虑这是伪代码,并用适当的语法编写自己的java代码。< /P>

//Client call

DownloadStore  store  = new DownloadStore(myMusicfile)

store.download();

DownloadStore  store  = new DownloadStore(myAlbum)

store.download();


//your download store
DownloadStore {

IMusicArtifact artifact;

DownloadStore(IMusicArtifact  artifact){
  this.artifact=artifact;
}

public downlod(){

//write common coding for any artifact...

//artifact specific implemenation is called here
artifact.download();

}

}


//your interface class
IMusicArtifact {

download();

}


//your concrete class
Muscifile implements IMusicArtifact {


download(){
// Music file related downloaind stuff
}

}


//your concrete class
Album implements IMusicArtifact {

download(){
// Album related downloaind stuff
}


}

解决方案3:创建一个
FavoriteList
类,该类为
Album
s添加/删除方法;创建一个具有
DownloadAlbum
DownloadSong
方法的服务类,以及本地文件所需的任何其他方法。@只有在实现不同的情况下,为DownloadAlbum和DownloadSong分别设置方法才有用。@Georg只是因为您的接口有不同的方法,这并不意味着实现是单独的,或者可能不共享,例如一个公共文件下载。@poke&Georg,感谢您的回复。DownloadAlbum只是对音乐文件进行迭代并下载。这是我的第一个解决方案。:)但是模型中的下载方法应该参考一些其他的基础设施,我相信基础设施意味着后端文件系统(ftp、OS文件系统等),如果是的话,你是正确的。我更喜欢这个解决方案,因为它可以帮助我们在不改变客户端调用的情况下向下载界面添加任何工件。
//Client call

DownloadStore  store  = new DownloadStore(myMusicfile)

store.download();

DownloadStore  store  = new DownloadStore(myAlbum)

store.download();


//your download store
DownloadStore {

IMusicArtifact artifact;

DownloadStore(IMusicArtifact  artifact){
  this.artifact=artifact;
}

public downlod(){

//write common coding for any artifact...

//artifact specific implemenation is called here
artifact.download();

}

}


//your interface class
IMusicArtifact {

download();

}


//your concrete class
Muscifile implements IMusicArtifact {


download(){
// Music file related downloaind stuff
}

}


//your concrete class
Album implements IMusicArtifact {

download(){
// Album related downloaind stuff
}


}