-1

I have assignment in java ,I need help please. I tried to solve it but I have some problem that can't understand them.

Assignment is: In this exercise, use the Template method pattern to define an abstract class Filter with a public method filter (the template method) that calls the method accept (the hook method) that can be implemented in different ways in the different concrete classes. Write a test program by extending the class Filter and defining accept so that only strings of at most three characters are accepted.

public abstract class Filter<T> { public abstract T[] filter(T[] list); public abstract boolean accept(T val); } public class FilterTest<T> extends Filter<T> { private int capacity = 0; public FilterTest(int cap) { this.capacity = cap; } @Override public T[] filter(T[] list1) { @SuppressWarnings("unchecked") T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity); int counter = 0; for (T t : list1) { if (accept(t)) { finalList[counter] = t; counter++; } } return finalList; } public void printArray(T[] list2) { for (int i = 0; i < list2.length; i++) { if (list2[i] != null) { System.out.print(list2[i] + " "); } } System.out.println(); } @Override public boolean accept(T val) { return String.valueOf(val).length() > 0 && String.valueOf(val).length() <= 3; } public static void main(String[] args) { FilterTest<String> filterTest = new FilterTest<>(8); String[] lists = { "Hi", "here", "is", "the", "AOOP", "course", "at", "University" }; System.out.print("My original list is: "); filterTest.printArray(lists); System.out.print(" The filtered list is: "); String[] filteredList = filterTest.filter(lists); filterTest.printArray(filteredList); } } 

Here is comment from my teacher: "not correct, only the accept method should be abstract in the Filter class, the filter method should be already implemented in the Filter class and not be abstract all implementation will be the same, only the accept method changes for different filters)".

I don't understand what should I do now, how the code will be correct. help please, Thanks

2
  • Look at the Predicate interface, it is very similar to your assignment. Commented Oct 31, 2019 at 16:51
  • My teacher want abstract not interface, have you idea please Commented Oct 31, 2019 at 17:10

1 Answer 1

0

I assume that Filter should look something like this

public abstract class Filter<T> { public T[] filter(T[] list1) { @SuppressWarnings("unchecked") T[] finalList = (T[]) Array.newInstance(list1.getClass().getComponentType(), capacity); int counter = 0; for (T t : list1) { if (accept(t)) { finalList[counter] = t; counter++; } } return finalList; } public abstract boolean accept(T val); } 

You can even declare Filter<T> as an interface and have a default implementation for filter. Have a look here

Sign up to request clarification or add additional context in comments.

3 Comments

You don't change any thing, can you explain what you have done please
Please have a look more accurately, the method definition of filter is moved to class Filter instead of being in the implementation. That's the point of template method design pattern.
Oh, Thanks, What about capacity variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.