Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper toy based on the given type.

Example

ToyFactory tf = ToyFactory();
Toy toy = tf.getToy('Dog');
toy.talk();
>> Wow toy = tf.getToy('Cat');
toy.talk();
>> Meow

加了一个Enum来表示不用的ToyType; 可以不加直接上String
 /**
* 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
@Override
public void talk(){
System.out.println("Wow");
}
} class Cat implements Toy {
// Write your code here
@Override
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
ToyType t = ToyType.fromString(type);
switch (t){
case DOG: return new Dog();
case CAT: return new Cat();
default: return null;
}
} public enum ToyType { DOG("Dog"), CAT("Cat"), UNKNOWN("UNKNOWN"); private String name; private ToyType(String name) {
this.name = name;
} public static ToyType fromString(String name) {
for (ToyType toyType : ToyType.values()) {
if (toyType.getName().equals(name)) {
return toyType;
}
}
return UNKNOWN;
} private String getName(){
return name;
}
}
}

Toy Factory的更多相关文章

  1. [LintCode] Toy Factory 玩具工厂

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  2. UVM基础之---------uvm factory机制base

    从名字上面就知道,uvm_factory用来制造uvm_objects和component.在一个仿真过程中,只有一个factory的例化存在. 用户定义的object和component types ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. Poj(3686),最小权匹配,多重匹配,KM

    题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...

  5. poj 3686

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3791   Accepted: 1631 Descr ...

  6. [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 Descr ...

  7. 2018.06.27The Windy's(费用流)

    The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...

  8. POJ3686 The Windy's 【费用流】*

    POJ3686 The Windy’s Description The Windy’s is a world famous toy factory that owns M top-class work ...

  9. POJ 3686 The Windy's(思维+费用流好题)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Descr ...

随机推荐

  1. PHP数组合并和去重的函数有哪些

    PHP数组合并和去重的函数有哪些 一.总结 一句话总结:合并:array_merge() array_merge_recursive() +号:去重:array_flip() array_unique ...

  2. Java 反射(简单捋一下)

    有Student类,Person类,还有一个叫Class的类,这是反射的源头. 正常方式:通过完整的类名 > 通过new实例化 > 取得实例化对象 反射方式:实例化对象 > getC ...

  3. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格惰性加载节点

    jQuery EasyUI 树形菜单 - 树形网格惰性加载节点 有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载 ...

  4. android -------- Data Binding的使用 ( 五) include

    Data Binding的中 include 标签的使用 inclune使用和原来一样,但要如何使数据也在 include中使用呢? 先看看我的布局文件 include的布局文件,也要使用 <l ...

  5. GIL锁,线程池

    内容梗概: 1.线程队列 2.线程池 3.GIL锁 1.线程队列 1.1先进先出队列(FIFO)import queueq = queue.Queue(3)q.put(1)q.put(2)q.put( ...

  6. HashMap的两种排序方式

    Map<String, Integer> map = new HashMap<String, Integer>();map.put("d", 2);map. ...

  7. 6月5 Smarty变量调节器

    变量调节器:<{$a|变量调节器}> 主要修改此页面的信息来了解变量调节器:test0605/main.php和模板文件:main0605.html 1.利用给定的变量调节器 capita ...

  8. 前端VUE框架

    一.什么是VUE?  它是一个构建用户界面的JAVASCRIPt框架  vue不关心你页面上的是什么标签,它操作的是变量或属性 为什么要使用VUE? 在前后端分离的时候,后端只返回json数据,再没有 ...

  9. leetcode-algorithms-30 Substring with Concatenation of All Words

    leetcode-algorithms-30 Substring with Concatenation of All Words You are given a string, s, and a li ...

  10. OWASP TOP 10 2017中文译文

    说明:owasp top 10其实有中文官方版本:本文是按着英文版进行翻译而成. 官方中文版:http://www.owasp.org.cn/owasp-project/OWASPTop102017v ...