"HR자바 Java Factory Pattern"의 두 판 사이의 차이

(새 문서: ==개요== ;{{subst:PAGENAME}} * {{HR자바 헤더}} {{HR자바 Advanced}} |} ---- <source lang='java'> </source> <source lang='java'> </source> <source lang='java'> </source>)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 2개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;HR자바 Java Factory Pattern
;HR자바 Java Factory Pattern
*  
* https://www.hackerrank.com/challenges/java-factory/problem


{{HR자바 헤더}}
{{HR자바 헤더}}
9번째 줄: 9번째 줄:
----
----


<source lang='java'>
<syntaxhighlight lang='java'>
</source>
import java.util.*;
<source lang='java'>
import java.security.*;
</source>
interface Food {
<source lang='java'>
    public String getType();
</source>
}
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>

2020년 11월 2일 (월) 02:51 기준 최신판

개요[ | ]

HR자바 Java Factory Pattern
해커랭크 Java
# 문제 비고
HR자바 Advanced e
53 HR자바 Java Varargs - Simple Addition
54 HR자바 Java Reflection - Attributes
55 HR자바 Can You Access?
56 HR자바 Prime Checker
57 HR자바 Java Factory Pattern
58 HR자바 Java Singleton Pattern
59 HR자바 Java Visitor Pattern
60 HR자바 Java Annotations
61 HR자바 Covariant Return Types
62 HR자바 Java Lambda Expressions
63 HR자바 Java MD5
64 HR자바 Java SHA-256

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) {
        //Write your code here
        if( order.equals("pizza") ) return new Pizza();
        if( order.equals("cake") ) return new Cake();
        return null;
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);
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}