简体   繁体   English

如何制作 model-view-controller?

[英]How to make a model-view-controller?

I need to modify my code so that it becomes a model-view-controller.我需要修改我的代码,使其变为 model-view-controller。 Since I'm a total noob when it comes to programming my head aches if I'm honest.因为如果我说实话,在编程方面我完全是个菜鸟。 Any help how to do this would be appreciated.任何帮助如何做到这一点将不胜感激。

For a better understanding I will paste the whole task so you can see what my code is for.为了更好地理解,我将粘贴整个任务,以便您查看我的代码的用途。

*In one pasture there are 200 bustling sheep. *一个牧场里有200只熙熙攘攘的羊。 The flock consists of 95 white sheep, 60 black sheep and 45 white-black sheep.羊群由95只白羊、60只黑羊和45只白黑羊组成。 The shepherd now wants to divide them up so that the corresponding wool can be sheared by color.牧羊人现在想把它们分开,以便可以按颜色剪出相应的羊毛。 Help him write a small script so that he can better order his sheep.帮他写一个小脚本,这样他就可以更好地命令他的羊了。 Please implement the first three points with variables, arrays, mathematical operators and functions.请用变量、arrays、数学运算符和函数实现前三点。 Tip: For a better overview, you can also define your own functions.提示:为了获得更好的概览,您还可以定义自己的函数。

  1. Store the sheep in appropriate groups.将绵羊存放在适当的组中。 Work out a concept that works with variables and arrays.制定一个适用于变量和 arrays 的概念。
  2. Create 200 sheep with the properties name, number of legs, color and assign them to a stable!使用属性名称、腿数、颜色创建 200 只羊并将它们分配给马厩!
  3. Assign the sheep randomly to a number of sheds.*将羊随机分配到多个棚子中。*

Here's my code sample, as I said the following part has to become a model-view-controller.这是我的代码示例,正如我所说,以下部分必须成为 model-view-controller。 How do i do this?我该怎么做呢?

Here is my code:这是我的代码:

<?php
$stableForEachColor = array("stableForWhite", "stableForBlack", "stableForWhiteBlack");
// Function to get the full amount of all sheep
function legs($all)
{
    $legs = 4;
    return $all * $legs;
}

$whiteSheep = 95;
$blackSheep = 60;
$whiteBlackSheep = 45;
$all = $whiteSheep + $blackSheep + $whiteBlackSheep;
echo "At the pasture there are $all sheep in total.\n\n";



$legs = legs($all);
echo "All sheep combined have $legs legs in total.\n\n";

// Names of the white sheep
for ($colorWhite = 1; $colorWhite <= $whiteSheep; $colorWhite++) {
    echo "Wooly $colorWhite is white.\n";
}
// Names of the black sheep
for ($colorBlack = 1; $colorBlack <= $blackSheep; $colorBlack++) {
    echo "Wooly $colorBlack is black.\n";
}

// Names of the white-black sheep
for ($colorWhiteBlack = 1; $colorWhiteBlack <= $whiteBlackSheep; $colorWhiteBlack++) {
    echo "Wooly $colorWhiteBlack is white-black.\n";
}



$stable = array('white_sheep' => $colorWhite, 'black_sheep' => $colorBlack, 'white-black_sheep' => $colorWhiteBlack);
echo '<pre>';
print_r($stable);

$sheep = 200;
$stablesC = 4;
for ($i = $sheep; $i > 0; $i--) {
    $stables[rand(0, ($stablesC - 1))] += 1;
}
echo '<pre>';
print_r($stables);

Most of that task involves just restructuring your code, to separate...well, the M, V and C bits.大多数任务只涉及重构代码,以分离......嗯,M、V 和 C 位。

There is no one true answer, but based on a quick glance i suspect the model part may look roughly like:没有一个真正的答案,但基于快速浏览我怀疑 model 部分可能看起来大致如下:

  • abstract class Sheep {... }抽象 class 羊 {... }
    • with private properties $name, $numberoflegs, $color带有私有属性 $name, $numberoflegs, $color
    • with public function getName() and getLegs() and getColor()与公共 function getName() 和 getLegs() 和 getColor()
    • with public function setName($x) and setLegs($x)与公共 function setName($x) 和 setLegs($x)
    • with public __construct($color, $shed, ?$legs, ?$name) function与公共 __construct($color, $shed, ?$legs, ?$name) function
  • class WhiteSheep extends Sheep {... } class WhiteSheep 扩展绵羊 {... }
    • with public __construct($shed) function与公共 __construct($shed) function
  • class BlackSheep extends Sheep {... } class BlackSheep 扩展绵羊 {... }
    • with public __construct($shed) function与公共 __construct($shed) function
  • class StripedSheep extends Sheep {... } class StripedSheep 延伸绵羊 {... }
    • with public __construct($shed) function与公共 __construct($shed) function
  • abstract class SheepCollection implements Iterator {... }抽象 class SheepCollection 实现 Iterator {... }
    • public function addSheep($sheep)公共 function addSheep($sheep)
    • public function removeSheep($sheep)公共 function removeSheep($sheep)
    • public function transferSheep($sheep, $shed)公共 function transferSheep($sheep, $shed)
    • public function setName($x)公共 function setName($x)
    • public function getName()公共 function getName()
    • public function getLegs()公共 function getLegs()
    • public function getAmountOfSheep()公共 function getAmountOfSheep()
    • private property $name私有财产 $name
    • private array $sheep私有数组 $sheep
    • public __construct($name)公共 __construct($name)
  • class Stable extends SheepCollection {... } class 稳定扩展 SheepCollection {... }
    • with public __construct() (could be a singleton)使用 public __construct() (可能是单例)
  • class Shed extends SheepCollection {... } class Shed 扩展 SheepCollection {... }
    • with public __construct($name)与公共 __construct($name)

and your view layer can likely be very slim (just a couple methods in total).并且您的视图层可能非常纤薄(总共只有几种方法)。

anyway, maybe this gives you a little of an idea on where to start.无论如何,也许这会让您对从哪里开始有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM