This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BubbleSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Bubble Sort | |
| return numbers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class QuickSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Quick Sort | |
| return numbers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BinarySearchImpl { | |
| private SortAlgorithm sortAlgorithm; | |
| public BinarySearchImpl(SortAlgorithm sortAlgorithm) { | |
| super(); | |
| this.sortAlgorithm = sortAlgorithm; | |
| } | |
| public int binarySearch(int [] numbers,int numberToSearchFor) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootApplication | |
| public class SpringIn5StepsApplication { | |
| public static void main(String[] args) { | |
| BinarySearchImpl binarySearch=new BinarySearchImpl(new BubbleSortAlgorithm()); | |
| int result = binarySearch.binarySearch(new int[] {12,4,6},3); | |
| System.out.println(result); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component //告訴Spring "BinarySearchImpl"視為Component | |
| public class BinarySearchImpl { | |
| @Autowired //告訴Spring 自動導入一個"sortAlgorithm" | |
| private SortAlgorithm sortAlgorithm; | |
| public BinarySearchImpl(SortAlgorithm sortAlgorithm) { | |
| super(); | |
| this.sortAlgorithm = sortAlgorithm; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component //告訴spring "BubbleSortAlgorithm"視為Bean | |
| public class BubbleSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Bubble Sort | |
| return numbers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class QuickSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Quick Sort | |
| return numbers; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootApplication //Spring看到這個註解,會自動掃瞄所在之package有沒有@Component @Autowired等 | |
| public class SpringIn5StepsApplication { | |
| public static void main(String[] args) { | |
| ApplicationContext applicationContext = //Spring的管理器,管理所有的Bean | |
| SpringApplication.run(SpringIn5StepsApplication.class, args); | |
| // BinarySearchImpl binarySearch=new BinarySearchImpl(new QuickSortAlgorithm()); | |
| //↑改透過Spring的管理器取得"BinarySearchImpl"實例 | |
| //前面已透過@Component告訴Spring"BinarySearchImpl"為Bean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| logging.level.org.springframework = debug |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| public class QuickSortAlgorithm implements SortAlgorithm{ | |
| public int[] sort(int[] numbers) { | |
| // Logic for Quick Sort | |
| return numbers; | |
| } | |
| } |