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. Seurat | 单细胞分析工具

    Seurat是一个老牌的单细胞分析工具了(satija的力作),我之前测试过,但是没怎么用. 最近发现这个工具又publish在了NBT上,所以很有必要看一下这篇文章. Integrating sin ...

  2. centos 安装 和 linux 简单命令

    1. centos 安装 参照:https://www.cnblogs.com/tiger666/articles/10259102.html 安装过程注意点: 1. 安装过程中的选择安装Basic ...

  3. day 7 -10 集合,文本、文件操作,函数

    day7 一.回顾 1.列表和字典在循环里边尽量不要删除元素,很麻烦 2.元组:如果元组里边只有一个逗号,且不加逗号,次元素是什么类型,就是什么类型. 二.集合 ''' 集合:可变的数据类型,它里边的 ...

  4. DRF之权限认证,过滤分页,异常处理

    1. 认证Authentication 在配置文件中配置全局默认的认证方案 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_f ...

  5. python基础之变量以及if语句

    1.变量 1.1定义:变量是一种介质,是将一些值暂时存储在内存中,方便后续程序调用.可将其看作容器但其内部的东西是可变化的. 1.2 变量的命名规则: 1.变量的命名只能由数字,字母,下划线构成. 2 ...

  6. Merge K Sorted List(含Merge Two Sorted LIst) leetcode java

    问题描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...

  7. 排座椅(洛谷P1056)

    题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上课时会交头接耳. 同 ...

  8. CSS3--难以想象的滤镜效果

    CSS 语法 filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate( ...

  9. 如何在VMware系统中的ubuntu16.04中建立与win7系统的共享文件夹

    点击虚拟机设置一次得到如图所示 系统默认放在了 /mnt/hgfs文件夹 点击虚拟机安装vmware tools 解压vmware tools压缩包 运行sudo ./vmware-install.p ...

  10. python-flask-SQLAlchemy-Utils组件

    SQLAlchemy-Utils,提供choice功能 定义: # pip3 install sqlalchemy-utils from sqlalchemy_utils import ChoiceT ...