If-Kette über String-Kundentypen
Ein zentraler Conditional Block enthält Auswahl und Algorithmen; ungültige Werte sind freie Strings.
Vollständiger Legacy-Modern-Vergleich aller Java-Klassen, Tests, Maven-POMs und relevanten Ressourcen dieses Moduls. Jede Datei ist genau einmal einer fachlich passenden Vergleichsgruppe zugeordnet.
Ein zentraler Conditional Block enthält Auswahl und Algorithmen; ungültige Werte sind freie Strings.
CustomerType ist typisiert, Strategien sind austauschbar und eine Factory übernimmt die Auswahl.
Die Ähnlichkeit ist eine technische Orientierung auf normalisiertem Quelltext. Sie ersetzt keine fachliche Bewertung.
| Vergleichsgruppe | Legacy-Dateien | Modern-Dateien | Legacy-Zeilen | Modern-Zeilen | Ähnlichkeit |
|---|---|---|---|---|---|
| Build-Konfiguration | 1 | 1 | 34 | 34 | 100% |
| Preislogik, Strategien und Test | 2 | 5 | 22 | 49 | 8% |
Das Pair-POM bindet die Legacy- und Modern-Submodule zusammen und gehört deshalb ebenfalls zur vollständigen Codeabdeckung.
Compiler- und Testkonfiguration wird verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| pom.xml | pom.xml | Direktvergleich Semantische Ähnlichkeit: 100% |
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>34patternsrefactoring-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>patterns-legacy</artifactId>
<packaging>jar</packaging>
<name>Patterns Legacy</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>34patternsrefactoring-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>patterns-modern</artifactId>
<packaging>jar</packaging>
<name>Patterns Modern</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Bedingungslogik wird in Strategy, Factory und Service mit fokussiertem Test zerlegt.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| LegacyPriceCalculator.java LegacyPriceCalculatorTest.java | CustomerType.java PricingStrategy.java PricingStrategyFactory.java PriceService.java PriceServiceTest.java | Direktvergleich Semantische Ähnlichkeit: 8% |
package at.aydin.lab.patterns.legacy;
// Design Pattern: Conditional Strategy (Legacy-Antipattern) – wählt Preisalgorithmen über Bedingungen.
public final class LegacyPriceCalculator {
public double price(String customerType, double base) {
if ("STANDARD".equals(customerType)) return base;
if ("PREMIUM".equals(customerType)) return base * 0.9;
if ("EMPLOYEE".equals(customerType)) return base * 0.7;
throw new IllegalArgumentException(customerType);
}
}package at.aydin.lab.patterns.legacy;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class LegacyPriceCalculatorTest {
@Test
void calculates() {
assertEquals(90, new LegacyPriceCalculator().price("PREMIUM", 100));
}
}package at.aydin.lab.patterns.modern;
public enum CustomerType {
STANDARD, PREMIUM, EMPLOYEE
}package at.aydin.lab.patterns.modern;
// Design Pattern: Strategy
// Zweck: Rabattalgorithmus ist austauschbar und ohne zentrale Switch-Anweisung erweiterbar.
@FunctionalInterface
public interface PricingStrategy {
double apply(double basePrice);
}package at.aydin.lab.patterns.modern;
import java.util.*;
// Design Pattern: Factory
// Zweck: Auswahl konkreter Strategies wird an einer Stelle konfiguriert.
public final class PricingStrategyFactory {
private final Map<CustomerType, PricingStrategy> strategies = Map.of(CustomerType.STANDARD, p -> p, CustomerType.PREMIUM,
p -> p * 0.9, CustomerType.EMPLOYEE, p -> p * 0.7);
public PricingStrategy forType(CustomerType type) {
return Optional.ofNullable(strategies.get(type)).orElseThrow();
}
}package at.aydin.lab.patterns.modern;
public final class PriceService {
private final PricingStrategyFactory factory;
public PriceService(PricingStrategyFactory factory) {
this.factory = factory;
}
public double price(CustomerType type, double base) {
return factory.forType(type).apply(base);
}
}package at.aydin.lab.patterns.modern;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PriceServiceTest {
@Test
void delegatesToStrategy() {
assertEquals(90, new PriceService(new PricingStrategyFactory()).price(CustomerType.PREMIUM, 100));
}
}