본문 바로가기
프로그래밍/디자인 패턴(프로그래밍)

[Design Pattern]Prototype Pattern이란?

by 그래도동 2019. 1. 17.
728x90
반응형

Prototype Pattern이란?

 패턴 내에서 객체의 생성을 제공한다. 객체 생성이 높은 비용으로 수 많은 요청을 하는경우나 비슷한 객체를 지속적으로 생성해야 할 때 사용한다.

 prototype pattern은 원래의 객체로부터 clone()을 사용하여 새로운 객체를 만들며 각 객체에 따라 데이터 수정이 가능하다.



예제


public class Users implements Cloneable {
    private List userList;

    public Users() {
        userList = new ArrayList();
    }

    public Users(List list) {
        this.userList = list;
    }

    public void loadData () {
        userList.add("ssw");
        userList.add("bjh");
        userList.add("ysm");
        userList.add("hoj");
    }

    public List getUserList() {
        return userList;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        List temp = new ArrayList();
        for (String s : this.getUserList()) {
            temp.add(s);
        }
        return new Users(temp);
    }
}


public class main {
    public static void main(String[] args) throws Exception {
        Users originUsers = new Users();
        originUsers.loadData();

        Users cloneUsers = (Users)originUsers.clone();
    }
}


Reference

 https://blog.seotory.com/post/2015/09/java-prototype-pattern

728x90
반응형

댓글