Factory Method Pattern
Last updated
Last updated
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();
}
}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() + " 호출 완료");
}