abstract class IMerch { void buy(); int get pricetag; } class E implements IMerch {…
Private research annotation
Sign in to add a private research label or note.
- User
abstract class IMerch { void buy(); int get pricetag; }
class E implements IMerch { final QuantityMerch _quantityMerch;
E() : _quantityMerch = QuantityMerch();
@override void buy() { /* some E logic / _quantityMerch.buy(); / some E logic */ }
@override int get pricetag => _quantityMerch.priceTag;
int get quantity => _quantityMerch.quantity; // getter for quantity }
class U implements IMerch { final SingleUseMerch _singleUseMerch;
U() : _singleUseMerch = SingleUseMerch();
@override void buy() { /* some U logic / _singleUseMerch.buy(); / some U logic */ }
@override int get pricetag => _singleUseMerch.priceTag;
bool get purchased => _singleUseMerch.purchased; // getter for purchased }
class Merch { int _priceTag = 0;
int get priceTag => _priceTag;
void buy() {/* some logic */} }
class SingleUseMerch extends Merch { bool _purchased = false; // field to keep track of purchase
@override void buy() { if (_purchased) return; // early return
code/* some before logic */ super.buy(); _purchased = true; /* some after logic */}
@override int get priceTag => 100; //example priceTag value
bool get purchased => _purchased; // getter for purchased }
class QuantityMerch extends Merch { int _quantity = 0; // field to keep track of quantity
@override void buy() { /* some before logic */
codesuper.buy(); _quantity++; // increase quantity by 1 /* some after logic */}
int get quantity => _quantity; // getter for quantity
@override int get priceTag => 50; //example priceTag value }
- Assistant
The code above defines an interface
IMerchthat has two methods and an integer property. Two classesEandUimplement this interface.Class
Econtains a field_quantityMerchof typeQuantityMerchand implements thebuyandpricetagmethods fromIMerch. It also defines aquantitygetter to retrieve the quantity of the item purchased.Class
Ucontains a field_singleUseMerchof typeSingleUseMerchand implements thebuyandpricetagmethods fromIMerch. It also defines apurchasedgetter to check if the item has been purchased or not.Class
Merchand its subclassesSingleUseMerchandQuantityMerchdefine the implementation of thebuymethod and apriceTagproperty.SingleUseMerchalso defines apurchasedproperty to keep track of whether the item has been purchased or not, whileQuantityMerchdefines aquantityproperty to keep track of how many of the item have been purchased.