Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Las instrucciones de este tema le ayudan a seleccionar los tipos y nombres correctos para los parámetros de los miembros. Los temas siguientes también presentan las instrucciones de diseño para los parámetros.
Utilice el tipo de parámetro menos derivado que proporciona la funcionalidad requerida por el miembro.
En el siguiente ejemplo de código se ilustra esta instrucción. La clase BookInfo hereda de la clase Publication. La clase Manager implementa dos métodos: BadGetAuthorBiography y GoodGetAuthorBiography. BadGetAuthorBiography utiliza las referencias a los objetos BookInfo aunque utiliza únicamente los miembros declarados en Publication. El método GoodGetAuthorBiography muestra el diseño correcto.
' 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;
}
No utilice parámetros reservados.
Las versiones futuras de una biblioteca pueden agregar nuevas sobrecargas que toman parámetros adicionales.
El ejemplo de código siguiente muestra primero un método incorrecto que infringe esta instrucción y, a continuación, muestra los métodos con el diseño correcto.
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.
}
No utilice métodos públicamente expuestos que toman punteros, matrices de punteros, o matrices multidimensionales como parámetros.
No debería ser necesario conocer estas características avanzadas para utilizar la mayoría de las bibliotecas.
Coloque todos sus parámetros de salida después de los parámetros pass-by-value y ref (excluidas las matrices de parámetros), incluso si ello produce una incoherencia en el orden de los parámetros de las sobrecargas.
Esta convención facilita la comprensión de la firma del método.
Sea coherente para denominar los parámetros cuando reemplace miembros o implemente miembros de interfaz.
Los reemplazos deberían utilizar los mismos nombres de parámetro. Las sobrecargas deberían utilizar los mismos nombres de parámetro que el miembro declarante. Las implementaciones de interfaces deberían usar los mismos nombres definidos en la firma del miembro de interfaz.
Portions Copyright 2005 Microsoft Corporation. Reservados todos los derechos.
Portions Copyright Addison-Wesley Corporation. Reservados todos los derechos.
Para obtener más información sobre las directrices de diseño, consulte “las instrucciones de diseño de Framework: Convenciones, frases realizadas y modelos para libro de bibliotecas reutilizables de .NET” de Krzysztof Cwalina y Brad Abrams, publicados por Addison-Wesley, 2005.
Vea también
Otros recursos
Instrucciones de diseño de miembros
Instrucciones de diseño para desarrollar bibliotecas de clases