分享
 
 
 

On Designing Good Libraries -- Part II

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Brad Abrams

You asked for it, you got it.. feedback always welcome.

Attribute Usage

?FONT face="Times New Roman" size=1 Suffix with 揂ttribute?O:P

?FONT face="Times New Roman" size=1 Perf tip: seal attribute classes for faster runtime lookup

?FONT face="Times New Roman" size=1 Specify the AttributeUsage attribute completely

?FONT face="Times New Roman" size=1 Don抰 rely on the defaults!

?FONT face="Times New Roman" size=1 Be as restrictive as possible

?FONT face="Times New Roman" size=1 You can always open it up later

[AttributeUsage(

AttributeTargets.All,

Inherited = true,

AllowMultiple = false)]

?FONT face="Times New Roman" size=1 AttributeTargets ?where is the attribute allowed to be applied?

?FONT face="Times New Roman" size=1 Inherited ?should derived members/types be considered to have this attribute?

?FONT face="Times New Roman" size=1 AllowMultiple ?Is it legal to put more than one instance of the attribute on a particular member?

?FONT face="Times New Roman" size=1 Use constructor arguments for required parameters (positional arguments)

?FONT face="Times New Roman" size=1 Provide a read-only property with the same name

?FONT face="Times New Roman" size=1 Use read-write properties for optional parameters (named arguments)

?FONT face="Times New Roman" size=1 Never use overloaded constructors

[AttributeUsage(AttributeTargets.All, AllowMultiple=true,

Inherited=false)]

public class NameAttribute : Attribute {

public NameAttribute (string userName) {..}

public string UserName {get {..}}

public int Age {get {..} set{..}}

} //end class

Usage:

[NameAttribute("Bob", Age=87)]

UserName ?positional argument

Age ?named argument

Static Classes

?FONT face="Times New Roman" size=1 Static classes contain just static members

?FONT face="Times New Roman" size=1 Compromise between pure OO design with usability

?FONT face="Times New Roman" size=1 Commonly used for

?FONT face="Times New Roman" size=1 Shortcuts for other operations (System.IO.File)

?FONT face="Times New Roman" size=1 Functionality for which a full OO wrapper is unwarranted (System.Environment)

public sealed class Environment {

private Environment(){} //prevents creation

public static void Exit(int exitCode) {..}

public static int ExitCode {

get {..}

set {..}

}

public static string CommandLine {

get {..}

}

}

?FONT face="Times New Roman" size=1 Best used when:

?FONT face="Times New Roman" size=1 Clear charter for the class

?FONT face="Times New Roman" size=1 Not a 搈iscellaneous?bucket

?FONT face="Times New Roman" size=1 Not the center point of a design

?FONT face="Times New Roman" size=1 Use sparingly

?FONT face="Times New Roman" size=1 Watch out for disconnected design

?FONT face="Times New Roman" size=1 Static classes

?FONT face="Times New Roman" size=1 Are sealed

?FONT face="Times New Roman" size=1 Have private default constructor

?FONT face="Times New Roman" size=1 No instance members

?FONT face="Times New Roman" size=1 Static Classes: Bad Design

?FONT face="Times New Roman" size=1 Late in the final milestone we added a method to tell if the runtime is being shut down

?FONT face="Times New Roman" size=1 However we added the method as an instance method making it completely uncallable

?FONT face="Times New Roman" size=1 public sealed class Environment {

private Environment() {} //Prevent creation

// ---snip---

public bool HasShutdownStarted {

get { return nativeHasShutdown(); }

}

public static string UserName { get {...} }

private static extern bool nativeHasShutdown();

// ---snip---

}

?FONT face="Times New Roman" size=1

Constructors

?FONT face="Times New Roman" size=1 Do minimal work in the constructor

?FONT face="Times New Roman" size=1 Only capture the parameters

?FONT face="Times New Roman" size=1 Cost is delayed

?FONT face="Times New Roman" size=1 You can throw exceptions from constructors

?FONT face="Times New Roman" size=1 Be consistent in the ordering and naming of constructor parameters

public class Foo {

private const string defaultA = "..";

private const string defaultB = "..";

public Foo():

this(defaultA,defaultB) {}

public Foo(string a):

this(a, defaultB) {}

public Foo(string a, string b) {

/* do work here */

}

}

?FONT face="Times New Roman" size=1 Many languages automatically add a public default constructor if you don抰 specify any

?FONT face="Times New Roman" size=1 Abstract classes get a protected constructor

?FONT face="Times New Roman" size=1 These two code snippets are equivalent:

public class Foo {

}

public class Foo {

public Foo () {}

}

?FONT face="Times New Roman" size=1 Always explicitly add a default constructor to avoid versioning issues

?FONT face="Times New Roman" size=1 Adding a new constructor removes the default one, breaking clients

// V1

public class Foo {

}

Calling Code works: Foo f = new Foo()

// V2

public class Foo {

public Foo (int value)

}

Calling code breaks: Foo f = new Foo()

Method Usage

?FONT face="Times New Roman" size=1 Use overloading only when the overloads do semantically the same thing

?FONT face="Times New Roman" size=1 Incorrect overload:

String.IndexOf(string value) {}

String.IndexOf(char[] anyOf) {}

?FONT face="Times New Roman" size=1 Correct overload:

Convert.ToString(int value) {}

Convert.ToString(double value) {}

?FONT face="Times New Roman" size=1 Used to avoid boxing

?FONT face="Times New Roman" size=1 Write(object) works for any type

?FONT face="Times New Roman" size=1 But specialization avoids boxing

?FONT face="Times New Roman" size=1 Do only when completely special casing

public static void Write (bool value);

public static void Write (int value);

public static void Write (double value);

public static void Write (object value);

?FONT face="Times New Roman" size=1 Use appropriate default values

?FONT face="Times New Roman" size=1 Simple method assumes default state

?FONT face="Times New Roman" size=1 More complex methods indicate changes from the default state

MethodInfo Type.GetMethod (string name);

//ignoreCase = false

MethodInfo Type.GetMethod (string name,

boolean ignoreCase);

?FONT face="Times New Roman" size=1 Use a zeroed state for the default value (such as: 0, 0.0, false, 摂, etc.)

?FONT face="Times New Roman" size=1 Be consistent in the ordering and naming of method parameters

?FONT face="Times New Roman" size=1 Only the method with the most parameters should be virtual if needed

public class Foo {

private const string defaultForA = "a default";

private const int defaultForB = 42;

public void Bar(){

Bar(defaultForA, defaultForB);

}

public void Bar (string a){

Bar(a, defaultForB);

}

public /*virtual*/ void Bar (string a, int b){

// core implementation here

}

}

?FONT face="Times New Roman" size=1 Variable number of arguments (e.g. printf)

?FONT face="Times New Roman" size=1 Use params

public static string Format(string format,

params object[] args);

?FONT face="Times New Roman" size=1 Not used for in/out params (e.g. scanf)

?FONT face="Times New Roman" size=1 Only provide overloads for performance reasons IF you special case each code path

void Format (string formatString, object arg1)

void Format (string formatString, object arg1, object arg2)

void Format (string formatString, params object [] args)

?FONT face="Times New Roman" size=1 Allowing method inlining by the JIT

?FONT face="Times New Roman" size=1 Minimize the use of virtual methods

?FONT face="Times New Roman" size=1 Don抰 write really large methods

?FONT face="Times New Roman" size=1 Don抰 have large numbers of locals

More to come...

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有