Toy Factory
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper toy based on the given type.
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的更多相关文章
- [LintCode] Toy Factory 玩具工厂
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
- UVM基础之---------uvm factory机制base
从名字上面就知道,uvm_factory用来制造uvm_objects和component.在一个仿真过程中,只有一个factory的例化存在. 用户定义的object和component types ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Poj(3686),最小权匹配,多重匹配,KM
题目链接 The Windy's | Time Limit: 5000MS | Memory Limit: 65536K | | Total Submissions: 4939 | Accepted: ...
- poj 3686
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3791 Accepted: 1631 Descr ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
- 2018.06.27The Windy's(费用流)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6003 Accepted: 2484 Descripti ...
- POJ3686 The Windy's 【费用流】*
POJ3686 The Windy’s Description The Windy’s is a world famous toy factory that owns M top-class work ...
- POJ 3686 The Windy's(思维+费用流好题)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5362 Accepted: 2249 Descr ...
随机推荐
- 最长连续子序列 Longest Consecutive Sequence
2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...
- nodejs中function*、yield和Promise的示例
var co = require("co"); var fs = require("fs"); function cusReadFile(fileName) { ...
- R语言中知识点总结(一)
source("http://bioconductor.org/biocLite.R") biocLite("GEOquery") library(Biobas ...
- Advances in Single Cell Genomics to Study Brain Cell Types | 会议概览
单细胞在脑科学方面的应用 Session 1: Deciphering the Cellular Landscape of the Brain Using Single Cell Transcript ...
- C#异步的世界(重点:新异步)
http://www.cnblogs.com/zhaopei/p/async_two.html
- LeetCode--268--缺失数字
问题描述: 给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数. 示例 1: 输入: [3,0,1] 输出: 2 示例 2: 输入: [9 ...
- android ------- 运行官方NDK 案例HelloJNI
下载案例 HelloJNI ,导入工程到Eclipse, 可以直接下载我的案例, 源码下载:https://github.com/DickyQie/android-ndk 目录图 使用命令生成 . ...
- IntelliJ IDEA 安装 Scala 插件
本页面中对在 IntelliJ 中安装 Scala 插件的步骤和方法进行了描述. 需要在 IntelliJ 安装 Scala 插件,你首先需要在你的计算机中安装 IntelliJ .IntelliJ ...
- Windows搭建Express环境
1. 以管理员身份运行cmd 2. cmd进入要配置项目的文件夹 3. nmp init 4. 使用default配置——一直回车 5. nmp install --save express //np ...
- IntelliJ IDEA的调试方法
快捷键F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 S ...