ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ Super Class์ ์๋ ค์ง์ง ์์ ๊ตฌ์ฒด ํด๋์ค๋ฅผ ์์ฑํ๋ ํจํด์ด๋ฉฐ, ์์ ํด๋์ค๊ฐ ์ด๋ค ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ง๋ฅผ ๊ฒฐ์ ํ๋๋ก ํ๋ ํจํด์ด๊ธฐ๋ ํ๋ค.
์ฆ, ๊ฐ์ฒด ์์ฑ ์ฒ๋ฆฌ๋ฅผ ์๋ธ ํด๋์ค๋ก ๋ถ๋ฆฌ ํด ์ฒ๋ฆฌํ๋๋ก ์บก์ํํ๋ ํจํด ์ผ๋ก ๊ฐ์ฒด์ ์์ฑ ์ฝ๋๋ฅผ ๋ณ๋์ ํด๋์ค/๋ฉ์๋๋ก ๋ถ๋ฆฌํจ์ผ๋ก์จ ๊ฐ์ฒด ์์ฑ์ ๋ณํ์ ๋๋นํ๋ ๋ฐ ์ ์ฉํ๋ค.
์ฃผ๋ก ์ธํฐํ์ด์ค ํ์
์ผ๋ก ์ค๋ธ์ ํธ๋ฅผ ๋ฐํํ๋ฏ๋ก Super Class์์๋ Sub Class์์ ์ ํํ ์ด๋ค ํด๋์ค์ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋ค์ด ๋ฐํํ ์ง ์์ง ๋ชปํ๋ค.
public interface Shape{
abstract void draw();
}
public class Star extends Shape{
@Override
public void draw(){
System.out.println("๋ณ ๊ทธ๋ฆฌ๊ธฐ");
}
}
public class Square extends Shape{
@Override
public void draw(){
System.out.println("๋ค๋ชจ ๊ทธ๋ฆฌ๊ธฐ");
}
}
public class ShapeFactory{
public Shape getShape(String shapeType){
if(StringUtils.isEmpty(shapeType)){
return null;
}
if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}else if(shapeType.equalsIgnoreCase("STAR")){
return new Star();
}
return null;
}
}
public FactoryPatternTest{
public static void main(String[] args){
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape("STAR");
shape1.draw();
Shape shape2 = shapeFactory.getShape("SQUARE");
shape2.draw();
}
}
์ฆ, ์๋ธ ํด๋์ค์์ ์ค๋ธ์ ํธ ์์ฑ ๋ฐฉ๋ฒ ํด๋์ค๋ฅผ ๊ฒฐ์ ํ ์ ์๋๋ก ๋ฏธ๋ฆฌ ์ ์ํด๋ ๋ฉ์๋๋ฅผ ํฉํ ๋ฆฌ ๋ฉ์๋๋ผ๊ณ ํ๋ฉฐ, ์ด ๋ฐฉ์์ ํตํด ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ด๋ผ๊ณ ํ๋ค.
Factory Method Pattern์ ์ ์ฌ์ฉํ ๊น?
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ์ฌ์ฉํ๋ ์ด์ ๋ ํด๋์ค๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ธฐ ์ํ๊ฒ์
๋๋ค. ๊ฒฐํฉ๋๋ผ๋ ๊ฒ์ ๊ฐ๋จํ ๋งํด ํด๋์ค์ ๋ณ๊ฒฝ์ ์ด ์๊ฒผ์ ๋ ์ผ๋ง๋ ๋ค๋ฅธ ํด๋์ค์๋ ์ํฅ์ ์ฃผ๋๊ฐ์
๋๋ค. ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํด ์ฌ์ฉํ๋ ๊ฒ์ ๋ฐฉ์งํ๊ณ ์๋ธ ํด๋์ค์ ์์ํจ์ผ๋ก์จ ๋ณด๋ค ํจ์จ์ ์ธ ์ฝ๋ ์ ์ด๋ฅผ ํ ์ ์๊ณ ์์กด์ฑ์ ์ ๊ฑฐํฉ๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฒฐํฉ๋ ๋ํ ๋ฎ์ถ ์ ์์ต๋๋ค.
์์
Sub Class์ getConnection()์ ํตํด ๋ง๋ค์ด์ง Connection ์ข
๋ฅ๊ฐ ๋ฌ๋ผ์ง ์ ์๊ฒ ํ๋ ๊ฒ์ ๋ชฉ์ ์ผ๋ก ํ๋ ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ์์์ด๋ค.
package springbook.user.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import springbook.user.domain.User;
public abstract class UserDao {
public void add(User user) throws ClassNotFoundException, SQLException {
Connection c = getConnection();
PreparedStatement ps = c.prepareStatement(
"insert into users(id, name, password) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws ClassNotFoundException, SQLException {
Connection c = getConnection();
PreparedStatement ps = c
.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
rs.close();
ps.close();
c.close();
return user;
}
abstract protected Connection getConnection() throws ClassNotFoundException, SQLException ;
}
package springbook.user.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DUserDao extends UserDao {
protected Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/springbook?characterEncoding=UTF-8",
"DuserId", "DUserPassword");
return c;
}
}
package springbook.user.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class NUserDao extends UserDao {
protected Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost/springbook?characterEncoding=UTF-8",
"NUserId", "NUserPassword");
return c;
}
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
UserDao dao = new NUserDao();
User user = new User();
user.setId("admin");
user.setName("test1");
user.setPassword("admintest");
dao.add(user);
System.out.println(user.getId() + " ๋ฑ๋ก ์๋ฃ");
User user2 = dao.get(user.getId());
System.out.println(user2.getName());
System.out.println(user2.getPassword());
System.out.println(user2.getId() + " ํธ์ถ ์๋ฃ");
}
NUserDao์ DUserDao๊ฐ Connection์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ด ๋ค๋ฅด๋ฏ๋ก, ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ผ๋ก ๋ณผ ์ ์๋ค.
์ฐธ๊ณ