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 ...
随机推荐
- pandas更换index,column名称
1)仅换掉index名称 df.index = list 2)调整index时,后面的项目也要跟着调整: df.reindex(list) 注意如果list中出现了df中没有的index,后面的项目会 ...
- codeforces 578a//A Problem about Polyline// Codeforces Round #320 (Div. 1)
题意:一个等腰直角三角形一样的周期函数(只有x+轴),经过给定的点(a,b),并且半周期为X,使X尽量大,问X最大为多少? 如果a=b,结果就为b 如果a<b无解. 否则,b/(2*k*x-a) ...
- Web3.js 0.20.x API 中文版翻译
文档原始链接为:https://web3.learnblockchain.cn/0.2x.x/,欢迎大家前往查阅,本文只是节选开头部分的介绍及API列表索引,以下为翻译正文: 为了开发一个基于以太坊的 ...
- synchronized相关用法简述
synchronized 锁,他是一个java 的关键字,能够保证同一线程只有一个线程访问或使用此修饰的代码块 用法 synchronized方法,synchronized块 synchronized ...
- 框架中如何根据fileupload工具包实现文件上传功能
工具包 Apache-fileupload.jar – 文件上传核心包. Apache-commons-io.jar – 这个包是fileupload的依赖包.同时又是一个工具包. 代码 servle ...
- drf 生成接口文档
REST framework可以自动帮助我们生成接口文档.接口文档以网页的方式呈现. 自动接口文档能生成的是继承自APIView及其子类的视图. 一.安装依赖 REST framewrok生成接口文档 ...
- VS Code插件
VS Code下载地址: https://code.visualstudio.com/ 1.view in browser 和 Open-In-Browser 安装可在编辑器中打开html,在 ...
- pre打印
echo "<pre>";print_r(var);echo "</pre>";
- python time 表示方式
- Object.keys的使用
链接:https://www.nowcoder.com/questionTerminal/52c41b84e32a4158883cb112a1d1f850来源:牛客网 输出对象中值大于2的key的数组 ...