2017-12-08 1 views
0

void sort() 메소드를 사용하여 정렬 클래스 (삽입, 병합 등)를 많이 작성하려면이 클래스가 구현하거나 확장 할 수있는 인터페이스 또는 추상 클래스가 필요합니다. 에서.자바 디자인 strucutre

그러나 정적 메서드를 정적으로 사용하려는 경우 추상 메서드로 정적 메서드를 유지할 수 없습니다.

그런 구현을위한 적절한 방법은 무엇입니까?

+2

양방향 (반복자에 조금 유사) 될 것이라고이 될 것이다! – ajb

+4

당신은 두 가지 모순 된 욕망을 가지고 있습니다. 정적 및 다형성. 그것은 하나 또는 다른 것입니다. –

+1

OK, 더 심각하게 : 당신은'분류기'객체가 필요합니다. 이 객체는 정렬중인 배열이 아니라 정렬 알고리즘을 제공하는 것이 목적 인 객체입니다. sort 메소드는 해당 객체의 인스턴스 메소드입니다. – ajb

답변

0

질문을 올바르게 이해 한 경우 static 메서드 sort(int data [ ])을 사용하는 솔루션은 Stategy 패턴을 사용하여 수행 할 수 있습니다 (int 데이터를 사용하여 간편하게 유지).
enter image description here 당신이 말했듯이, 그룹 인터페이스 SortAlgorithm 예, InsertionSort, MergeSort, Quicksort 등을위한 등 모든 종류의 전략은 ... 추상 메소드 sort(int data [])를 구현합니다.
그런 다음 내 예 choice 클라이언트

class Client{ 
    public static void main(String [ ] args){ 
     Scanner sc = new Scanner(System.in); 
     int choice = sc.nextInt(); 
     int data= new int [5] 
     data={3,7,7,1,0}; 
     Sorter.sort(data,choice); 
    } 
0

글쎄, 난 당신이 그것을 복잡하게 만들려고 생각에 의해 주어진 정수입니다 final sort(int data [ ], int choice)를 사용하는 추상 형식 SortAlgorithm에 대한 참조를 소유하는 클래스 Sorter 물품. 귀하의 문제는 간단합니다. 몇 가지 정렬 기능 만 코딩하면됩니다. 어디서나 객체 지향 사고를 적용 할 수는 없습니다. 이것은 다른 재사용 가능한 알고리즘을 구현 한 것입니다. 당신이 정말로 이것을 디자인 할 필요가 있다면. 이 방법을 시도 할 수 있습니다 :

interface Sorter<T> { 

    // This method accepts any type of object and sorts it and return it in sorted order 
    //Used Generics to support all types. You can read T as List or Array if you want to understand it in a simple way 
    public T sort(T t); 
} 



class MergeSort implements Sorter<int[]> { 


    @Override 
    public int[] sort(int[] numbersToSort) { 
    //algorithm goes here 

    } 

} 


class BubbleSort implements Sorter<int[]> { 

    @Override 
    public int[] sort(int[] numbersToSort) { 
     //algorithm goes here 
    } 

} 


class InsertionSort implements Sorter<int[]> { 

    @Override 
    public int[] sort(int[] numbersToSort) { 
     //algorithm goes here 
    } 

} 





enum SortingAlgorithms{ 
INSERTIONSORT, 
BUBBLESORT, 
MERGESORT; 

} 

class SorterFactory { 
    public static Sorter<int[]> getSortingAlgorithm(SortingAlgorithms alg) { 
     switch(alg) { 
     case INSERTIONSORT : 
      return new InsertionSort(); 

     case BUBBLESORT : 
      return new BubbleSort(); 

     case MERGESORT : 
      return new MergeSort(); 

     default: 
      return new BubbleSort(); 
     } 
    } 
} 



public class SortingExecutor { 
    public static void main (String... cmdArgs) { 

     int[] toBeSorted = {6, 7, 1, 0, 3}; 

     //get a bubble sort algorith which can take int[] as input and return the sorted int[] as output 
     Sorter<int[]> bubbleSort = SorterFactory.getSortingAlgorithm(SortingAlgorithms.BUBBLESORT); 
     bubbleSort.sort(toBeSorted); 



    } 
} 
0

다리 디자인 패턴은 여기에 당신을 도울 것입니다. 하나의 계층 구조에는 분류 자 ​​참조 기본 집계 클래스로 데이터를 보유 할 다른 집계가 포함됩니다. 분류자인을 기본 클래스로 사용하는 다른 계층 구조는 구체적인 클래스에서 다양한 종류의 정렬 알고리즘을 제공합니다.

장점은 양 측면이 독립적으로 성장할 수 있다는 점입니다. 브리지 패턴에서

만 변화는 집계-분류기 관계는 정적하지 마십시오