장점 :
추상적인 코드를 코드를 구체적인 코드 변경없이도 확장이가능함(ocp)
추상적인 코드와 구체적인 코드 분리할 수 있음
특징은 클라이언트는 추상적인 계층구조만 사용 implementation 간접 사용
클라이언트가 추상화되어있는 abstraction 계층 만사용하고
public interface Champion {
void move();
void skillQ();
void skillW();
void skillE();
void skillR();
}
public class DefaultChampion implements Champion {
private Skin skin;
private String name;
public DefaultChampion(Skin skin, String name) {
this.skin = skin;
this.name = name;
}
@Override
public void move() {
System.out.printf("%s %s move\n", skin.getName(), this.name);
}
@Override
public void skillQ() {
System.out.printf("%s %s Q\n", skin.getName(), this.name);
}
@Override
public void skillW() {
System.out.printf("%s %s W\n", skin.getName(), this.name);
}
@Override
public void skillE() {
System.out.printf("%s %s E\n", skin.getName(), this.name);
}
@Override
public void skillR() {
System.out.printf("%s %s R\n", skin.getName(), this.name);
}
}
public class 아리 extends DefaultChampion {
public 아리(Skin skin) {
super(skin, "아리");
}
}
public interface Skin {
String getName();
}
public class PoolParty implements Skin {
@Override
public String getName() {
return "PoolParty";
}
}
public class Client {
public static void main(String[] args) {
Champion PoolParty = new 아리(new PoolParty());
PoolParty.skillQ();
PoolParty.skillR();
}
}
public class JdbcExample {
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("org.h2.Driver");
//driver -> Implementation
//Connection,Statement, PreparedStatement ,ResultSet -> 추상화 Abstraction
try (Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "")) {
String sql = "CREATE TABLE ACCOUNT " +
"(id INTEGER not NULL, " +
" email VARCHAR(255), " +
" password VARCHAR(255), " +
" PRIMARY KEY ( id ))";
Statement statement = conn.createStatement();
statement.execute(sql);
PreparedStatement statement1 = conn.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
출처 : 백기선님의 디자인패턴