了解泛型的.Net - 银行域名 [英] Understanding .Net Generics - Bank Domain
问题描述
这是学习泛型(与.NET 4.0)的尝试。我一直在编程了约4.5年。直到现在我还没有使用实时项目泛型。我一直在做的是阅读一些泛型文章,并尝试去了解它所有的时间。现在的问题是 - 他们大多试图解释可用泛型各种语法。他们举例,如方形,圆形和形状解释。
现在我有机会设计一个小的应用程序。我想使用泛型那里。 [我看到泛型是在我的新项目一个很好的候选人的好机会]
我想出现在是从理解的意图银行域为例泛型。我想了解以下4。
1)泛型类
2)泛型方法
3)通用接口
4)泛型委托
编辑:运营,并且是类型独立是仿制药很好的候选人。这是我在下面的例子中错过了最大的点之一。
我已经创建了通用类的例子。 ?能否请您与其他三个项目与银行领域的简单例子帮助
请注意:当使用泛型类的,我才知道,它在打开帮助 - 封闭原理。即使我添加新的帐户类型,泛型类需要改变。不断变化的逻辑(利息计算)变为具体的类中。
请注意:在下面,语法可能不正确,因为它键入它没有一个Visual Studio。但观认为好
编辑:会的AccountManager根据其作用的BankAccount类更多更好的名字? ?这是任何一种反模式
泛型类 - 有银行网域范例
公用接口IBankAccount
{
公众诠释的利息;
公众诠释DepositedAmount;
公众诠释DurationInMonth;
}
公共类FixedAccount:IbankAccount
{
公众诠释利息
{
得到
{
返回(DurationInMonth * 0.5)
}
}
公众诠释DepositedAmount {获取;集};
公众诠释DurationInMonth {获取;集};
}
公共类SavingsAccount:IbankAccount
{
公众诠释利息
{
得到
{
返回((DurationInMonth / 2)* 0.1)
}
}
公众诠释DepositedAmount {获取;设置};
公众诠释DurationInMonth {获取;集};
}
类公共及的BankAccount LT; T>其中T:IbankAccount
{
笔账=新T();
公共无效的createAccount(INT持续时间,INT金额)
{
帐户。 DurationInMonth =持续时间;
帐户。 DepositedAmount =入账金额;
INT interestVal =帐户。利益;
SaveToDatabase(T);
}
}
阅读:
-
决定何时何地使用泛型
的http:// en.csharp-online.net/CSharp_Generics_Recipes -Deciding_When_and_Where_to_Use_Generics_Problem
<李>
只是我的两分钱,因为@Lijo问我在这里评论。
我会去与最上面的答案。
,而总结,泛型是的无类型行为重用。你的泛型类型有其实是一个 IBankAccount
哪位是一个非常特殊的接口 - 大概是说这是不对的。我不是说你不能使用限制为一个接口,但该接口将是一个非常通用的接口本身,如的IDisposable
或 IConvertible
。
This is an attempt to learn Generics (with .Net 4.0). I have been programming for about 4.5 years. Till now I have not used Generics in real time projects. All the time what I have been doing is reading some article about generics and try to understand it. The problem is – most of them try to explains various syntax available with Generics. They explain with examples such as Square, Circle and shapes.
Now I have got a chance to design a small application. I would like to use Generics there. [I do see good chances of Generics being a good candidate in my new project]
What I have come up with now is an example from Bank domain with the intention of understanding Generics. I am trying to understand the following 4.
1) Generic classes
2) Generic Methods
3) Generic Interfaces
4) Generic Delegates
EDIT: Operations that are type-independant are good candidates for generics. This is the one of the biggest points I missed in my following example.
I have created an example for "Generic classes". Could you please help with simple examples for other three items with the Bank domain?
Note: While using Generic class, I came to know that it helped in Open-Closed Principle. Even if I add new account type, the generic class need to change. The changing logic (interest calculation) goes inside the specific class.
Note: In the following, the syntax may not be correct as it typed it without a Visual Studio. But the concept holds good.
EDIT: Will "AccountManager" be a more better name for "BankAccount" class based on its role? Is it any kind of anti-pattern?
Generic Class - Example with Bank Domain
Public Interface IBankAccount
{
Public int Interest;
Public Int DepositedAmount;
Public int DurationInMonth;
}
Public class FixedAccount: IbankAccount
{
Public int Interest
{
Get
{
Return (DurationInMonth*0.5)
}
}
Public Int DepositedAmount {get;set};
Public int DurationInMonth {get;set};
}
Public class SavingsAccount: IbankAccount
{
Public int Interest
{
Get
{
Return ((DurationInMonth/2)*0.1)
}
}
Public Int DepositedAmount {get;set};
Public int DurationInMonth {get;set};
}
Public Class BankAccount<T> Where T: IbankAccount
{
T account = new T();
Public void CreateAccount(int duration, int amount)
{
account. DurationInMonth = duration;
account. DepositedAmount = amont;
int interestVal = account. Interest;
SaveToDatabase (T);
}
}
READING:
Generics vs inheritance (when no collection classes are involved)
A Factory Pattern that will satisfy the Open/Closed Principle?
Deciding When and Where to Use Generics http://en.csharp-online.net/CSharp_Generics_Recipes—Deciding_When_and_Where_to_Use_Generics_Problem
Just my two cents, since @Lijo asked me to comment here.
I would go with most of the above answers.
But to summarise, Generics is typeless reuse of behaviour. The fact that your generic type has to be an IBankAccount
-which is a very specific interface - is saying this probably is not right. I am not saying that you cannot use restrictions for an interface but that interface would be a very generic interface itself such as IDisposable
or IConvertible
.
这篇关于了解泛型的.Net - 银行域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!