Birleştirme
- Java kursu
- İleri seviye
- Ders 77/82
- 6 dk
Tüm katmanları birleştirerek çalışan bir mini servis yazarsın.
Birleştirme
Son derste tüm katmanları birleştirirsin. main içinde şu akışı calistiracaksin:
ledgerüzerine stream filtreleme uygula ve filtrelenen kayıt sayısını yazdır.ExecutorServiceile toplamı hesapla ve yazdır.LedgerServiceTestsınıfınıntestGetCountyöntemini çağır.- Son olarak
service.getCount()sonucunu yazdır.
Tüm bu adımlar deterministik olduğundan çıktı her seferinde aynıdır. Bu, ileri düzey bir uygulamanın nasıl katman katman kurulduğunu gösterir.
Uygulama Tüm akışı birleştir
Önceki dersin çözümünü kullan. main içinde şunları ekle: service.ledger.stream().filter(t -> t.amount > 100).count() ile filtrelenen kayıt sayısını bul ve yazdır. ExecutorService ile tüm tutamlari topla, pool.submit(task).get() ile al ve yazdır. LedgerServiceTest örneği oluşturup testGetCount() çağır. Son olarak service.getCount() sonucunu yazdır.
Başlangıç kodu
import java.util.ArrayList;
import java.util.concurrent.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class Transaction {
String category;
int amount;
Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}
}
class LedgerService {
ArrayList<Transaction> ledger = new ArrayList<>();
void add(Transaction t) { ledger.add(t); }
int getCount() { return ledger.size(); }
}
class LedgerServiceTest {
@Test
void testGetCount() {
LedgerService s = new LedgerService();
s.add(new Transaction("market", 120));
assertEquals(1, s.getCount());
}
}
public class Main {
public static void main(String[] args) throws Exception {
LedgerService service = new LedgerService();
service.add(new Transaction("market", 120));
service.add(new Transaction("fuel", 80));
// Tum akışı birlestir: filtrele, concurrent topla, test et, yazdır
}
}Örnek çözüm
import java.util.ArrayList;
import java.util.concurrent.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class Transaction {
String category;
int amount;
Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}
}
class LedgerService {
ArrayList<Transaction> ledger = new ArrayList<>();
void add(Transaction t) { ledger.add(t); }
int getCount() { return ledger.size(); }
}
class LedgerServiceTest {
@Test
void testGetCount() {
LedgerService s = new LedgerService();
s.add(new Transaction("market", 120));
assertEquals(1, s.getCount());
}
}
public class Main {
public static void main(String[] args) throws Exception {
LedgerService service = new LedgerService();
service.add(new Transaction("market", 120));
service.add(new Transaction("fuel", 80));
long filteredCount = service.ledger.stream()
.filter(t -> t.amount > 100)
.count();
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> {
int sum = 0;
for (Transaction t : service.ledger) sum += t.amount;
return sum;
};
int total = pool.submit(task).get();
pool.shutdown();
LedgerServiceTest test = new LedgerServiceTest();
test.testGetCount();
System.out.println(filteredCount);
System.out.println(total);
System.out.println(service.getCount());
}
}Beklenen çıktı
1 200 2
Uygulama Özet: mini entegrasyon
Yeni bir Summary sınıfı tanımla. İçerisinde void run() yöntemi olsun; bu yöntem iki kayıtlı bir LedgerService oluştursun, getCount() ve stream ile filter(t -> t.amount > 100).count() sonuçlarını ayrı satırlarda yazdırsın. main içinde new Summary().run() çağır.
Başlangıç kodu
import java.util.ArrayList;
class Transaction {
String category;
int amount;
Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}
}
class LedgerService {
ArrayList<Transaction> ledger = new ArrayList<>();
void add(Transaction t) { ledger.add(t); }
int getCount() { return ledger.size(); }
}
// Summary sınıfını tanımla
public class Main {
public static void main(String[] args) {
// Summary örneğini çalıştır
}
}Örnek çözüm
import java.util.ArrayList;
class Transaction {
String category;
int amount;
Transaction(String category, int amount) {
this.category = category;
this.amount = amount;
}
}
class LedgerService {
ArrayList<Transaction> ledger = new ArrayList<>();
void add(Transaction t) { ledger.add(t); }
int getCount() { return ledger.size(); }
}
class Summary {
void run() {
LedgerService s = new LedgerService();
s.add(new Transaction("market", 120));
s.add(new Transaction("fuel", 80));
System.out.println(s.getCount());
long big = s.ledger.stream().filter(t -> t.amount > 100).count();
System.out.println(big);
}
}
public class Main {
public static void main(String[] args) {
new Summary().run();
}
}Beklenen çıktı
2 1
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 Birleştirme dersinde hangi katmanlar birlikte çalışır?
- Stream, Concurrent ve Test Doğru cevap
- Yalnızca Concurrent
- Yalnızca Stream
- Yalnızca Test
Açıklama: Stream, Concurrent ve Test katmanları aynı main içinde çalışır.
2 Future.get çağrısını tamamla
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> { /* toplam */ return 200; };
int total = pool.submit(task).____();
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> { /* toplam */ return 200; };
int total = pool.submit(task).get();
Açıklama: get() sonucu hazır olana dek bekler ve döndürür.
3 Proje özetini eşleştir
| Kavram | Anlamı |
|---|---|
Transaction |
Model sınıfı |
Stream filter |
100 üstü kayıtlar |
ExecutorService |
Concurrent toplam |
LedgerServiceTest |
Birim test |