Hope your day is going well.
I want to make the following composition of two objects with the same inheritance root:
There will be 3 basic operations on the Wallet class: add
, getCards
and getPaymentCards
. getCards
should return both Card
s and PaymentCard
s. This is a place where I've faced a problem of how to store these cards in the Wallet:
class Card {}
class BadgeCard extends Card {}
class IdCard extends Card {}
class PaymentCard extends Card {}
class WalletT21 {
Set<Card> cards;
Set<PaymentCard> paymentCards;
void add(Card c) {
cards.add(c);
Integer i = 0;
i = 1;
i = 1;
System.out.println(i);
}
void add(PaymentCard c) {
paymentCards.add(c);
}
Set<Card> cards() {
return Stream.concat(cards.stream(), paymentCards.stream())
.collect(Collectors.<Card>toSet());
}
Set<PaymentCard> paymentCards() {
return paymentCards;
}
}
class WalletT22 {
Set<Card> cards;
Set<PaymentCard> paymentCards;
void add(Card c) {
cards.add(c);
}
void add(PaymentCard c) {
cards.add(c);
paymentCards.add(c);
}
Set<Card> cards() {
return cards;
}
Set<PaymentCard> paymentCards() {
return paymentCards;
}
}
All these ideas are, let's say, not perfect. The reason for uniting these classes under the one roof (Wallet) is that the higher code uses cards for identification (every card is an account identifier) and payments (not every card is payable but every paymentcard is an identifier (Card)).
Is there a more elegant way to make such composition?
WalletT21
, where thecards()
method returns the union of the two sets used internally? – Bart van Ingen Schenau Nov 29 '16 at 11:35