문서 편집 권한이 없습니다. 다음 이유를 확인해주세요: 요청한 명령은 다음 권한을 가진 사용자에게 제한됩니다: 사용자. 문서의 원본을 보거나 복사할 수 있습니다. ==개요== ;HR자바 Java Factory Pattern * https://www.hackerrank.com/challenges/java-factory/problem {{HR자바 헤더}} {{HR자바 Advanced}} |} ---- <syntaxhighlight lang='java'> import java.util.*; import java.security.*; interface Food { public String getType(); } class Pizza implements Food { public String getType() { return "Someone ordered a Fast Food!"; } } class Cake implements Food { public String getType() { return "Someone ordered a Dessert!"; } } class FoodFactory { public Food getFood(String order) { </syntaxhighlight> <syntaxhighlight lang='java'> //Write your code here if( order.equals("pizza") ) return new Pizza(); if( order.equals("cake") ) return new Cake(); return null; </syntaxhighlight> <syntaxhighlight lang='java'> public class Solution { public static void main(String args[]){ Do_Not_Terminate.forbidExit(); try{ Scanner sc=new Scanner(System.in); //creating the factory FoodFactory foodFactory = new FoodFactory(); //factory instantiates an object Food food = foodFactory.getFood(sc.nextLine()); System.out.println("The factory returned "+food.getClass()); System.out.println(food.getType()); } catch (Do_Not_Terminate.ExitTrappedException e) { System.out.println("Unsuccessful Termination!!"); } } } class Do_Not_Terminate { public static class ExitTrappedException extends SecurityException { private static final long serialVersionUID = 1L; } public static void forbidExit() { final SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(Permission permission) { if (permission.getName().contains("exitVM")) { throw new ExitTrappedException(); } } }; System.setSecurityManager(securityManager); } } </syntaxhighlight> 이 문서에서 사용한 틀: 틀:Ed (원본 보기) 틀:HR자바 Advanced (원본 보기) 틀:HR자바 헤더 (원본 보기) 틀:언어아이콘 (원본 보기) 틀:언어이미지 (원본 보기) HR자바 Java Factory Pattern 문서로 돌아갑니다.