lintcode:玩具工厂
题目
工厂模式是一种常见的设计模式。请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类。可以假设只有猫和狗两种玩具。
ToyFactory tf = ToyFactory();
Toy toy = tf.getToy('Dog');
toy.talk();
>> Wow
toy = tf.getToy('Cat');
toy.talk();
>> Meow
解题
/**
* 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
public void talk(){
System.out.println("Wow");
}
} class Cat implements Toy {
// Write your code here
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
if(type.equals("Dog")){
return new Dog();
}
if(type.equals("Cat")){
return new Cat();
}
return null;
}
}
lintcode:玩具工厂的更多相关文章
- [LintCode] Toy Factory 玩具工厂
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
- lintcode-496-玩具工厂
496-玩具工厂 工厂模式是一种常见的设计模式.请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类.可以假设只有猫和狗两种玩具. 您在真实的面试中是否遇到过这个题? Yes 样例 ToyF ...
- BI数据分析中KPI,KGI,CSF概念
1. 行为产生数据 先来谈一谈,自己对数据基础概念的思考.我认为首先要建立的核心观点是:行为产生数据. 翻译一下这个核心观点.意思就是,当我们在思考或描述数据相关需求的时候,必然要包含这样的语素:&q ...
- CountDownLatch 计数器
这里我暂时只讲CountDownLatch的作用和怎么使用,至于他是怎么实现这种功能的,涉及源码,以后我再补上. 正文 什么是CountDownLatch? CountDownLatch是在java1 ...
- 【Go语言入门系列】(九)写这些就是为了搞懂怎么用接口
[Go语言入门系列]前面的文章: [Go语言入门系列](六)再探函数 [Go语言入门系列](七)如何使用Go的方法? [Go语言入门系列](八)Go语言是不是面向对象语言? 1. 引入例子 如果你使用 ...
- [LintCode] Shape Factory 形状工厂
Factory is a design pattern in common usage. Implement a ShapeFactory that can generate correct shap ...
- lintcode:形状工厂
题目 工厂模式是一种常见的设计模式.实现一个形状工厂 ShapeFactory 来创建不同的形状类.这里我们假设只有三角形,正方形和矩形三种形状. 样例 ShapeFactory sf = new S ...
- .NET设计模式(5):工厂方法模式(Factory Method)(转)
工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...
- .NET设计模式(5):工厂方法模式(Factory Method)
):工厂方法模式(Factory Method) 工厂方法模式(Factory Method) --.NET设计模式系列之五 Terrylee,2004年1月2日 转载:http://terry ...
随机推荐
- iOS中远程推送实现—在Apple的生产环境上测试Push Notifications功能
1.在“Provisioning Profiles”中点击“Add”按钮. 2.在“What type of provisioning profile do you need?”页面中选择“Distr ...
- mysql substring_index
select * from tablename where substring_index(field1,'_',-1)=‘abc' #表中field1的值结构为123_abc
- Google Volley: How to send a POST request with Json data?
sonObjectRequest actuallyaccepts JSONObject as body. From http://arnab.ch/blog/2013/08/asynchronous- ...
- 20145103JAVA第一次实验报告
20145103<Java程序设计>第一次实验报告 实验内容及其步骤 一.命令行下java程序开发 建立一个java文件,然后在命令行中,对程序进行javac编译,就生成了.class文件 ...
- /lib64/libc.so.6: version `GLIBC_2.14' not found问题
<备忘> 参考文章: https://my.oschina.net/zhangxu0512/blog/262275 问题答疑: http://blog.sina.com.cn/s/blog ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Search for a Range】cpp
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- X86架构CPU的逻辑原理
本篇只是初略介绍X86的逻辑运行原理,并不涉及物理层面和汇编层面的知识. 一.冯洛伊曼体系的运作过程: 1.CPU的历史就不扯了,有兴趣的朋友可以网上搜一下. 2.X86CPU是基于冯洛伊曼架构体系, ...
- python 数据结构-列表
列表常用方法汇总: #定义列表li li=[12.23,456,88,9] a=[1,2,3] #添加元素到列表结尾 li.append(360) #追加列表元素extend(L) li.extend ...
- Sprite Kit 入门教程
Sprite Kit 入门教程 Ray Wenderlich on September 30, 2013 Tweet 这篇文章还可以在这里找到 英语, 日语 If you're new here, ...