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. 动态规划-击爆气球 Burst Balloons

    2018-10-03 19:29:43 问题描述: 问题求解: 很有意思的题目,首先想到的是暴力遍历解空间,当然也用到了memo,可惜还是TLE,因为时间复杂度确实有点过高了,应该是O(n!). Ma ...

  2. Windows 7 Update Settings Disabled (Important Updates Grayed Out)

    This worked for me: 1) Hold WindowsKey + R     (is hold Start & press R on your keyboard) 2) Typ ...

  3. 停止学习框架(Stop Learning Frameworks)

    https://www.cnblogs.com/strick/p/10161733.html

  4. CentOS优化

    一.CentOS6.x优化 #.更改yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...

  5. LeetCode--342--4的幂

    问题描述: 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶:你能不使用 ...

  6. 用ActionController::Renderer的render方法渲染模版

    使用Cable进行pub: ActionCable.server.broadcast "call", {address: AddressesController.render(@a ...

  7. Confluence 6 管理和恢复空间管理权限

    管理和恢复空间管理权限 可能有些空间的空间管理权限被系统的超级管理删除掉了.这样的空间是没有任何空间管理员的,用户和用户组都不能对空间进行管理.只有 Confluence 管理员权限的用户可以删除一个 ...

  8. windows系统文件和linux系统文件

    windows系统文件和linux系统文件 1.单用户操作系统和多用户操作系统 单用户操作系统:指一台计算机在同一时间 只能由一个用户 使用,一个用户独自享用系统的全部硬件和软件资源 Windows ...

  9. CentOS6.8下实现配置配额

    CentOS6.8下实现配置配额 Linux系统是支持多用户的,即允许多个用户同时使用linux系统,普通用户在/home/目录下均有自己的家目录,在默认状态下,各个用户可以在自己的家目录下任意创建文 ...

  10. leetcode-algorithms-2 Add Two Numbers

    leetcode-algorithms-2 Add Two Numbers You are given two non-empty linked lists representing two non- ...