[笔试题] 用 PHP 写一个微波炉
在网上看到一个笔试题,感觉挺有意思的,然后我尝试着解一解,欢迎各位大佬指正。
代码题(用OOP的思想编码,注意代码规范) 写出你的思路,最好有代码
用php写一个微波炉,注意物品正加热时不能开门,带皮带壳食物不能被加热。
感谢@MrJing、@minororange两位大佬的建议,更进一步理解了OOP,现更新第二版。
第二版
<?php
/**
* Created by PhpStorm.
* User: arun
* Date: 2019-04-30
* Time: 16:10
*/
/**
* 厨房用具
* Interface kitchenWare
*/
interface kitchenWare {
/**
* 加工食材
* @param Food $food
* @return mixed
*/
public function process(Food $food);
/**
* 是否正在加工
* @return mixed
*/
public function hasProcess();
}
/**
* 微波炉
* Class MicrowaveOven
*/
class MicrowaveOven implements kitchenWare
{
/**
* 是否加热中
* @var bool
*/
protected $is_heat = false;
/**
* @param Food $food
* @return mixed|string
*/
public function process(Food $food)
{
if ($this->hasProcess()) {
return '已有食物在加热,无法打开';
} else {
if ($food->hasShuck() || $food->hasPericarp()) {
return '食物带壳或者带皮,无法进行加热';
} else {
$this->is_heat = true;
return '食物加热中,加热完成即可取出';
}
}
}
/**
* 是否正在加工
* @return bool|mixed
*/
public function hasProcess()
{
return $this->is_heat;
}
}
/**
* 烤箱
* Class Oven
*/
class Oven implements kitchenWare
{
/**
* 是否烧烤中
* @var bool
*/
protected $is_heat = false;
/**
* @param Food $food
* @return mixed|string
*/
public function process(Food $food)
{
if ($this->is_heat) {
return '已有食物在烤制,无法打开';
} else {
if ($food->hasShuck()) {
return '食物带壳,无法进行烤制';
} else {
$this->is_heat = true;
return '食物烤制中,完成即可取出';
}
}
}
/**
* 是否正在加工
* @return bool|mixed
*/
public function hasProcess()
{
return $this->is_heat;
}
}
/**
* 食物
* Class Food
*/
class Food
{
/**
* 是否带壳
* @var bool
*/
protected $is_shuck = false;
/**
* 是否带皮
* @var bool
*/
protected $is_pericarp = false;
/**
* Food constructor.
* @param bool $is_shuck
* @param bool $is_pericarp
*/
public function __construct(bool $is_shuck, bool $is_pericarp)
{
$this->is_shuck = $is_shuck;
$this->is_pericarp = $is_pericarp;
}
/**
* 判断是否带壳
* @return bool
*/
public function hasShuck()
{
return $this->is_shuck;
}
/**
* 判断是否带皮
* @return bool
*/
public function hasPericarp()
{
return $this->is_pericarp;
}
}
/**
* 烹饪
* Class Cooking
*/
class Cooking
{
/**
* @var kitchenWare
*/
protected $kitchenWare;
/**
* Cook constructor.
* @param kitchenWare $kitchenWare
*/
public function __construct(kitchenWare $kitchenWare)
{
$this->kitchenWare = $kitchenWare;
}
/**
* 烹饪食物
* @param Food $food
* @return mixed
*/
public function cooking(Food $food)
{
$data = $this->kitchenWare->process($food);
return $data;
}
}
/**
* 微波炉加热
* @return mixed
*/
function test()
{
$cooking = new Cooking(new MicrowaveOven());
$food = new Food(false, false);
$result = $cooking->cooking($food);
$result2 = $cooking->cooking($food);
var_dump($result, $result2);
}
/**
* 烤箱烤制
* @return mixed
*/
function test2()
{
$cooking = new Cooking(new Oven());
$food = new Food(false, true);
$result = $cooking->cooking($food);
$result2 = $cooking->cooking($food);
var_dump($result, $result2);
}
test();
test2();
第一版
<?php
/**
* Created by PhpStorm.
* User: arun
* Date: 2019-04-30
* Time: 16:10
*/
/**
* 厨房用具
* Interface kitchenWare
*/
interface kitchenWare {
/**
* 加工食材
*
* @param $food
* @return mixed
*/
public function process($food);
}
/**
* 微波炉
* Class MicrowaveOven
*/
class MicrowaveOven implements kitchenWare
{
/**
* 是否加热中
* @var bool
*/
protected $is_heat = false;
public function process($food)
{
if ($this->is_heat) {
return '已有食物在加热,无法打开';
} else {
if ($food['is_shuck'] || $food['is_pericarp']) {
return '食物带壳或者带皮,无法进行加热';
} else {
$this ->is_heat = true;
return '食物加热中,加热完成即可取出';
}
}
}
}
/**
* 烤箱
* Class Oven
*/
class Oven implements kitchenWare
{
/**
* 是否烧烤中
* @var bool
*/
protected $is_heat = false;
public function process($food)
{
if ($this->is_heat) {
return '已有食物在烤制,无法打开';
} else {
if ($food['is_shuck']) {
return '食物带壳,无法进行烤制';
} else {
$this ->is_heat = true;
return '食物烤制中,完成即可取出';
}
}
}
}
/**
* 食物
* Class Food
*/
class Food
{
/**
* 是否带壳
* @var bool
*/
protected $is_shuck = false;
/**
* 是否带皮
* @var bool
*/
protected $is_pericarp = false;
/**
* Food constructor.
* @param bool $is_shuck
* @param bool $is_pericarp
*/
public function __construct(bool $is_shuck, bool $is_pericarp)
{
$this->is_shuck = $is_shuck;
$this->is_pericarp = $is_pericarp;
}
public function getFood()
{
return [
'is_shuck' => $this->is_shuck,
'is_pericarp' => $this->is_pericarp,
];
}
}
/**
* 烹饪
* Class Cooking
*/
class Cooking
{
/**
* @var kitchenWare
*/
protected $kitchenWare;
/**
* Cook constructor.
* @param kitchenWare $kitchenWare
*/
public function __construct(kitchenWare $kitchenWare)
{
$this->kitchenWare = $kitchenWare;
}
/**
* 烹饪食物
* @param $food
* @return mixed
*/
public function cooking($food)
{
$data = $this->kitchenWare->process($food);
return $data;
}
}
/**
* 微波炉加热
* @return mixed
*/
function test()
{
$cooking = new Cooking(new MicrowaveOven());
$food = new Food(false, false);
$result = $cooking->cooking($food->getFood());
$result2 = $cooking->cooking($food->getFood());
var_dump($result, $result2);
}
/**
* 烤箱烤制
* @return mixed
*/
function test2()
{
$cooking = new Cooking(new Oven());
$food = new Food(false, true);
$result = $cooking->cooking($food->getFood());
$result2 = $cooking->cooking($food->getFood());
var_dump($result, $result2);
}
test();
test2();
本作品采用《CC 协议》,转载必须注明作者和本文链接
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
文章来源blog.arunfung.com
本帖由系统于 5年前 自动加精
高认可度评论:
在
interface
中的process
和Cooking
对象的cooking
中限制$food
变量的类型:参照 @MrJing 的建议,在
Food
对象中定义两个方法最后调用:
传递对象比传递数组要好一点,因为传递数组,在使用的时候有可能数组里面没有这个元素,容易出现 bug 。
传递对象的话,
if ($food['is_shuck'])
改成$food->hasShuck()
,一是更加语义化,二是避免上述 bug。赞一个,一看到这个题目我直接暴躁了,以为是热饭那个微博炉呢
@wonbin :grinning: 哈哈,是热饭的那个微波炉啊,有那么一点标题党,只是我个人对 oop 的一点理解,希望可以帮助更多人
:joy: :joy: :joy:,还真是微波炉呢。
Food 声明为一个接口,有2个方法:hasShuck、hasPericarp。这样就感觉更“OOP”一点了
在
interface
中的process
和Cooking
对象的cooking
中限制$food
变量的类型:参照 @MrJing 的建议,在
Food
对象中定义两个方法最后调用:
传递对象比传递数组要好一点,因为传递数组,在使用的时候有可能数组里面没有这个元素,容易出现 bug 。
传递对象的话,
if ($food['is_shuck'])
改成$food->hasShuck()
,一是更加语义化,二是避免上述 bug。非常有意思的题目
你好,有点想不明白,为什么test 方法中用调用两次$cooking->cooking($food)
@Leo_Phoenix 你好,这主要想表达的是正在加热,无法开门的情况。
改了下。挺有意思的 :joy:
你看这微波炉它又大又圆
else用的有些多呀,有些可以去掉
@xun1009 :smiley:如果有想法,可以简单给出建议,我后续更新进文章中,thanks
@锋子 :+1:改进很好的扩展了食物类,但是没有根据题意做出改进,稍稍有点偏题额,哈哈, :smiley:不过还是鼓励多方面思考,尝试更优解的实现。
可以再帮我写个电饭锅么
刚吃完午饭..
下划线命名差评
@arunfung 哈哈偷了个懒。觉得有意思就简单试了下,嘿嘿。
这绝对是个超级人工智能的微波炉,要是当年我用微波炉加热鸡蛋的时候是这个款微波炉。。。呃,也不用那么的糗了 :flushed:
哈哈 一开始差点暴躁了
你好,PHP进程挂了,我要热饭
提供一种思路, 用状态机实现 可以动态更改状态,这个是代码关键 代码如下 (只写了大概 可以跑 但还是可以优化的)
下班抽空搞的 写的有点子粗糙 仅提供一种思路吧
状态目前只有 Open, Heat, Close 有需要可以扩展其他状态,这套维护性和拓展性都还好,不过会冗余一些东西,关键在于它的状态可以动态调整,按需使用,仅供参考。
装饰者模式?最近在看 Head First 设计模式,感觉和装饰者模式有些类似
才知道微波炉不能加热带壳带皮的东西 :stuck_out_tongue_closed_eyes:
早餐靠他了
@aoxiang594 :smile:哈哈,赶紧东西热起来