天天看点

过滤器java_过滤器模式

Python 代码:

# Filter Pattern with Python Code

from abc import abstractmethod,ABCMeta

# 创建一个类,在该类上应用标准

class Person():

_name = ""

_gender = ""

_maritalStatus = ""

def __init__(self,inName,inGender,inMaritalStatus):

self._name = inName

self._gender = inGender

self._maritalStatus = inMaritalStatus

def getName(self):

return self._name

def getGender(self):

return self._gender

def getMaritalStatus(self):

return self._maritalStatus

@staticmethod

def printPersons(inPersons):

for aPerson in inPersons:

print("Person : [ Name : {0}, Gender : {1}, Marital Status : {2}]".format(aPerson.getName(),aPerson.getGender(),aPerson.getMaritalStatus()))

# 为标准(Criteria)创建一个接口

class Criteria(metaclass=ABCMeta):

@abstractmethod

def meetCriteria(self,inPersons):

pass

# 创建实现了Criteria接口的实体类

class CriteriaMale(Criteria):

def meetCriteria(self,inPersons):

malePersons = []

for aPerson in inPersons:

if aPerson.getGender().upper() == "MALE":

malePersons.append(aPerson)

return malePersons

class CriteriaFemale(Criteria):

def meetCriteria(self,inPersons):

femalePersons = []

for aPerson in inPersons:

if aPerson.getGender().upper() == "FEMALE":

femalePersons.append(aPerson)

return femalePersons

class CriteriaSingle(Criteria):

def meetCriteria(self,inPersons):

singlePersons = []

for aPerson in inPersons:

if aPerson.getMaritalStatus().upper() == "SINGLE":

singlePersons.append(aPerson)

return singlePersons

class AndCriteria(Criteria):

_criteria = None

_otherCriteria = None

def __init__(self,inCriteria,inOtherCriteria):

self._criteria = inCriteria

self._otherCriteria = inOtherCriteria

def meetCriteria(self,inPersons):

# 获得第一次过滤结果

firstCriteriaPerson = self._criteria.meetCriteria(inPersons)

# 返回第二次过滤结果

return self._otherCriteria.meetCriteria(firstCriteriaPerson)

class OrCriteria(Criteria):

_criteria = None

_otherCriteria = None

def __init__(self, inCriteria, inOtherCriteria):

self._criteria = inCriteria

self._otherCriteria = inOtherCriteria

def meetCriteria(self,inPersons):

#先用Criteria分别构造数据

firstCriteriaItems = self._criteria.meetCriteria(inPersons)

otherCriteriaItems = self._otherCriteria.meetCriteria(inPersons)

# 完成或运算

for aPerson in otherCriteriaItems:

if aPerson not in firstCriteriaItems :

firstCriteriaItems.append(aPerson)

return firstCriteriaItems

# 调用输出

if __name__ == '__main__':

persons = []

persons.append(Person("Robert","Male","Single"))

persons.append(Person("John", "Male", "Married"))

persons.append(Person("Laura", "Female", "Married"))

persons.append(Person("Diana", "Female", "Single"))

persons.append(Person("Mike", "Male", "Single"))

persons.append(Person("Bobby", "Male", "Single"))

male = CriteriaMale() female = CriteriaFemale()

single = CriteriaSingle()

singleMale = AndCriteria(single, male)

singleOrFemale = OrCriteria(single,female)

print("Males: ")

Person.printPersons(male.meetCriteria(persons))

print("\nFemales: ")

Person.printPersons(female.meetCriteria(persons))

print("\nSingle: ")

Person.printPersons(single.meetCriteria(persons))

print("\nSingle Males: ")

Person.printPersons(singleMale.meetCriteria(persons))

print("\nSingle or Female:")

Person.printPersons(singleOrFemale.meetCriteria(persons))

Siskin.xu

Siskin.xu

sis***@sohu.com11个月前 (03-11)