[07]Go设计模式:过滤器模式(FilterPattern)
过滤器模式
一、简介
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
二、代码
package main
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
)
// Request is the input of the filter
type Request interface{}
// Response is the output of the filter
type Response interface{}
// Filter interface is the definition of the data processing components
// Pipe-Filter structure
type Filter interface {
Process(data Request) (Response, error)
}
type SplitFilter struct {
delimiter string
}
func NewSplitFilter(delimiter string) *SplitFilter {
return &SplitFilter{delimiter}
}
func (sf *SplitFilter) Process(data Request) (Response, error) {
str, ok := data.(string) //检查数据格式/类型,是否可以处理
if !ok {
return nil, fmt.Errorf("input data must be string")
}
parts := strings.Split(str, sf.delimiter)
return parts, nil
}
type ToIntFilter struct {
}
func NewToIntFilter() *ToIntFilter {
return &ToIntFilter{}
}
func (tif *ToIntFilter) Process(data Request) (Response, error) {
parts, ok := data.([]string)
if !ok {
return nil, fmt.Errorf("input data should be []string")
}
ret := make([]int, 0)
for _, part := range parts {
s, err := strconv.Atoi(part)
if err != nil {
return nil, err
}
ret = append(ret, s)
}
return ret, nil
}
type SumFilter struct {
}
func NewSumFilter() *SumFilter {
return &SumFilter{}
}
func (sf *SumFilter) Process(data Request) (Response, error) {
elms, ok := data.([]int)
if !ok {
return nil, errors.New("input data should be []int")
}
ret := 0
for _, elem := range elms {
ret += elem
}
return ret, nil
}
type Pipeline struct {
Name string
Filters *[]Filter
}
func NewPipeline(name string, filters ...Filter) *Pipeline {
return &Pipeline{
Name: name,
Filters: &filters,
}
}
// call each filter's process function
func (p *Pipeline) Process(data Request) (Response, error) {
var ret interface{}
var err error
for _, filter := range *p.Filters {
ret, err = filter.Process(data)
if err != nil {
return ret, err
}
data = ret
}
return ret, err
}
func main() {
//例子中将字符串分割,再转成int,再求和
split := NewSplitFilter(",")
converter :=NewToIntFilter()
sum := NewSumFilter()
p :=NewPipeline("p1", split, converter, sum)
ret, err := p.Process("4,5,6")
if err != nil {
log.Fatal(err)
}
if ret != 15 {
log.Fatalf("The expected is 6, but the actual is %d", ret)
}
fmt.Println(ret)
}
完整代码:https://gitee.com/ncuzhangben/GoStudy/tree/master/go-design-pattern/07-Filter
三、参考链接
1、https://www.runoob.com/design-pattern/filter-pattern.html
[07]Go设计模式:过滤器模式(FilterPattern)的更多相关文章
- 设计模式のFilterPattern(过滤器模式)----结构模式
一.产生背景 我们有一堆“人”的对象,我们应该怎么选择出其中的男性.女性或者其他类型的呢?这时候我们可以用过滤器模式 二.通常做法 我们将创建一个 Person 对象.Criteria 接口和实现了该 ...
- 设计模式系列之过滤器模式(Chriteria Pattern)
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类 ...
- 设计模式之过滤器模式(php实现)
/** * github地址:https://github.com/ZQCard/design_pattern * 过滤器模式(Filter Pattern)或标准模式(Criteria Patter ...
- 【设计模式 - 7】之过滤器模式(Filter)
1 模式简介 过滤器模式(Filter)也叫标准模式(Criteria),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来. 2 实例 需求 ...
- 设计模式之过滤器模式——Java语言描述
过滤器模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来 实现 创建一个Person对象.Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列 ...
- Java设计模式应用——过滤器模式
storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中. 实际上,中间告警结果可能 ...
- 过滤器模式(Filter Pattern)
过滤器模式 一.什么是过滤器模式 过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类型的设计模式属于结构型 ...
- Java设计模-过滤器模式
过滤器模式 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接 ...
- 【转】Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式
[转]Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式 博客分类: 企业应用面临的问题 java并发编程 Struts2的线程安全ThreadLocal模式St ...
随机推荐
- dt系统中tag如何使用like与%来进行模糊查询
在destoon中,如果一个品牌的详细显示页,如果要显示与品牌相关的供应的话,可以通过查询标题中带有品牌关键字的这一条件来进行查询,但是经过测试发现不能正确解析, 然后查看文件的源文件,发现 {tag ...
- python写入csv
import xlwtimport csvnewfile=open("wu.csv","w",newline="")filewriter=c ...
- 爬虫 - 解析库之Beautiful Soup
了解Beautiful Soup 中文文档: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...
- [ARIA] Create an Accessible Tooltip on a Text Input
Here we use HTML and CSS to create a stylish yet semantic tooltip on a form input. I am using aria-d ...
- Otsu 类间方差法
又称最大类间方差法.是由日本学者大津(Nobuyuki Otsu)于1979年提出的[1],是一种自适合于双峰情况的自动求取阈值的方法.又叫大津法,简称Otsu. 算法提出初衷是是按图像的灰度特性 ...
- H5如何实现关闭当前页面,跳转到新页面?
小程序有此功能的跳转方法. 那么H5如何实现该功能? 很简单. location.replace('new.html') 这个方法可以实现 关闭当前页面,跳转到新页面 的效果. 而 windo ...
- learning java FileReader
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import ...
- dropbox icloud and nustore
dropbox icloud and nustore 这里只是写一下自己的感受. 曾经搜索无数遍, 想着用哪个比较好, 想来比较一下, 还不如自己直接用用看吧. 于是同时用了很久的 dropbx 和 ...
- 京东Java架构师讲解购物车的原理及Java实现
今天来写一下关于购物车的东西, 这里首先抛出四个问题: 1)用户没登陆用户名和密码,添加商品, 关闭浏览器再打开后 不登录用户名和密码问:购物车商品还在吗? 2)用户登陆了用户名密码,添加商品,关闭浏 ...
- NamedPipeStream的使用
NamedPipeStream的使用具体案例如下: using System; using System.Data; using System.Data.SQLite; using System.IO ...