LintCode_496_Toy Factory
2016-04-18 21:49
447 查看
Factory is a design pattern in common usage. Please implement a
can generate proper toy based on the given type.
您在真实的面试中是否遇到过这个题?
Yes
样例
ToyFactorywhich
can generate proper toy based on the given type.
您在真实的面试中是否遇到过这个题?
Yes
样例
ToyFactory tf = ToyFactory(); Toy toy = tf.getToy('Dog'); toy.talk(); >> Wow toy = tf.getToy('Cat'); toy.talk(); >> Meow
/** * Your object will be instantiated and called as such: * ToyFactory tf = new ToyFactory(); * Toy toy = tf.getToy(type); * toy.talk(); */ interface Toy { void talk(); } class Dog implements Toy { // Write your code here public void talk(){ System.out.println("Wow"); } } class Cat implements Toy { // Write your code here public void talk(){ System.out.println("Meow"); } } public class ToyFactory { /** * @param type a string * @return Get object of the type */ public Toy getToy(String type) { // Write your code here Toy toy; if(type.equals("Cat")){ toy = new Cat(); return toy; }else if (type.equals("Dog")){ toy = new Dog(); return toy; } return null; } }
相关文章推荐
- 第一篇博客
- 浙大MOOC 第七章测验
- Angularjs基础(三)
- 爱不爱只有自己知道,变没变只有自…
- shell中eval的神奇之法,shell中引号的争端
- 山寨币开发
- 一图丨雾霾红色预警 对你的…
- 【Leetcode】:226. Invert Binary Tree 问题 in JAVA
- error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
- Andriod——IPC进程通信JAVA层工作原理简析
- hdu 2032
- ios入门
- PHPthinking赠书了!
- 操作系统是怎么工作的——函数的堆栈框架/嵌入式代码
- POJ 3422 Kaka's Matrix Travels 【最小费用最大流】
- yii2 memcache 跨平台交互 键和值不一样
- HDOJ--2000--ASCII码排序(java实现)
- SlidingMenu库的导入以及Jar mismatch问题
- python 多线程就这么简单
- STL list