Share via


パラメーターのデザイン

このトピックのガイドラインに従うと、メンバー パラメーターに適切な型と名前を選択できます。 パラメーターのデザイに関するガイドラインは、次のトピックにもあります。

メンバーに必要な機能を提供する最低派生パラメーター型を使用してください。

このガイドラインを次のコード例に示します。 BookInfo クラスは、Publication クラスを継承します。 Managerクラスには、2 つのメソッドを実装しています。BadGetAuthorBiography・GoodGetAuthorBiography. BadGetAuthorBiographyへの参照を使用してBookInfoで宣言されたメンバーだけを使用してもオブジェクトPublication。 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;
    }
// A class with some basic information.
public ref class Publication
{
private:
    String^ author;
    DateTime publicationDate;

public:
    Publication(String^ author, DateTime publishDate)
    {
        this->author = author;
        this->publicationDate = publishDate;
    }

    property DateTime PublicationDate
    {
        DateTime get() {return publicationDate;}
    }

    property String^ Author
    {
        String^ get() {return author;}
    }
};

// A class that derives from Publication
public ref class BookInfo : public Publication
{
private:
    String^ isbn;

public:
    BookInfo(String^ author, DateTime publishDate, String^ isbn) :
            Publication(author, publishDate)
    {
        this->isbn = isbn;
    }

    property String^ Isbn
    {
        String^ get() {return isbn;}
    }
};

private enum class ErrorOptions {ThrowOnError};

private enum class CasingOptions {CaseInsensitive};

private ref class BetterType
{
internal:
    static void GetType(String^ name,
    ErrorOptions throwOption,
    CasingOptions caseOption)
    {}
};

public ref class Manager
{
public:
    // 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.
}
void BadStoreTimeDifference(DateTime localDate,
    TimeZone^ toWhere,
    Object^ reserved)
{
    // Do work here.
}

void GoodCStoreTimeDifference(DateTime localDate,
    TimeZone^ toWhere)
{
    // Do work here.
}

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.

設計ガイドラインの詳細についてを参照してください、「フレームワークの設計ガイドライン。規則、慣用句、および再利用可能なパターン。ネット ライブラリ」本クシシュトフ Cwalina、ブラッド エイブラムス、アスキー、2005 年発表しました。

参照

その他の技術情報

メンバーのデザインのガイドライン

クラス ライブラリ開発のデザイン ガイドライン