I wanted to know what the advantages of using each of the following methods to differentiate between the main subclasses / Damages are the parent class and handle them differently. I know this is very basic, but I have not been able to compare it completely anywhere between these methods. For example: - I have payment super nectar class and two extended classes one-time payments and membership - my One method, switchpaymentstate , is to manage each one of these types differently
-
Option 1: Use of example
Payment of public zero switch payment (payment payment) {if (event of payment oneTimePayment} {// do something} and if ( Payment for membership) {// anything else}}
-
Option 2: Use of ANIM type logic (or other ...)
Public anonymous paymenttap {ONE_TIME_PAYMENT, SUBSCRIPTION; } Public abstract payment (form of payment type) {this.type = type; } Public OneTimePayment () {Super (ONE_TIME_PAYMENT); } Public membership () {super (subscription); }
And then:
Payment of Public Zero Switch Payment (Payment Payment) {Switch (Payment Type) {Case ONE_TIME_PAYMENT: // Something Smash ; Case subscription: // Something break; Option}: Use of overload methods
Payment of public zero switch payment (one-way payment payment) {/ / Something to do} Public Void SwitchPaymentState (subscription payment) {// something}
So, the best way to go (or a whole other) The way?) And why?
Edit: Based on the type of class I need to do, it is not working in class, I need to pay some data form and It will not help other services, then implement such kind of functionality within the classroom and making calls without calls to it, unfortunately will not help in this matter. Thanks!
Your option 3 will not work in many cases, because at least the time the overloading is resolved Goes on walking time. If your reference type is payment
, it is not possible to use overloading.
In the context of object-oriented design, the use of override methods is the "clean" method. However, its disassimity is that similar functionality is on many sections, while the solution in the switch and installation is all together.
An alternative which provides the best of both worlds is the so-called Visitor Pattern. You create an interface payment visitor
for each category, which you want to have Take care, like:
Interface payment visitor {void visitOneTimePayment (OneTimePayment payment); Zero travel membership (subscription payment); }
After that you have a method attack in superstas:
abstract class payment {... abstract zero call visitor (payment visitor visitor ); }
which you apply to all subclasses as follows:
Class OneTimePayment {... @ Override Zero Call Visitors {Payment Visitors} {visitor. HandleOneTimePayment (this); }} Class membership {... @ override zero call visitor (payment visitor) {visitor.handle membership (this); Now, in all cases where you would otherwise write something (in pseudo-java): switch (type x) {case OneTimePayment: // code break; Case membership: // code break; }
Now you can type clean and type-safe:
x.callVisitor (new payment widget) {@Override void handleOneTimePayment (OneTimePayment payment) {// code} @ Override zero handbus (subscription payment) {// code}});
Note that the visitor has been implemented in the internal class, so the method can still reach all the (effective) final variables defined in the body.
No comments:
Post a Comment