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 | 31 |
Tags
- 반고
- 개발자면접
- 프로그래밍
- nft
- adb logcat
- 캠핑
- 언어치료
- ARM Trustzone 설명
- 면접
- ARM Trust Zone
- 프로그래머면접
- 프로그래머
- 에어텐트
- threadtime
- 텐트
- arm trustzone 강의
- 개발자
- 주식선택기준
- 보안강의
- ARM Trustzone
- ARM trustzone 내용
- 중고텐트
- 초캠중고
- 아웃웰
- 게임 NFT
- 코딩
- android log
- 캠핑장
- setns
- arm trust zone 강의
Archives
- Today
- Total
콩딱일상
디자인 패턴 : Factory Pattern 본문
Factory Pattern이라고 많이 사용하였지만, 실제로는 Simple Factory Pattern만 계속해서 사용한것 같다.
사실 원리는 아주 간단하다.
헤드퍼스트 예제에서는 PizzaStore를 통해서 예제가 나온다. 이게 간단하기 때문에 그냥 이용하면 된다.
class PizzaStore {
public:
Pizza orderPizza(string type) {
Pizza pizza = createPizza(type);
pizza.prepare();
...
return pizza;
}
protected: //예제는 protected인데 사실 private로 하여도 문제는 없을것 같은데...
virtual Pizza createPizza(string type) = 0;
}
class NYPizzaStore : public PizzaStore {
public:
private:
Pizza createPizza(string type) override {
if (type == "치즈") {
return 치즈피자;
}
}
}
class Pizza {
public:
virtual void prepare() {
...
}
}
사실 이러한 형식인데. 여기에서 Factory의 중요한 점은 PizzaStore의 코드의 변경이 없이 새로운 PizzaStore를 추가할수 있다는 점이라고 생각한다.
Pattern을 적용하는데 분명한 이유가 필요하다. 아무 이유도 없이 적용할 필요가 없는것이다.
위의 경우는 어찌보면 Pizza를 만드는 과정에 대해서 interface화가 잘 되어 있어서 가능한것 일수도 있다.
대다수 사용하는 Simple Factory Method의 경우에는 새로운 type이 추가되면 결국 create method쪽에서 수정이 발생해야 한다.
아래의 코드는 조금 복잡하긴 하지만 결국 Simple Factory Method이다.
class ExtensionViewHostFactory {
public:
ExtensionViewHostFactory(const ExtensionViewHostFactory&) = delete;
ExtensionViewHostFactory& operator=(const ExtensionViewHostFactory&) = delete;
static std::unique_ptr<ExtensionViewHost> CreatePopupHost(const GURL& url,
Browser* browser);
static std::unique_ptr<ExtensionViewHost> CreateDialogHost(const GURL& url,
Profile* profile);
static std::unique_ptr<ExtensionViewHost> CreateSidePanelHost(
const GURL& url,
Browser* browser,
content::WebContents* web_contents);
};
std::unique_ptr<ExtensionViewHost> ExtensionViewHostFactory::CreateDialogHost(
const GURL& url,
Profile* profile) {
DCHECK(profile);
return CreateViewHost(url, profile, /*browser=*/nullptr,
/*web_contents=*/nullptr,
mojom::ViewType::kExtensionDialog);
}
std::unique_ptr<ExtensionViewHost> CreateViewHost(
const GURL& url,
Profile* profile,
Browser* browser,
content::WebContents* web_contents,
extensions::mojom::ViewType view_type) {
DCHECK(profile);
// A NULL browser may only be given for dialogs or side panels.
DCHECK(browser || view_type == mojom::ViewType::kExtensionDialog ||
view_type == mojom::ViewType::kExtensionSidePanel);
const Extension* extension = GetExtensionForUrl(profile, url);
if (!extension)
return nullptr;
if (profile->IsOffTheRecord()) {
return CreateViewHostForIncognito(extension, url, profile, browser,
web_contents, view_type);
}
return CreateViewHostForExtension(extension, url, profile, view_type, browser,
web_contents);
}
...
코드의 내용이 많아서 전부 다 내용을 가져오지는 못하였지만 기본적인 의미는 파악할수 있을것으로 생각된다.
사실 나의 경우에는 simple factory method의 형식을 보다 많이 사용하는것 같다.
interface는 동일하지만 상속의 개념이 필요없는 경우에는 simple factory method가 더 맞을수 있다고 생각도 된다.