Skip to content

Instantly share code, notes, and snippets.

@Frdnspnzr
Created July 9, 2013 16:33
Show Gist options
  • Save Frdnspnzr/5958897 to your computer and use it in GitHub Desktop.
Save Frdnspnzr/5958897 to your computer and use it in GitHub Desktop.
Beispiele für Kopplungsarten
public class KopplungBeispiele {
/*
* Datenelementkopplung
*
* Ich übergebe nur elementare Daten, es liegt kaum Kopplung vor.
*/
private String dateToString(int day, int month, int year) {
return Integer.toString(day) + Integer.toString(month)
+ Integer.toString(year);
}
/*
* Datenstrukturkopplung
*
* Ich übergebe einen selbstdefinierten, strukturierten Datentyp. Die
* Methode wird von dessen Ausarbeitung abhängig.
*/
private boolean printAdress(Customer customer) {
// Wenn Customer sich ändert müssen wir diese Abfrage anpassen.
// Im schlimmsten Fall kommt ein Feld hinzu: Die fehlende Prüfung führt
// nicht zu einem Fehler!
if (customer.name.isEmpty() || customer.adress.isEmpty())
return false;
// In der Realität Drucker ansteuern …
return true;
}
// Besser
private boolean printAdress2(String name, String adress) {
if (name.isEmpty() || adress.isEmpty())
return false;
// Drucker …
return true;
}
private class Customer {
public String name;
public String adress;
}
/*
* Hybridkopplung
*
* Ich steuere in Vorwärtsrichtung durch einen Parameter, wie die Methode
* sich verhält. Ändert sich die Methode müssen sich auch alle Aufrufer
* anpassen.
*/
private Node getChild(Node parent, Side side) {
if (side == Side.LEFT)
return parent.left;
else
return parent.right;
}
// Besser
private Node getLeftNode(Node parent) {
return parent.left;
}
private Node getRightNode(Node parent) {
return parent.right;
}
private class Node {
public Node left;
public Node right;
}
private enum Side {
LEFT, RIGHT;
}
/*
* Externe Kopplung
*
* Zwei Module kommunizieren nicht über die Schnittstelle sondern über
* externe Daten miteinander.
*/
private void addToAccount(Account account, float value) {
account.add(value);
// Wenn es sich nur um eine Simulation handelt Betrag wieder abziehen.
// Dieses Verhalten ist von Außen nicht nachvollziehbar.
if (Status.simulate)
account.substract(value);
}
private abstract class Account {
public abstract void add(float value);
public abstract void substract(float value);
}
private static class Status {
public static boolean simulate = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment