Created
January 23, 2019 16:03
-
-
Save jbandura/ecd50af2524cd29b58497492ed07abad to your computer and use it in GitHub Desktop.
Strategia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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