简体   繁体   中英

Java how to implement this interface?

I am working on some test exam questions that don't contain the answers and i have been stuck for a while. I have this interface (Stringcombiner.java)

package section3_apis.part1_interfaces;

public interface StringCombiner {
    String combine(String first, String second);
}

and this factory (CombinerFactory.java)

package section3_apis.part1_interfaces;

public class CombinerFactory{
    /**
     * This method serves a StringCombiner that will surround the given arguments with double quotes,
     * separated by spaces and the result surrounded by single quotes.
     *
     * For example, the call
     *      combiner.combine("one", "two")
     * will return '"one" "two"'
     * @return quotedCombiner
     */
    static StringCombiner getQuotedCombiner() {
        //YOUR CODE HERE (and remove the throw statement)

        throw new UnsupportedOperationException("Not implemented yet");
    }

I have been fiddling around with it for quite some time, but I am unable to solve it. what I have tried so far: I tried to make CombinerFactory implement the interface, which then adds an override, but I don't understand how I then use the string combine in the getQuotedCombiner. I also tried to make a new instance of Stringcombiner in the getQuotedCombiner, but I'm pretty sure that's not what I'm supposed to do. When I try one of these ways, It asks me for values to put in combine, but the endgoal is that the Junit tests are used. I assume I need to put in place some kind of placeholders or a main class that implements the method but still leaves the method open to be used from the outside (by the tests) I'm kinda spitballing here, just trying to get my thoughts on paper about what I think I should be doing.

I would appreciate some guidance in the right direction on how to tackle this problem.

Assuming you only can put code inside the getQuotedCombiner method, you need to return an anonymous class that implements the StringCombiner interface. For example:

static StringCombiner getQuotedCombiner() {
    return new StringCombiner() {
        public String combine(String first, String second) {
            return "'\"" + first + "\" \"" + second + "\"'";
        }
    };
}

With Java 8 you can simplify it using a lambda expression:

static StringCombiner getQuotedCombiner() {
    return (first, second) -> "'\"" + first + "\" \"" + second + "\"'";
}

In case the exercise lets you create other classes, you could add a new class, for example QuotedStringCombiner that implements the interface:

public class QuotedStringCombiner implements StringCombiner {
    
    @Override
    public String combine(String first, String second) {
        return "'\"" + first + "\" \"" + second + "\"'";
    }
}

And on the getQuotedCombiner method of CombinerFactory you could return a new instance of this class:

static StringCombiner getQuotedCombiner() {
    return new QuotedStringCombiner();
}

Or, implement the Singleton pattern, to avoid creating an instance every time a quoted combiner is requested:

private static final QuotedStringCombiner QUOTED_COMBINER_INSTANCE = new QuotedStringCombiner();

static StringCombiner getQuotedCombiner() {
    return QUOTED_COMBINER_INSTANCE;
}
public class StringCombinerImpl implements StringCombiner {
    public String combine(String first, String second) {
        throw new UnsupportedOperationException("Not implemented yet");
    }
}

just change the throw statement with the code needed to do what the method is expected to do.

To use it, add the instance creation to getQuotedCombiner :

static StringCombiner getQuotedCombiner() {
    return new StringCombinerImpl();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM