"Filter Pattern" Design Pattern

What is filter pattern

The Filter pattern is a structural design pattern that allows you to process chains of objects using different criteria (filters) to connect requests to processes. Filter mode is useful when you need to filter objects by different filter conditions.

Why use the filter pattern

Using the filter mode can make us more flexible and convenient when operating on object collections. It can dynamically filter objects according to different filter conditions, and return the filtered object collection. In addition, the filter mode can also help us reduce the coupling of the code, making the code more concise and easy to maintain.

Where do you use it at work

In Android, filter mode is usually used to handle data filtering in list controls such as ListView and RecyclerView. For example, we can filter the data in the list through the keywords entered by the user, or filter the data according to some attributes of the data. In addition, there are some libraries in Android such as RxJava that also provide the implementation of the filter mode.

Design ideas

In the filter pattern, we define a Filter interface, which contains a filter () method, which receives a request and returns a filtered request.

We also need to define a FilterChain class that maintains a list of filters and provides a method addFilter() to add filters. When a request is passed to FilterChain, it will be processed by each filter's filter() method in turn.

Finally, we need to define a Request class that contains some properties that we need to filter. When used on the client side, we can create a Request object and pass it to FilterChain for processing.

Code implementation of the filter pattern

The following is an example of implementing a simple filter pattern:
First, we define the Filter interface:

public interface Filter {
    
    
    void filter(Request request, FilterChain filterChain);
}

Then, we define the FilterChain class:

public class FilterChain {
    
    
    private List<Filter> filters = new ArrayList<>();
    private int index = 0;

    public FilterChain addFilter(Filter filter) {
    
    
        filters.add(filter);
        return this;
    }

    public void doFilter(Request request) {
    
    
        if (index >= filters.size()) {
    
    
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.filter(request, this);
    }
}

Next, we define the Request class:

public class Request {
    
    
    private String keyword;
    private int price;

    public Request(String keyword, int price) {
    
    
        this.keyword = keyword;
        this.price = price;
    }

    public String getKeyword() {
    
    
        return keyword;
    }

    public int getPrice() {
    
    
        return price;
    }
}

Finally, we define two filters:

public class KeywordFilter implements Filter {
    
    
    private String keyword;

    public KeywordFilter(String keyword) {
    
    
        this.keyword = keyword;
    }

    @Override
    public void filter(Request request, FilterChain filterChain) {
    
    
        if (request.getKeyword().contains(keyword)) {
    
    
            filterChain.doFilter(request);
        }
    }
}

public class PriceFilter implements Filter {
    
    
    private int maxPrice;

    public PriceFilter(int maxPrice) {
    
    
        this.maxPrice = maxPrice;
    }

    @Override
    public void filter(Request request, FilterChain filterChain) {
    
    
        if (request.getPrice() <= maxPrice) {
    
    
            filterChain.doFilter(request);
        }
    }
}

The client uses the filter pattern as follows:

public static void main(String[] args) {
    
    
    Request request = new Request("phone", 1000);
    FilterChain filterChain = new FilterChain();
    filterChain.addFilter(new KeywordFilter("phone"))
            .addFilter(new PriceFilter(900));
    filterChain.doFilter(request);
}

In the above code, we first created a Request object and defined two filters: KeywordFilter and PriceFilter. Then, add these two filters to the FilterChain, and call the doFilter() method to filter the request.

Summarize

The filter pattern is a simple and practical design pattern that can help us dynamically filter object collections and reduce code coupling. In Android development, the filter mode is usually used to process the filtering of list data, which is very practical.

おすすめ

転載: blog.csdn.net/weixin_45112340/article/details/129811510