patternjavaMinor
Card payment strategies
Viewed 0 times
paymentstrategiescard
Problem
In my project, I have 3 payment mode. Via Manually Entered , Card Swipe, and cash. I have 2 fragments named
One of my friends says it would have been good if you have kept in a single file rather than 2 separate files, because when there is a change you will have to change in either files. But I kept them separate because of the Single Responsibility Principle of object-oriented design.
So how could I make my code better and effective accordingly with the Strategy design pattern?
```
public class RecordCardManuallyFragment extends BaseFragment implements View.OnFocusChangeListener {
private View view;
private TextView tvTotal, tvTotalAmount, tvCardInfo, tvNameOnCard, tvCardNumber, tvCardExpiration, tvCVV;
private TextView btnRecordPayment;
private TextView etExpiryDate;
private EditText etNameOnCard, etCardNumber, etCvv;
private RelativeLayout layoutCardNumber;
private CardInformationModel cardInformationModel;
public RecordCardManuallyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_record_card_manually, container, false);
initViews();
setClickListener();
setCustomFonts();
setValuesToUIFromSession();
((HomeActivity) getActivity()).visibilityOfCounter(false);
((HomeActivity) getActivity()).setVisibilityOfEditIcon(false);
return view;
}
private void setValuesToUIFromSession() {
double amount = session.getUserCart().getAmount();
String formattedAmount = String.format("%.2f", amount);
tvTotalAmount.setText("$" + formattedAmoun
RecordCardManuallyFragment and SwipeCadFragment and they both are almost identical according to functionality except in RecordCardManually you have to type info manually and in swipe card fragment you informaion gets filled on the form automatically.One of my friends says it would have been good if you have kept in a single file rather than 2 separate files, because when there is a change you will have to change in either files. But I kept them separate because of the Single Responsibility Principle of object-oriented design.
So how could I make my code better and effective accordingly with the Strategy design pattern?
```
public class RecordCardManuallyFragment extends BaseFragment implements View.OnFocusChangeListener {
private View view;
private TextView tvTotal, tvTotalAmount, tvCardInfo, tvNameOnCard, tvCardNumber, tvCardExpiration, tvCVV;
private TextView btnRecordPayment;
private TextView etExpiryDate;
private EditText etNameOnCard, etCardNumber, etCvv;
private RelativeLayout layoutCardNumber;
private CardInformationModel cardInformationModel;
public RecordCardManuallyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_record_card_manually, container, false);
initViews();
setClickListener();
setCustomFonts();
setValuesToUIFromSession();
((HomeActivity) getActivity()).visibilityOfCounter(false);
((HomeActivity) getActivity()).setVisibilityOfEditIcon(false);
return view;
}
private void setValuesToUIFromSession() {
double amount = session.getUserCart().getAmount();
String formattedAmount = String.format("%.2f", amount);
tvTotalAmount.setText("$" + formattedAmoun
Solution
Your friend is suggesting you have a class that does the common work, and then subclasses to handle specialized work. This is really core OOP.
For example, your base class (say,
Then when you create the subclass, you'd set
Then in each subclass, you'd call
That's just an example, and could be handled differently. Generally speaking, if you see the same block of code in multiple places, you could probably optimize that away.
Single Responsibility Principle is not relevant in any way here. Basic inheritance (https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) and DRY (https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) are much more appropriate to what you're describing.
For example, your base class (say,
CardTransactionFragment) might have an onCreateView method like this:@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(mLayoutId, container, false);
initViews();
setClickListener();
setCustomFonts();
setValuesToUIFromSession();
...Then when you create the subclass, you'd set
mLayoutId somehow (either directly through a setter, or with Intent arguments, or most likely have that class be abstract and define mLayoutId in the subclass).Then in each subclass, you'd call
super.onCreateView, as well as any other specific functionality, in the subclass's definition of onCreateView.That's just an example, and could be handled differently. Generally speaking, if you see the same block of code in multiple places, you could probably optimize that away.
Single Responsibility Principle is not relevant in any way here. Basic inheritance (https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) and DRY (https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) are much more appropriate to what you're describing.
Code Snippets
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(mLayoutId, container, false);
initViews();
setClickListener();
setCustomFonts();
setValuesToUIFromSession();
...Context
StackExchange Code Review Q#133863, answer score: 3
Revisions (0)
No revisions yet.