Understanding software behaviour parameterization II

 

Please read the part I on this article.

To provide a nice basic solution to the stated problem in part I, let’s define a family of algorithms that encapsulate each algorithm (called a strategy), and select an algorithm at run-time. In this case the family of algorithms is StudentPredicate and different strategies including StudentCgpaPredicate and StudentDeptPredicate.

public interface StudentPredicate{
boolean test (Student student);
}

 

public class StudentCgpaPredicate implments StudentPreictae
{
    public boolen test ( Student students )
    {
        return student.getCgpa();
    }

}

 

 

public class StudentDeptPredicate implements StudentPreictae
{
    public boolen test ( Student students )
    {
        return student.getDept();
    }

}

Now let’s use our filterStudent method to accept StudentPredicate objects to test a condition on an Student. This is what behavior software behaviour parameterization . This means: the ability to tell a method to take multiple behaviors (or strategies) as parameters and use them internally to accomplish different behaviors.

 

public List<Student> filterStudent( List<Student> studentList, 
StudentPredicate studentPred )
{
    List<Student> result = new ArrayList<>();
    For (Student student : studentList){
    if ( studentPred.test(student) ){
        result.add(student);
    }

}
    return result;
}

Now the code flexibility is improving. Suppose for example, if the a new requirement is to find all student in engineering and medical department with cgpa <=2.0 , all you need to do is create a class that implements the StudentPredicate accordingly. Your code is now flexible enough for any change of requirements involving the attributes of Student:

 

public class EngrAndMedStuPredicate implements StudentPredicate{
    public boolean test(Student student){
        return "engineering".equals(student.getDept()) && "medical".equals(student.getDept())
                && student.getVgpa() <= 2.0 ;
    }
}
    List<Student> studentList =
            filterStudent(inventory, new EngrAndMedStuPredicate());

 

Now the behavior of the filterStudent method depends on the code you pass to it via the StudentPredicate object. In other words, you’ve parameterized the behavior of the filterStudent method!

Look awesome ! Just starting . Look out for the part III of this article

Advertisements

One thought on “Understanding software behaviour parameterization II

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: