1+ package zad03 ;
2+
3+ /**
4+ * Główna klasa programu - pełni rolę "Composition Root" we wzorcu Dependency Injection.
5+ *
6+ * Composition Root to miejsce, gdzie:
7+ * - Tworzone są wszystkie zależności (obiekty)
8+ * - Konfigurowane są powiązania między komponentami
9+ * - Wstrzykiwane są zależności do obiektów, które ich potrzebują
10+ *
11+ * W tej implementacji Main:
12+ * 1. Analizuje argumenty wiersza poleceń
13+ * 2. Tworzy odpowiednią implementację TrybPracy
14+ * 3. Wstrzykuje ją do klasy Aplikacja
15+ * 4. Uruchamia aplikację
16+ *
17+ * Dzięki temu podejściu:
18+ * - Klasa Aplikacja nie wie, jaki konkretny tryb będzie używany
19+ * - Łatwo można dodać nowe tryby (np. REST API) bez modyfikacji Aplikacji
20+ * - Kod jest łatwy do testowania (można wstrzyknąć mock'i)
21+ */
22+ public class Main {
23+
24+ public static void main (String [] args ) {
25+ try {
26+ // Analiza argumentów i utworzenie odpowiedniego trybu pracy
27+ TrybPracy trybPracy = utworzTrybPracy (args );
28+
29+ if (trybPracy == null ) {
30+ // Wyświetlono pomoc lub wystąpił błąd - kończymy
31+ return ;
32+ }
33+
34+ // === COMPOSITION ROOT ===
35+ // Tutaj tworzymy zależności i wstrzykujemy je do aplikacji
36+ Aplikacja aplikacja = new Aplikacja (trybPracy );
37+
38+ // Uruchomienie aplikacji
39+ aplikacja .uruchom ();
40+
41+ } catch (Exception e ) {
42+ System .err .println ("Wystąpił nieoczekiwany błąd: " + e .getMessage ());
43+ e .printStackTrace ();
44+ System .exit (1 );
45+ }
46+ }
47+
48+ /**
49+ * Tworzy odpowiednią implementację TrybPracy na podstawie argumentów wiersza poleceń.
50+ *
51+ * Ta metoda jest częścią Composition Root - decyduje, która implementacja
52+ * interfejsu TrybPracy zostanie utworzona i wstrzyknięta do aplikacji.
53+ *
54+ * @param args argumenty wiersza poleceń
55+ * @return implementacja TrybPracy lub null jeśli wyświetlono pomoc
56+ */
57+ private static TrybPracy utworzTrybPracy (String [] args ) {
58+ // Brak argumentów - tryb konsolowy
59+ if (args .length == 0 ) {
60+ System .out .println ("Uruchamianie w trybie konsolowym..." );
61+ System .out .println ();
62+ return new TrybKonsolowy ();
63+ }
64+
65+ // Jeden argument
66+ if (args .length == 1 ) {
67+ String argument = args [0 ];
68+
69+ // Pomoc
70+ if (argument .equals ("--help" ) || argument .equals ("-h" )) {
71+ wyswietlPomoc ();
72+ return null ;
73+ }
74+
75+ // Tryb REST API (demonstracyjny)
76+ if (argument .equals ("--rest-demo" )) {
77+ System .out .println ("Uruchamianie w trybie REST API (demo)..." );
78+ System .out .println ();
79+ return new TrybRestApi ("https://api.example.com/polecenia" );
80+ }
81+
82+ // Tryb skryptowy - argument to nazwa pliku
83+ System .out .println ("Uruchamianie w trybie skryptowym..." );
84+ System .out .println ();
85+
86+ TrybSkryptowy trybSkryptowy = new TrybSkryptowy (argument );
87+
88+ // Sprawdzenie czy plik jest dostępny
89+ if (!trybSkryptowy .czyPlikDostepny ()) {
90+ System .err .println ("Błąd: Plik '" + argument + "' nie istnieje lub nie można go odczytać." );
91+ System .err .println ("Sprawdź czy plik istnieje w bieżącym katalogu i czy masz uprawnienia do jego odczytu." );
92+ System .err .println ();
93+ System .err .println ("Przykład użycia:" );
94+ System .err .println (" java zad03.Main test_polecenia.txt" );
95+ System .exit (1 );
96+ }
97+
98+ return trybSkryptowy ;
99+ }
100+
101+ // Dwa argumenty - tryb REST API z własnym URL
102+ if (args .length == 2 && args [0 ].equals ("--rest" )) {
103+ String endpointUrl = args [1 ];
104+ System .out .println ("Uruchamianie w trybie REST API..." );
105+ System .out .println ();
106+ return new TrybRestApi (endpointUrl );
107+ }
108+
109+ // Za dużo argumentów lub nieznane argumenty
110+ System .err .println ("Błąd: Nieprawidłowe argumenty." );
111+ System .err .println ("Użyj --help aby zobaczyć dostępne opcje." );
112+ System .exit (1 );
113+ return null ;
114+ }
115+
116+ /**
117+ * Wyświetla pomoc dotyczącą użytkowania programu.
118+ */
119+ private static void wyswietlPomoc () {
120+ System .out .println ("=" .repeat (70 ));
121+ System .out .println (" PROGRAM OBSŁUGI POLECEŃ - DEPENDENCY INJECTION" );
122+ System .out .println ("=" .repeat (70 ));
123+ System .out .println ();
124+ System .out .println ("UŻYTKOWANIE:" );
125+ System .out .println (" java zad03.Main - tryb konsolowy" );
126+ System .out .println (" java zad03.Main <plik> - tryb skryptowy" );
127+ System .out .println (" java zad03.Main --rest-demo - tryb REST API (demo)" );
128+ System .out .println (" java zad03.Main --rest <url> - tryb REST API z własnym URL" );
129+ System .out .println (" java zad03.Main --help - wyświetlenie tej pomocy" );
130+ System .out .println ();
131+ System .out .println ("WZORZEC DEPENDENCY INJECTION:" );
132+ System .out .println ();
133+ System .out .println (" Ten program wykorzystuje wzorzec Dependency Injection (DI):" );
134+ System .out .println (" - Main.java pełni rolę 'Composition Root'" );
135+ System .out .println (" - Klasa Aplikacja otrzymuje TrybPracy przez konstruktor" );
136+ System .out .println (" - Aplikacja nie wie, jaki konkretny tryb będzie używany" );
137+ System .out .println ();
138+ System .out .println (" Zalety DI:" );
139+ System .out .println (" - Luźne powiązanie między komponentami" );
140+ System .out .println (" - Łatwe testowanie (można wstrzyknąć mock'i)" );
141+ System .out .println (" - Łatwe rozszerzanie o nowe tryby pracy" );
142+ System .out .println ();
143+ System .out .println ("TRYBY PRACY:" );
144+ System .out .println ();
145+ System .out .println ("1. TRYB KONSOLOWY (brak argumentów)" );
146+ System .out .println (" - Polecenia wprowadzane interaktywnie przez użytkownika" );
147+ System .out .println (" - Przykład: java zad03.Main" );
148+ System .out .println ();
149+ System .out .println ("2. TRYB SKRYPTOWY (jeden argument - nazwa pliku)" );
150+ System .out .println (" - Polecenia wczytywane z podanego pliku" );
151+ System .out .println (" - Przykład: java zad03.Main test_polecenia.txt" );
152+ System .out .println ();
153+ System .out .println ("3. TRYB REST API (--rest-demo lub --rest <url>)" );
154+ System .out .println (" - Stub przygotowany na przyszłe rozszerzenie" );
155+ System .out .println (" - Pokazuje gotowość architektury na nowe źródła danych" );
156+ System .out .println (" - Przykład: java zad03.Main --rest-demo" );
157+ System .out .println (" - Przykład: java zad03.Main --rest https://api.example.com/polecenia" );
158+ System .out .println ();
159+ System .out .println ("DOSTĘPNE POLECENIA:" );
160+ System .out .println (" pomoc - wyświetla nazwiska implementatorów" );
161+ System .out .println (" grupa - wyświetla grupę dziekańską" );
162+ System .out .println (" czesc <Nazwisko> - wyświetla powitanie z nazwiskiem" );
163+ System .out .println (" exit/quit/wyjscie - kończy program" );
164+ System .out .println ();
165+ }
166+ }
0 commit comments