1. Trang chủ
  2. » Giáo án - Bài giảng

Predicate+_+Closure.ppt

13 115 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 13
Dung lượng 105,5 KB

Nội dung

Predicate and Closure Predicate and Closure Functors Functors  Functors: a functional object (object that encapsulates a Functors: a functional object (object that encapsulates a function) = method pointer in C function) = method pointer in C  A functor is either Predicate, Closure or Transformer A functor is either Predicate, Closure or Transformer  Predicate: evaluates objects and return a boolean Predicate: evaluates objects and return a boolean  Transformer: evaluate objects and return new objects Transformer: evaluate objects and return new objects  Closure: accept objects and execute code Closure: accept objects and execute code Example Example  Student objects Student objects  A -> honorRollStudents list A -> honorRollStudents list  D or F -> problemStudents list D or F -> problemStudents list  Iterate each list: Iterate each list:  honorRollStudents -> award honorRollStudents -> award  problemStudents -> schedule a meeting with parents problemStudents -> schedule a meeting with parents Old style Old style  List allStudents = getAllStudents(); List allStudents = getAllStudents();  // Create 2 ArrayLists to hold honorRoll students // Create 2 ArrayLists to hold honorRoll students  // and problem students // and problem students  List honorRollStudents = new ArrayList(); List honorRollStudents = new ArrayList();  List problemStudents = new ArrayList(); List problemStudents = new ArrayList(); Iterator allStudentsIter = allStudents.iterator(); Iterator allStudentsIter = allStudents.iterator(); while( allStudentsIter.hasNext() ) { while( allStudentsIter.hasNext() ) { Student s = (Student) allStudentsIter.next(); Student s = (Student) allStudentsIter.next(); if( s.getGrade().equals( "A" ) ) { if( s.getGrade().equals( "A" ) ) { honorRollStudents.add( s ); honorRollStudents.add( s ); } else if( s.getGrade().equals( "B" ) && } else if( s.getGrade().equals( "B" ) && s.getAttendance() == PERFECT) { s.getAttendance() == PERFECT) { honorRollStudents.add( s ); honorRollStudents.add( s ); } else if( s.getGrade().equals( "D" ) || } else if( s.getGrade().equals( "D" ) || s.getGrade().equals( "F" ) ) { s.getGrade().equals( "F" ) ) { problemStudents.add( s ); problemStudents.add( s ); } else if( s.getStatus() == SUSPENDED ) { } else if( s.getStatus() == SUSPENDED ) { problemStudents.add( s ); problemStudents.add( s ); } } } } // For all honorRoll students, add an award and // For all honorRoll students, add an award and // save to the Database. // save to the Database. Iterator honorRollIter = Iterator honorRollIter = honorRollStudents.iterator(); honorRollStudents.iterator(); while( honorRollIter.hasNext() ) { while( honorRollIter.hasNext() ) { Student s = (Student) honorRollIter.next(); Student s = (Student) honorRollIter.next(); // Add an award to student record // Add an award to student record s.addAward( "honor roll", 2005 ); s.addAward( "honor roll", 2005 ); Database.saveStudent( s ); Database.saveStudent( s ); } } // For all problem students, add a note and // For all problem students, add a note and // save to the database. // save to the database. Iterator problemIter = problemStudents.iterator(); Iterator problemIter = problemStudents.iterator(); while( problemIter.hasNext() ) { while( problemIter.hasNext() ) { Student s = (Student) problemIter.next(); Student s = (Student) problemIter.next(); // Flag student for special attention // Flag student for special attention s.addNote( "talk to student", 2005 ); s.addNote( "talk to student", 2005 ); s.addNote( "meeting with parents", 2005 ); s.addNote( "meeting with parents", 2005 ); Database.saveStudent( s ); Database.saveStudent( s ); } } New way New way  Previous solutions: procedural Previous solutions: procedural  How about changing criteria: adding more clauses ( a How about changing criteria: adding more clauses ( a problem student is classified as having one B, perfect problem student is classified as having one B, perfect attendance but attended detention more than five attendance but attended detention more than five times) times)  Decoupling of the criteria from code (criteria -> Decoupling of the criteria from code (criteria -> predicate, action -> closure) predicate, action -> closure) First Predicate – Honor student criteria First Predicate – Honor student criteria // Anonymous Predicate that decides if a student // Anonymous Predicate that decides if a student // has made the honor roll. // has made the honor roll. Predicate isHonorRoll = new Predicate() { Predicate isHonorRoll = new Predicate() { public boolean evaluate(Object object) { public boolean evaluate(Object object) { Student s = (Student) object; Student s = (Student) object; return( ( s.getGrade().equals( "A" ) ) || return( ( s.getGrade().equals( "A" ) ) || ( s.getGrade().equals( "B" ) && ( s.getGrade().equals( "B" ) && s.getAttendance() == PERFECT ) ); s.getAttendance() == PERFECT ) ); } } }; }; Second Predicate – Problem student criteria Second Predicate – Problem student criteria // Anonymous Predicate that decides if a student // Anonymous Predicate that decides if a student // has a problem. // has a problem. Predicate isProblem = new Predicate() { Predicate isProblem = new Predicate() { public boolean evaluate(Object object) { public boolean evaluate(Object object) { Student s = (Student) object; Student s = (Student) object; return ( ( s.getGrade().equals( "D" ) || return ( ( s.getGrade().equals( "D" ) || s.getGrade().equals( "F" ) ) || s.getGrade().equals( "F" ) ) || s.getStatus() == SUSPENDED ); s.getStatus() == SUSPENDED ); } } }; };

Ngày đăng: 16/07/2014, 04:00

w