-
Simple Factory PatternModeling/DesignPattern 2019. 8. 22. 09:16
1. Overview
2. Description
2.1 Motivation
Here we simply move the instantiation logic to a separate class and most commonly to a static method of this class
Some do not consider a simple factory to be a design pattern, as its simply a method that encapsulates object instantiation. Nothing complex goes on in that method.
Typically we want to do this if we have more than one option when instantiating object and simple logic is used to choose a correct class.
2.2 Consideration
2.2.1 Implementation
Add a method that returns the desired object instance which is typically static and accepts some argument to decide which class to instantiate. You can also provide additional arguments that will be used to instantiate objects.
Simple factory can be just a method in the existing class. Adding a separate class, however, allows other parts of your code to use simple factory more easily.
Simple factory itself doesn't need any state tracking so it's best to keep this as a static method.
2.2.2 Design
Simple factory will, in turn, may use other design patterns like builder to construct objects.
In case you want to specialize in your simple factory in subclasses, you need factory method design pattern instead.
2.3 Comparison between Simple factory and Factory method
Simple Factory Factory Method We simply move our instantiation logic away from client code. Typically in a static method Factory method is more useful when you want to delegate object creation to subclasses Simple factory knows about all classes whose objects it can create In Factory method we don't know in advance about all product subclasses. 2.4 Pitfalls
The criteria used by simple factory to decide which object to instantiate can get more convoluted/complex over time. If you find yourself in such situations then use factory method design pattern.
3. Example
abstract class Post { private Long id; private String title; private String content; private LocalDateTime createdOn; private LocalDateTime publishedOn; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public LocalDateTime getCreatedOn() { return createdOn; } public void setCreatedOn(LocalDateTime createdOn) { this.createdOn = createdOn; } public LocalDateTime getPublishedOn() { return publishedOn; } public void setPublishedOn(LocalDateTime publishedOn) { this.publishedOn = publishedOn; } } class NewsPost extends Post { private String headline; private LocalDate newsTime; public String getHeadline() { return headline; } public void setHeadline(String headline) { this.headline = headline; } public LocalDate getNewsTime() { return newsTime; } public void setNewsTime(LocalDate newsTime) { this.newsTime = newsTime; } } class BlogPost extends Post { private String author; private String[] tags; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String[] getTags() { return tags; } public void setTags(String[] tags) { this.tags = tags; } } class ProductPost extends Post { private String imageUrl; private String name; public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class PostFactory { public static Post createPost(String type) { switch (type) { case "blog": return new BlogPost(); case "news": return new NewsPost(); case "product": return new ProductPost(); default: throw new IllegalArgumentException("Post type is unknown"); } } } public class DemoClient { public static void main(String[] args) { Post post = PostFactory.createPost("news"); System.out.println(post); } }
4. Reference
https://code.tutsplus.com/tutorials/design-patterns-the-simple-factory-pattern--cms-22345
https://www.codeproject.com/Articles/1131770/Factory-Patterns-Simple-Factory-Pattern
'Modeling > DesignPattern' 카테고리의 다른 글
Model–view–controller (MVC) (0) 2020.02.23 Categorizing Design Pattern (0) 2019.09.29 Abstract factory (0) 2019.08.23 Factory Pattern (0) 2019.08.22 Bridge Design Pattern (0) 2019.08.19