按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
only CalculateTaxToPay() assigns the data member。 The purpose of the data member is to
provide information about the operation CalculateTaxToPay() without giving the exact details
of the operation。
The idea behind _calculatedTaxable and the declaration of CalculateTaxToPay() is to
provide a mechanism where the derived class does not need to calculate things again。 Consider
the example of a country where; if your taxable ine is above 400 currency units; a surtax of
10 currency units is calculated。 You don’t know what your taxable ine is until the function
CalculateTaxToPay() is executed; and that function returns only the total payable taxes。 So
how do you know if you should apply the surtax in this situation? One solution is to reverse
calculate the payable taxes; but that would involve quite a few additional steps。 An easier solution
is to write some code in the base class method of CalculateTaxToPay() that stores the taxable
ine so the subclass has access to it。
The original implementation of CalculateTaxToPay() does not consider a surtax; so the
derived class must contain that functionality。 Since CalculateTaxToPay() can be overridden
without the data member _calculatedTaxable; the derived class would need to implement the
functionality in the base class to calculate whether or not the surtax applies。 Following is an
sample derived class implementation of the tax engine for such a situation; stored in a namespace
called Surtax; to distinguish it from the base functionality。
Namespace Surtax
Friend Class TaxEngine
Inherits BaseTaxEngine
Public Overrides Function CalculateTaxToPay(ByVal account As ITaxAccount) _
As Double
Dim taxToPay As Double = MyBase。CalculateTaxToPay(account)
If _calculatedTaxable 》 400 Then
taxToPay = taxToPay + 10
End If
Return taxToPay
End Function
Public Overrides Function CreateTaxAccount() As ITaxAccount
Throw New Exception(〃The method or operation is not implemented。〃)
End Function
End Class
End Namespace
In the implementation of CalculateTaxToPay(); we replace the Overridable keyword with
Overrides; implying that the functionality of TaxEngine replaces the functionality of BaseTaxEngine。
Since our fictional country calculates the basic tax similarly to most countries; the functionality
of BaseTaxEngine。CalculateTaxToPay() can be used。 Thus; the first line of TaxEngine。
CalculateTaxToPay() is MyBase。CalculateTaxToPay(); meaning the base class (BaseTaxEngine)
method CalculateTaxToPay() is called。
…………………………………………………………Page 199……………………………………………………………
CH AP T E R 7 ■ L E AR N IN G AB O U T CO M P O N E N TS AN D C L AS S H I E R AR C HI E S 177
Calling the base class results in calculating a basic tax to pay amount。 We need to figure out
if a surtax applies; and that is where the protected data member _calculatedTaxable es into
play。 Having called BaseTaxEngine。CalculateTaxToPay(); the data member _calculatedTaxable is
assigned and contains the amount that is being taxed。 Thus; TaxEngine。CalculateTaxToPay()
can make a decision if more than 400 currency units have been earned。 And if so; then the vari
able taxToPay is incremented with another 10 currency units。 Had _calculatedTaxable not
existed; TaxEngine。CalculateTaxToPay() would have needed to call the base class functionality
to get the basic tax rate; and then recalculate the taxable monies to figure out if the surtax applied。
■Note When you use Overrides with methods; you are saying that you need something special。 This does
not imply that the base class functionality will be called。 It implies that you might call the base class functionality
and perform some additional operations。 Thus; when designing base class functionality; it is important to
track calculations or operations using protected data members。 The data members avoid having derived classes
perform the same operations multiple times; slowing down the application and avoiding potential errors。
USING NAMESPACES
Namespaces are used to define classes; interfaces; and structures that belong together。 In this chapter’s
example; there is an American tax engine; a Swiss tax engine; and so on。 For organizational purposes; each
should have its own namespace。 Sometimes you might even create a custom assembly for each set of inter
face implementations; but you would still need to create a namespace。 Creating a namespace is independent
of the fact of whether or not you create a separate assembly。
In the examples; I use namespaces like Surtax and Canada。 Whenever you create a class or a module;
or some other type; you can embed the type within the namespace。 Then to use the newly defined
namespaces in code pieces that are not part of the namespace; use the Imports keyword。
Abstracting Instantiations with Factories
Take a good look at the scope declaration of the tax engine shown earlier and pare it with
the scope declaration for the ITaxEngine interface。 What you will notice is that ITaxEngine is
declared as Public scope; as is BaseTaxEngine; and TaxEngine is declared as Friend。 Going back
to our project structure; this scope declaration implies that any reference to LibTax will see the
interface ITaxEngine and BaseTaxEngine; but not TaxEngine。 For example; the following