|
| 1 | +package com.luv2code.designpatterns.behavioral.strategy.v2_compare_with_method_references; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * Role: Client |
| 8 | + * |
| 9 | + * Demonstrates the Strategy pattern by sorting courses using different strategies. |
| 10 | + * Shows how sorting behavior can be changed by providing different strategy implementations. |
| 11 | + * Can even change sorting strategy at runtime. |
| 12 | + */ |
| 13 | +public class MainApp { |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + |
| 17 | + List<Course> courses = new ArrayList<>(); |
| 18 | + |
| 19 | + Course course1 = new Course("JavaScript for Beginners", 4.8, 9800); |
| 20 | + Course course2 = new Course("Advanced Game Development", 4.5, 5000); |
| 21 | + Course course3 = new Course("Building Scalable Cloud Solutions", 4.9, 5000); |
| 22 | + |
| 23 | + courses.add(course1); |
| 24 | + courses.add(course2); |
| 25 | + courses.add(course3); |
| 26 | + |
| 27 | + System.out.println("=== Before Sorting ==="); |
| 28 | + displayCourses(courses); |
| 29 | + |
| 30 | + System.out.println(); |
| 31 | + |
| 32 | + // Create sorter with initial strategy (default ascending order) |
| 33 | + CourseSorter courseSorter = new CourseSorter(new NameSortStrategy()); |
| 34 | + |
| 35 | + System.out.println("=== Sort by name (ascending - default) ==="); |
| 36 | + courseSorter.sort(courses); |
| 37 | + displayCourses(courses); |
| 38 | + |
| 39 | + System.out.println(); |
| 40 | + |
| 41 | + // Change strategy at runtime with explicit descending order |
| 42 | + System.out.println("=== Sort by name (descending) ==="); |
| 43 | + courseSorter.setSortStrategy(new NameSortStrategy(SortDirection.DESCENDING)); |
| 44 | + courseSorter.sort(courses); |
| 45 | + displayCourses(courses); |
| 46 | + |
| 47 | + System.out.println(); |
| 48 | + |
| 49 | + // Change to different sorting criteria with default ascending |
| 50 | + System.out.println("=== Sort by rating (ascending - default) ==="); |
| 51 | + courseSorter.setSortStrategy(new RatingSortStrategy()); |
| 52 | + courseSorter.sort(courses); |
| 53 | + displayCourses(courses); |
| 54 | + |
| 55 | + System.out.println(); |
| 56 | + |
| 57 | + // Change to descending |
| 58 | + System.out.println("=== Sort by rating (descending) ==="); |
| 59 | + courseSorter.setSortStrategy(new RatingSortStrategy(SortDirection.DESCENDING)); |
| 60 | + courseSorter.sort(courses); |
| 61 | + displayCourses(courses); |
| 62 | + |
| 63 | + System.out.println(); |
| 64 | + |
| 65 | + // Sort by student count ascending |
| 66 | + System.out.println("=== Sort by number of students (ascending - default) ==="); |
| 67 | + courseSorter.setSortStrategy(new StudentCountSortStrategy()); |
| 68 | + courseSorter.sort(courses); |
| 69 | + displayCourses(courses); |
| 70 | + |
| 71 | + System.out.println(); |
| 72 | + |
| 73 | + // Sort by student count descending |
| 74 | + System.out.println("=== Sort by number of students (descending) ==="); |
| 75 | + courseSorter.setSortStrategy(new StudentCountSortStrategy(SortDirection.DESCENDING)); |
| 76 | + courseSorter.sort(courses); |
| 77 | + displayCourses(courses); |
| 78 | + |
| 79 | + System.out.println(); |
| 80 | + |
| 81 | + // Sort by student count AND rating (ascending) |
| 82 | + System.out.println("=== Sort by number of students AND rating (ascending - default) ==="); |
| 83 | + courseSorter.setSortStrategy(new StudentCountAndRatingSortStrategy()); |
| 84 | + courseSorter.sort(courses); |
| 85 | + displayCourses(courses); |
| 86 | + |
| 87 | + System.out.println(); |
| 88 | + |
| 89 | + // Sort by student count AND rating (descending) |
| 90 | + System.out.println("=== Sort by number of students AND rating (descending) ==="); |
| 91 | + courseSorter.setSortStrategy(new StudentCountAndRatingSortStrategy(SortDirection.DESCENDING)); |
| 92 | + courseSorter.sort(courses); |
| 93 | + displayCourses(courses); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + private static void displayCourses(List<Course> courses) { |
| 98 | + |
| 99 | + for (Course course : courses) { |
| 100 | + System.out.println(course); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments