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 ...
随机推荐
- 最大频率栈 Maximum Frequency Stack
2018-10-06 22:01:11 问题描述: 问题求解: 为每个频率创建一个栈即可. class FreqStack { Map<Integer, Integer> map; Lis ...
- C# DataTable 通过Linq分组
datatable我们是经常使用到的,但是需要对数据进行分组,具体代码如下: var result = dt.AsEnumerable().GroupBy(f => new { type = f ...
- c# DLL封装并调用
1.封装自己的dll: a.打开visual studio - 文件 - 新建 - 项目- 类库 - 名称MyTestDll: b.右键Class1.cs - 修改为 TestDll.cs; c.在里 ...
- jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional, 原 Inferred type 'S' for type parameter 'S' is not within its bound;
jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional 在使用springboot 方法报错: Inferred type 'S' for type para ...
- TCHART类型
private Steema.TeeChart.Styles.Pie pieSeries1; private Steema.TeeChart.Styles.Pie pieSeries2; privat ...
- 老老实实学WCF
老老实实学WCF 第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我 ...
- webService上传图片
webService /// <summary> /// 上传图片webServer 的摘要说明 /// </summary> [WebService(Namespace = ...
- spring boot(十三)小技巧
一些springboot小技巧.小知识点 初始化数据 我们在做测试的时候经常需要初始化导入一些数据,如何来处理呢?会有两种选择,一种是使用Jpa,另外一种是Spring JDBC.两种方式各有区别下面 ...
- Django 的逆向解析url--reverse(转)
https://www.cnblogs.com/zhenfei/p/6368955.html Django中提供了一个关于URL的映射的解决方案,你可以做两个方向的使用: 1.有客户端的浏览器发起一个 ...
- 关于export和export default的区别
ES6的模块化中,export与export default都可以用于导出常量.函数.文件.模块等,我们可以通过在其它文件或模块中import(常量.函数.文件.模块)的方式导入,但在一个文件或模块中 ...