Skip to content

Instantly share code, notes, and snippets.

@jbandura
Created January 23, 2019 16:03
Show Gist options
  • Save jbandura/ecd50af2524cd29b58497492ed07abad to your computer and use it in GitHub Desktop.
Save jbandura/ecd50af2524cd29b58497492ed07abad to your computer and use it in GitHub Desktop.
Strategia
public interface IDatabaseStrategy {
void inicjujPolaczenie();
void zapiszDoBazyDanych(int dane);
void zamknijPolaczenie();
}
public class MysqlStrategy implements IDatabaseStrategy {
@Override
public void inicjujPolaczenie() {
System.out.println("Inicjuje MySQL");
}
@Override
public void zapiszDoBazyDanych(int dane) {
System.out.println("Zapis "+ dane +" do MySQL");
}
@Override
public void zamknijPolaczenie() {
System.out.println("Zamykam polaczenie z MYSQL");
}
}
public class PostgreSQLStrategy implements IDatabaseStrategy {
@Override
public void inicjujPolaczenie() {
System.out.println("Inicjuje PostgreSQL");
}
@Override
public void zapiszDoBazyDanych(int dane) {
System.out.println("Zapis "+ dane +" do PostgreSQL");
}
@Override
public void zamknijPolaczenie() {
System.out.println("Zamykam polaczenie z PostgreSQL");
}
}
public class Aplikacja {
private IDatabaseStrategy dbStrategy;
public Aplikacja(IDatabaseStrategy dbStrategy) {
this.dbStrategy = dbStrategy;
}
public void setDbStrategy(IDatabaseStrategy newStrategy) {
this.dbStrategy.zamknijPolaczenie();
this.dbStrategy = newStrategy;
this.dbStrategy.inicjujPolaczenie();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment