多态性: 关于多态性的定义, 我还没有想出一个通俗的定义.
举例说明:
Car, Truck, Roadster, Vehicle,前面说过.Vehile是车辆, 也就是Generalized class, Car, Truck, Roadster是Vehicle的Specialized class.
而且这么多各类的车, 我有一个买车的操作. Buy, 在买车的时候, 需要根据买车人描述的车的种类, 车的价格,然后来返回车的类型. 哪种车, 什么价格, 这就需要BA去和stakeholder商量了. 于是,我说, 我要买车, 我给了Vehicle一个请求, 提供了必务的条件, 然后Vehicle返回了一款车给我, 可能是Bus, Car也可能是Roadster, 这是根据我给Vehicle的条件来决定的. 也就是说Vehicle是多样的.
同样的, 比如说Buy的这个方法, 在Vehicle中已经声明, 在Bus, Car中也可以使用, 但是不同的车购买的流程可能不一样. 因此就要在Bus的Buy, Car的Buy是重新书写这个购买的过程. BA便需要将这些描述清楚.
BA 所要做的, 就是找出这些东西, 如果Bus, Car的购买流程与Vehicle没有什么不一样的话, 便执行Vehicle中的Buy, 如果有不同的话, 就要在Bus, 或者是Bar中指出,并说明. 这里的说明不是长篇大论, 并不需要解释,什么条件, 情况下执行这个, 什么条件下,执行那个,只是说 "有不同" 就OK了~
P.S.
如果在C#中想要体现多态性, 虚函数是最好的方式之一.
下面的E文,是用另一个例子来描述的.
Polymorphic Objects
Suppose that a financial company handles different subtypes of Funds, such as an Asia Fund, Domestic Fund, and so on, each with its own idiosyncrasies. The BA models this situation using a generalized class, Fund, and a specialized class for each subtype of Fund.
Next, the BA moves on to capture investment rules in an Investment class. Checking with the stakeholders, the BA finds that one of its operations, invest capital, deals with all Funds the same way, regardless of subtype. The BA handles this by ensuring that the documentation for the invest capital operation refers exclusively to the generalized class Fund—not to any of its specializations.When the operation is actually executed, though, the Fund object will take on one of many forms—for example, an Asia Fund or a Domestic Fund. In other words, the Fund object is polymorphic.
Polymorphic Operations
Continuing with the same example, since all the Fund subtypes have to be able to accept deposits, the BA defines a Fund operation called accept deposit. This operation is inherited by all the specializations. The BA can also specify a method for this Fund operation that will be inherited by the specializations. But what if one or more of the specializations— for example, the Asia Fund—uses a different procedure for accepting deposits? In this case, the BA can add documentation to the Asia Fund class that describes a method that overrides the one inherited from the generalized class. (For example, the method described might involve supplementary charges.) In practice, when capital investment causes a Fund to accept a deposit, the method that is used to carry out the operation will take on one of many forms. This is what is meant by a polymorphic operation.With polymorphic operations, the selection of the method depends on which particular class (Asia
Fund, Domestic Fund, and so on) is carrying it out.
Read more...
Recent Comments