in Education by
let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); } I want to create a new class implementing the above interface but i need to implement a function with another parameter public class MacroConditionChecker implements ConditionChecker { public boolean isInCondition(Person p, MacroView mv); } Two problems: One: if i change the interface signature to boolean isInCondition(Person p, MacroView mv); then i need to update all the classes implementing ConditionChecker Two: I want the consumers of ConditionChecker to just call isInCondition as-is I think that means: public class MacroConditionChecker implements ConditionChecker { private static final MacroView mv; public MacroConditionChecker(MacroView mv) { this.mv = mv; } public boolean isInCondition(Person p){ // now i have access to MacroView } } so, the only change i need is make at the time I decide to use MacroConditionChecker and the call to isInCondition is not changed Am i on the right track? or Is there some way else to accomplish this? or should i separate out MacroView as its own interface public class MacroConditionChecker implements ConditionChecker implements MacroView JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
ConditionChecker reminds Command design pattern. Comment from the linked page: Command decouples the object that invokes the operation from the one that knows how to perform it. To achieve this separation, the designer creates an abstract base class that maps a receiver (an object) with an action (a pointer to a member function). The base class contains an execute() method that simply calls the action on the receiver. This is exactly, what you need. In case you need to check only internal state of Person object it is enough. When you want to check Person object with external API that's OK to create implementation which binds external API in constructor with Person object in method. Simple example: import java.util.ArrayList; import java.util.List; public class DesignPatterns { public static void main(String[] args) { List checkers = new ArrayList<>(); checkers.add(person -> person != null); checkers.add(person -> person.getName() != null); checkers.add(person -> person.getName().length() > 0); checkers.add(new MacroViewConditionChecker(new MacroView())); checkers.add(new RestApiConditionChecker(new RestApi())); Person person = new Person(); person.setName("Name"); for (ConditionChecker checker : checkers) { System.out.println(checker.isInCondition(person)); } } } interface ConditionChecker { boolean isInCondition(Person person); } class MacroViewConditionChecker implements ConditionChecker { private final MacroView macroView; public MacroViewConditionChecker(MacroView macroView) { this.macroView = macroView; } @Override public boolean isInCondition(Person person) { return macroView != null; } } class MacroView { } class RestApiConditionChecker implements ConditionChecker { private final RestApi restApi; public RestApiConditionChecker(RestApi restApi) { this.restApi = restApi; } @Override public boolean isInCondition(Person person) { return restApi.checkName(person.getName()); } } class RestApi { public boolean checkName(String name) { System.out.println("Validate name ..."); System.out.println(name + " is valid"); return true; } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } You can use this pattern together with Chain of Responsibility. This approach does not bind Person object with any implementation. This bind is done in specific ConditionChecker implementation which can be easily exchanged.

Related questions

0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Which of the following is the correct way of implementing an interface salary by class manager? (a) class manager ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is the correct way of implementing an interface A by class B? (a) class B extends A{ ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What requires less resources? (a) Thread (b) Process (c) Thread and Process (d) Neither Thread nor ... portion Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: What are the differences ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: What are the differences ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Where does a new element be inserted in linked list implementation of a queue? (a) Head of list (b) ... Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which is the new method introduced in java 8 to iterate over a collection? (a) for (String i : StringList) ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following is used for implementing inheritance through class? (a) inherited (b) using (c) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What would be the result if a class extends two interfaces and both have a method with same name and ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
...