2018年4月2日 星期一

函式傳遞的進化論 (2) - function object

上一篇中講到利用函式指標與callback函式可以讓我們的程式更有彈性。
不過在一些物件導向的程式語言(例如Java)中,我們沒辦法直接把函式傳遞給另一個函式

註:在物件導向的程式語言中,函式(function)被稱做方法(method),之後我會使用方法一詞

在Java中萬物都是物件(唔,其實還有primitive type這東西的存在,不過我們先忽略它),方法沒有辦法單獨宣告與使用,它必須依附在某個類別下。

所以一個通用的解法是用一個interface將方法包起來
  1. //Comparator.java
  2. public interface Comparator{
  3. int compare(int first, int second);
  4. }

此時我們的bubbleSort可以將Comparator物件傳遞進來,並且在需要時呼叫Comparator的compare方法(第10行)
  1. //Sorter.java
  2. public class Sorter{
  3. public static void main(String []args){
  4. ...
  5. }
  6. public void bubbleSort(int[] array, Comparator c){
  7. for(int round=0; round<array.length; round++){
  8. for(int i=0; i<array.length-1; i++){
  9. if( c.compare(array[i],array[i+1]) > 0){
  10. int temp = array[i];
  11. array[i] = array[i+1];
  12. array[i+1] = temp;
  13. }
  14. }
  15. }
  16. }
  17. }

同樣當我們需要不同排序邏輯時,只需要實做Comparator介面就好
  1. //SortByAsc.java
  2. public class SortByAsc implements Comparator{
  3. public int compare(int first, int second){
  4. if(first < second){
  5. return -1;
  6. }
  7. else if(first > second){
  8. return 1;
  9. }
  10. else{
  11. return 0;
  12. }
  13. }
  14. }

使用時只要new一個有實做Comparator介面的物件即可。
  1. //Sorter.java
  2. public class Sorter{
  3. public static void main(String []args){
  4. int[] array = {5,2,8,1};
  5. Sorter obj = new Sorter();
  6. obj.bubbleSort(array, new SortByAsc());
  7. for(int elem : array){
  8. System.out.println(elem);
  9. }
  10. }
  11. public void bubbleSort(int[] array, Comparator c){
  12. ...
  13. }
  14. }

這裡的SortByAsc就是所謂的function object,它僅僅是方法(函式)的物件而已。

沒有留言:

張貼留言