Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- Factory Method Pattern
- design pattern
- java
- camel case error
- Linux
- executable jar
- prototype pattern
- data
- v-show vs v-if
- maven
- zen of python
- Singleton pattern
- builder pattern
- IPv6
- ubuntu
- Python
- MVVM
- Spring Boot
- mount point
- $event.target.value
- vuejs
- kebab case
- click.native
- v-model vs v-bind
Archives
- Today
- Total
Eddie's Blog
[design pattern] Prototype Pattern 본문
유형: 생성자 패턴
다양한 객체를 생성하기 위해 Prototypical(prototype을 구현한) 인스턴스를 사용하고, 이 Prototype의 복제를 통해 새로운 객체를 생성한다.
// prototype
public abstract class PlayableContent implements Cloneable {
public abstract void play();
@Override
protected Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
// concrete prototypes
public class Movie extends PlayableContent {
private String title;
public Movie(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public void play() {
System.out.println(String.format("Movie (%s) plays", this.title));
}
@Override
public PlayableContent clone() {
System.out.println("Cloning Movie...");
return (Movie) super.clone();
}
}
public class Song extends PlayableContent {
private String title;
public Song(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public void play() {
System.out.println(String.format("Song (%s) plays", this.title));
}
@Override
public PlayableContent clone() {
System.out.println("Cloning Song...");
return (Song) super.clone();
}
}
// container
public class Player {
private static Map<String, PlayableContent> contents = new HashMap<String, PlayableContent>() {{
put("movie", new Movie("Back to the future"));
put("song", new Song("Billie Jean"));
}};
public static PlayableContent getContent(String contentType) {
return (PlayableContent) contents.get(contentType).clone();
}
}
// test
public class Test {
public static void main(String[] args) {
PlayableContent content = Player.getContent("song");
content.play();
//Cloning Song...
//Song (Billie Jean) plays
}
}
'programmings' 카테고리의 다른 글
[design pattern] Singleton Pattern (0) | 2019.03.28 |
---|---|
[design pattern] Factory Method Pattern (0) | 2019.03.28 |
[design pattern] Builder Pattern (0) | 2019.03.28 |
[design pattern] Abstract Factory Pattern (0) | 2019.03.28 |
[spring boot] How to package a project with shell-executable jar (0) | 2019.02.11 |
Comments