更新 : 2007 年 11 月
このトピックのガイドラインに従うと、メンバ パラメータに適切な型と名前を選択できます。パラメータのデザイに関するガイドラインは、次のトピックにもあります。
メンバに必要な機能を提供する最低派生パラメータ型を使用してください。
このガイドラインを次のコード例に示します。BookInfo クラスは、Publication クラスを継承します。Manager クラスは、BadGetAuthorBiography と GoodGetAuthorBiography の 2 つのメソッドを実装します。BadGetAuthorBiography は、Publication で宣言されたメンバだけを使用する場合でも BookInfo オブジェクトへの参照を使用します。GoodGetAuthorBiography メソッドは、適切なデザインを示しています。
' A Class with some basic information.
Public Class Publication
Dim Protected authorValue as String
Dim Protected publicationDateValue as DateTime
Public Sub new(author as String, publishDate as DateTime)
Me.authorValue = author
Me.PublicationDateValue = publishDate
End Sub
Public Readonly Property PublicationDate as DateTime
Get
Return publicationDateValue
End Get
End Property
Public Readonly Property Author as String
Get
Return authorValue
End Get
End Property
End Class
' A Class that derives from Publication
Public Class BookInfo
Inherits Publication
Dim isbnValue as String
Public Sub new(author as string, _
publishDate as DateTime, _
isbn as String)
MyBase.New(author, publishDate)
Me.isbnValue = isbn
End Sub
Public Readonly Property Isbn as String
Get
Return isbnValue
End Get
End Property
End Class
Public Class Manager
' This method does not use the Isbn member
' so it doesn't need a specialized reference to Books
Shared Function BadGetAuthorBiography(book as BookInfo) as String
Dim biography as String = ""
Dim author as String = book.Author
' Do work here.
Return biography
End Function
' This method shows the correct design.
Shared Function GoodGetAuthorBiography(item as Publication) as String
Dim biography as String = ""
Dim author as String = item.Author
' Do work here.
Return biography
End Function
// A class with some basic information.
public class Publication
{
string author;
DateTime publicationDate;
public Publication(string author, DateTime publishDate)
{
this.author = author;
this.publicationDate = publishDate;
}
public DateTime PublicationDate
{
get {return publicationDate;}
}
public string Author
{
get {return author;}
}
}
// A class that derives from Publication
public class BookInfo :Publication
{
string isbn;
public BookInfo(string author, DateTime publishDate, string isbn) :
base(author, publishDate)
{
this.isbn = isbn;
}
public string Isbn
{
get {return isbn;}
}
}
public class Manager
{
// This method does not use the Isbn member
// so it doesn't need a specialized reference to Books
static string BadGetAuthorBiography(BookInfo book)
{
string biography = "";
string author = book.Author;
// Do work here.
return biography;
}
// This method shows the correct design.
static string GoodGetAuthorBiography(Publication item)
{
string biography = "";
string author = item.Author;
// Do work here.
return biography;
}
予約済みパラメータは使用しないでください。
今後のバージョンのライブラリでは、追加のパラメータを受け取る新しいオーバーロードを追加できます。
次のコード例では、最初に、このガイドラインに違反する不適切なメソッド、次に適切なデザインのメソッドを示します。
Public Sub BadStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone, _
reserved as Object)
' Do work here.
End Sub
Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone)
' Do work here.
End Sub
Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone, _
useDayLightSavingsTime as Boolean)
' Do work here.
End Sub
public void BadStoreTimeDifference (DateTime localDate,
TimeZone toWhere,
Object reserved)
{
// Do work here.
}
public void GoodCStoreTimeDifference (DateTime localDate,
TimeZone toWhere)
{
// Do work here.
}
public void GoodCStoreTimeDifference (DateTime localDate,
TimeZone toWhere,
bool useDayLightSavingsTime)
{
// Do work here.
}
ポインタ、ポインタの配列、または多次元配列をパラメータとして受け取るパブリック公開メソッドは使用しないでください。
ほんどのライブラリを使用する際に、このような高度な機能についての知識は不要です。
結果としてオーバーライド間でパラメータの順序に非一貫性が生じる場合でも、out パラメータはすべて、値渡しおよび ref パラメータ (パラメータ配列を除きます) の後に配置してください。
この規約により、メソッド シグネチャがわかりやすくなります。
メンバをオーバーライドしたり、インターフェイス メンバを実装したりするときは、パラメータに一貫性のある名前を付けてください。
オーバーライドには、同じパラメータ名を使用する必要があります。オーバーロードには、宣言しているメンバと同じパラメータ名を使用する必要があります。インターフェイス実装には、インターフェイス メンバ シグネチャで定義された名前と同じ名前を使用する必要があります。
Portions Copyright 2005 Microsoft Corporation.All rights reserved.
Portions Copyright Addison-Wesley Corporation.All rights reserved.
デザイン ガイドラインの詳細については、2005 年に Addison-Wesley から出版されている Krzysztof Cwalina、Brad Abrams 共著の『Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries』を参照してください。