This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| design_pattern:abstract_factory_pattern [2017/10/28 09:52] – ledyx | design_pattern:abstract_factory_pattern [2021/02/07 03:24] (current) – [Abstract Factory Pattern] ledyx | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| 관련 부품을 조합해서 인스턴스 생성 | 관련 부품을 조합해서 인스턴스 생성 | ||
| * 부품의 구체적인 구현에는 주목하지 않고, **인터페이스(API)에 주목**. | * 부품의 구체적인 구현에는 주목하지 않고, **인터페이스(API)에 주목**. | ||
| - | * 추상적인 공장(Abstract Factory)에서 추상적인 | + | |
| + | * 많은 수의 연관된 하위 클래스를 특정 그룹으로 묶어 한번에 교체.\\ ex) 특정 라이브러리 배포시 OS별로 지원하는 기능이 다를 때, OS별 기능 변경을 통합적으로 변경. | ||
| - | {{tag> | + | {{tag> |
| + | = Factory = | ||
| + | == Abstract Factory == | ||
| + | <sxh java> | ||
| + | package factory; | ||
| + | |||
| + | public abstract class Factory { | ||
| + | public static Factory getFactory(String className) { | ||
| + | Factory factory = null; | ||
| + | |||
| + | try { | ||
| + | factory = (Factory) Class.forName(className).newInstance(); | ||
| + | } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return factory; | ||
| + | } | ||
| + | |||
| + | public abstract LinkItem createLink(String caption, String url); | ||
| + | public abstract TrayItem createTray(String caption); | ||
| + | public abstract Page createPage(String caption, String author); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Abstract Products == | ||
| + | <sxh java ; title: Template> | ||
| + | public abstract class Item { | ||
| + | protected String caption; | ||
| + | |||
| + | public Item(String caption) { | ||
| + | super(); | ||
| + | this.caption = caption; | ||
| + | } | ||
| + | |||
| + | public abstract String makeHTML(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | <sxh java ; title: Concrete, Abstract Product> | ||
| + | package factory; | ||
| + | |||
| + | public abstract class LinkItem extends Item { | ||
| + | |||
| + | protected String url; | ||
| + | |||
| + | public LinkItem(String caption, String url) { | ||
| + | super(caption); | ||
| + | this.url = url; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: Concrete, Abstruct Product> | ||
| + | package factory; | ||
| + | |||
| + | import java.util.ArrayList; | ||
| + | |||
| + | public abstract class TrayItem extends Item { | ||
| + | |||
| + | protected final ArrayList< | ||
| + | |||
| + | public TrayItem(String caption) { | ||
| + | super(caption); | ||
| + | } | ||
| + | |||
| + | public void add(Item item) { | ||
| + | tray.add(item); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: Abstract Product> | ||
| + | package factory; | ||
| + | |||
| + | import java.io.BufferedWriter; | ||
| + | import java.io.FileOutputStream; | ||
| + | import java.io.OutputStreamWriter; | ||
| + | import java.nio.charset.StandardCharsets; | ||
| + | import java.util.ArrayList; | ||
| + | |||
| + | public abstract class Page { | ||
| + | protected String title; | ||
| + | protected String author; | ||
| + | protected ArrayList< | ||
| + | |||
| + | public Page(String title, String author) { | ||
| + | this.title = title; | ||
| + | this.author = author; | ||
| + | } | ||
| + | |||
| + | public void add(Item item) { | ||
| + | content.add(item); | ||
| + | } | ||
| + | |||
| + | public void output() { | ||
| + | System.out.println(this.makeHTML()); | ||
| + | |||
| + | try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(title + " | ||
| + | bw.write(this.makeHTML()); | ||
| + | } catch (Exception e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public abstract String makeHTML(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Concrete Factories = | ||
| + | == Concrete Factory1 - ListFactory == | ||
| + | === Concrete Factory === | ||
| + | <sxh java> | ||
| + | package listfactory; | ||
| + | |||
| + | import factory.Factory; | ||
| + | import factory.LinkItem; | ||
| + | import factory.Page; | ||
| + | import factory.TrayItem; | ||
| + | |||
| + | public class ListFactory extends Factory { | ||
| + | |||
| + | @Override | ||
| + | public LinkItem createLink(String caption, String url) { | ||
| + | return new ListLink(caption, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public TrayItem createTray(String caption) { | ||
| + | return new ListTray(caption); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public Page createPage(String caption, String author) { | ||
| + | return new ListPage(caption, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Concrete Products === | ||
| + | |||
| + | <sxh java> | ||
| + | package listfactory; | ||
| + | |||
| + | import factory.LinkItem; | ||
| + | |||
| + | public class ListLink extends LinkItem { | ||
| + | |||
| + | public ListLink(String caption, String url) { | ||
| + | super(caption, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append("< | ||
| + | sb.append("< | ||
| + | sb.append(url); | ||
| + | sb.append(" | ||
| + | sb.append(caption); | ||
| + | sb.append("</ | ||
| + | sb.append("</ | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java> | ||
| + | package listfactory; | ||
| + | |||
| + | import factory.TrayItem; | ||
| + | |||
| + | public class ListTray extends TrayItem { | ||
| + | |||
| + | public ListTray(String caption) { | ||
| + | super(caption); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append("< | ||
| + | sb.append(caption); | ||
| + | |||
| + | sb.append(" | ||
| + | tray.forEach(item -> sb.append(item.makeHTML())); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("</ | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java> | ||
| + | package listfactory; | ||
| + | |||
| + | import factory.Page; | ||
| + | |||
| + | public class ListPage extends Page { | ||
| + | |||
| + | public ListPage(String title, String author) { | ||
| + | super(title, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append("< | ||
| + | sb.append("< | ||
| + | sb.append(title); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("< | ||
| + | |||
| + | sb.append("< | ||
| + | sb.append(title); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("< | ||
| + | content.forEach(item -> sb.append(item.makeHTML())); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("< | ||
| + | |||
| + | sb.append("< | ||
| + | sb.append(author); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("</ | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Concrete Factory2 - DivFactory == | ||
| + | === Concrete Factory === | ||
| + | <sxh java> | ||
| + | package divfactory; | ||
| + | |||
| + | import factory.Factory; | ||
| + | import factory.LinkItem; | ||
| + | import factory.Page; | ||
| + | import factory.TrayItem; | ||
| + | |||
| + | public class DivFactory extends Factory { | ||
| + | |||
| + | @Override | ||
| + | public LinkItem createLink(String caption, String url) { | ||
| + | return new DivLink(caption, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public TrayItem createTray(String caption) { | ||
| + | return new DivTray(caption); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public Page createPage(String caption, String author) { | ||
| + | return new DivPage(caption, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Concrete Products === | ||
| + | <sxh java> | ||
| + | package divfactory; | ||
| + | |||
| + | import factory.LinkItem; | ||
| + | |||
| + | public class DivLink extends LinkItem { | ||
| + | |||
| + | public DivLink(String caption, String url) { | ||
| + | super(caption, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append(" | ||
| + | sb.append("< | ||
| + | sb.append(url); | ||
| + | sb.append(" | ||
| + | sb.append(caption); | ||
| + | sb.append("</ | ||
| + | sb.append("</ | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java> | ||
| + | package divfactory; | ||
| + | |||
| + | import factory.TrayItem; | ||
| + | |||
| + | public class DivTray extends TrayItem { | ||
| + | |||
| + | public DivTray(String caption) { | ||
| + | super(caption); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append(" | ||
| + | |||
| + | sb.append(" | ||
| + | sb.append(caption); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append(" | ||
| + | tray.forEach(item -> sb.append(item.makeHTML())); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("</ | ||
| + | sb.append("< | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java> | ||
| + | package divfactory; | ||
| + | |||
| + | import factory.Page; | ||
| + | |||
| + | public class DivPage extends Page { | ||
| + | |||
| + | public DivPage(String title, String author) { | ||
| + | super(title, | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String makeHTML() { | ||
| + | StringBuilder sb = new StringBuilder(); | ||
| + | sb.append("< | ||
| + | sb.append("< | ||
| + | sb.append(title); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("< | ||
| + | |||
| + | sb.append("< | ||
| + | sb.append(title); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append(" | ||
| + | content.forEach(item -> sb.append(item.makeHTML())); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("< | ||
| + | |||
| + | sb.append("< | ||
| + | sb.append(author); | ||
| + | sb.append("</ | ||
| + | |||
| + | sb.append("</ | ||
| + | |||
| + | return sb.toString(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = Client = | ||
| + | * <fc red> | ||
| + | * API만 이용! 말그대로 <fc red> | ||
| + | |||
| + | <sxh java ; highlight: [13]> | ||
| + | import factory.Factory; | ||
| + | import factory.LinkItem; | ||
| + | import factory.Page; | ||
| + | import factory.TrayItem; | ||
| + | |||
| + | public class Main { | ||
| + | public static void main(String[] args) { | ||
| + | operateFactory(" | ||
| + | operateFactory(" | ||
| + | } | ||
| + | |||
| + | private static void operateFactory(String className) { | ||
| + | Factory factory = Factory.getFactory(className); | ||
| + | |||
| + | |||
| + | LinkItem link_todayHumor = factory.createLink(" | ||
| + | LinkItem link_ruliweb = factory.createLink(" | ||
| + | |||
| + | TrayItem trary_community = factory.createTray(" | ||
| + | trary_community.add(link_todayHumor); | ||
| + | trary_community.add(link_ruliweb); | ||
| + | |||
| + | |||
| + | LinkItem link_google = factory.createLink(" | ||
| + | LinkItem link_daum = factory.createLink(" | ||
| + | |||
| + | LinkItem link_naver = factory.createLink(" | ||
| + | LinkItem link_naver_m = factory.createLink(" | ||
| + | |||
| + | |||
| + | TrayItem tray_naver = factory.createTray(" | ||
| + | tray_naver.add(link_naver); | ||
| + | tray_naver.add(link_naver_m); | ||
| + | |||
| + | TrayItem tray_searchEngine = factory.createTray(" | ||
| + | tray_searchEngine.add(link_google); | ||
| + | tray_searchEngine.add(link_daum); | ||
| + | tray_searchEngine.add(tray_naver); | ||
| + | |||
| + | |||
| + | Page page = factory.createPage(className, | ||
| + | page.add(trary_community); | ||
| + | page.add(tray_searchEngine); | ||
| + | |||
| + | page.output(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == 실행 결과 == | ||
| + | === Concrete Factory1 - ListFactory === | ||
| + | |||
| + | <sxh html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <li> | ||
| + | 커뮤니티 | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| + | </ | ||
| + | 검색 엔진 | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | <li> | ||
| + | Naver | ||
| + | <ul> | ||
| + | < | ||
| + | < | ||
| + | </ul> | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | === Concrete Factory2 - DivFactory === | ||
| + | |||
| + | <sxh html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <div> | ||
| + | <div style=" | ||
| + | < | ||
| + | |||
| + | <div> | ||
| + | < | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | <br> | ||
| + | |||
| + | <div style=" | ||
| + | < | ||
| + | |||
| + | <div> | ||
| + | < | ||
| + | |||
| + | < | ||
| + | |||
| + | <div style=" | ||
| + | < | ||
| + | |||
| + | <div> | ||
| + | < | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | <br> | ||
| + | </ | ||
| + | </ | ||
| + | <br> | ||
| + | </ | ||
| + | < | ||
| + | </ | ||
| + | </ | ||