在php oop中完成此任务的最佳方法是什么

时间:2016-06-09 13:12:09

标签: php oop

SPECIFICATION

我被要求创建一个新的PHP类库(任何PHP版本)来帮助我管理我的鱼缸。该库应满足以下用户故事并展示测试能力。

以下是用户故事:

•用户应该能够在鱼缸中添加3种鱼类(金鱼,天使鱼,巴贝尔鱼)并命名鱼 •用户应该能够通过Feed()方法查看罐中放入多少食物。     这应该返回所需鱼类总量的重量克数 •用户应该能够将有关鱼缸中鱼的数据保存到JSON文件中 •确保设计允许我将来添加更多类型的鱼,而无需更换油箱库。     o每条金鱼0.1克     o每只天使鱼0.2克     每只巴贝尔鱼0.3克

MY ATTEMPT

## CLASSES ##

    # Tank Class (Parent) // # DEFINES EMPTY TANK
class Tank {
    # Member variables
    // Set default unique ID (allows duplicates of Name)
    protected $ID = 'undefined';
    // Set default Type
    protected $type = 'notype';
    // Set default Name
    protected $name = 'unnamed';
    // Set default Appetite
    protected $appetite = 0;        
}//end class Tank
# Fish Class (Child of Tank) // # DEFINE FISH TYPES (GROUP)
class Fish extends Tank {
    # Member functions
    // setting ID
    function setID($par){$this->ID = $par;}//end function
    // getting ID
    function getID(){$FishID = $this->ID;}//end function
    // setting Type
    function setType($par){$this->type = $par;}//end function
    // getting Type
    function getType(){$FishType = $this->type;}//end function
    // setting Name
    function setName($par){$this->name = $par;}//end function
    // getting Name
    function getName(){$FishName = $this->name;}//end function
    // setting Appetite
    function setAppetite($par){$this->appetite = $par;}//end function
    // getting Appetite
    function getAppetite(){$FishAppetite = $this->appetite;}//end function
}//end class Fish
$Tank = array();

## Functions ##
# Print Array Function // FOR TESTING
function see($array){print('<pre>');print_r($array);print('</pre>');}//end Function

# Add Fish Function 
function Add($Fish){
    # Instantiating our Fish
    $NewFish = new Fish;
    # Giving our Fish a unique ID
    $NewFish->setID( $Fish['ID'] );
    # Naming our Fish
    $NewFish->setName( $Fish['Name'] );
    # Classifying our Fish
    $NewFish->setType( $Fish['Type'] );
    # Preparing our Fish's Diet
    $NewFishAppetite = $Fish['Appetite'];
    # Setting Diets for our fish
    $NewFish->setAppetite( $Fish['Appetite'] );
    # Display New Fish Data
    see($NewFish);
    # Return New Fish Data
    return $NewFish;
}//end Function AddNewFish
# Feed Function 
function Feed($TankData){
    // Set Default amount of fish food at zero
    $FoodAccumulator = 0;
    // set array counter
    $counter = 0;
    // count how many array items
    $counted = count($TankData);
    // while counter is less than counted...
    while ($counter < $counted){
        // add each Appetite value to the Acuumulator
        $FoodAccumulator = ($FoodAccumulator + $TankData[$counter]['Appetite']);
        // increment counter
        $counter++;
    }//end while
    //return the weight in grams of the total required fish food
    return $FoodAccumulator.' grams';
}//end function Feed()
# Write json Function 
function jsonWrite($jsonMyFile,$Array){
    file_put_contents($jsonMyFile,json_encode($Array));
}// end jsonWrite Function
# Read json Function 
function jsonRead($jsonMyFile){
    // copy file content into a string var
    $json_file = file_get_contents($jsonMyFile);
    // convert the string to a json object
    $JsonFileObject = json_decode($json_file);
    // return the json object
    return $JsonFileObject;
}//end jsonRead Function




// let's build the Tank and add some fish...
// DEFINING SINGLE FISH
########################################################################################
### New Fish Template ###
// Design Fish
$Fish = array("ID"=>"0","Name"=>"Test Fish","Type"=>"TestFish","Appetite"=>"0.9");
// Add Fish to Tank
Add($Fish);
// Update Tank Data
array_push($Tank,$Fish);
########################################################################################


### New Fish 1 ###
$Fish = array("ID"=>"1","Name"=>"Mr Gold","Type"=>"Goldfish","Appetite"=>"0.1");// Design
Add($Fish);// Add
array_push($Tank,$Fish);// Update
### New Fish 2 ###
$Fish = array("ID"=>"2","Name"=>"Miss Angel","Type"=>"Angelfish","Appetite"=>"0.2");// Design
Add($Fish);// Add
array_push($Tank,$Fish);// Update
### New Fish 3 ###
$Fish = array("ID"=>"3","Name"=>"Hitch","Type"=>"Babelfish","Appetite"=>"0.3");// Design
Add($Fish);// Add
array_push($Tank,$Fish);// Update


// CALCULATE TOTAL FEED
echo '<hr/>'.Feed($Tank).'<hr/>';

php // json file i/o
// define json file
$jsonMyFile = 'tank.json';// chmod 0666

// write Tank data to json file
echo '<hr/>writing json<hr/>';
jsonWrite($jsonMyFile,$Tank);

// TESTING
// read Tank data from json file
echo '<hr/>reading json<hr/>';
see(jsonRead($jsonMyFile));

php exit();

###############################

提前为新手道歉

1 个答案:

答案 0 :(得分:1)

OOP是关于负责(真实世界)事物的类。 Tank类是这样的,所以没关系。

但是Fish永远不能成为Tank的子类,因为它们几乎不是同一个东西。 Gold FishAngel FishBabel Fish可以是Fish的子类,因为它们实际上只是鱼,而不仅仅是一条鱼。 Fish不详细Tank,因此它不能成为子类。

我会通过执行以下操作来重构您的代码:

  1. Fish成为自己的班级,让Gold FishAngel FishBabel Fish扩展它
  2. Tank课程中添加一个字段,用于跟踪其中Fish个字段。例如:protected $fishInTank = Array();
  3. 添加一种方法,可让您添加&amp;删除鱼到你的坦克(从而为你的数组添加一条记录!):

    public addFishToTank(Fish $fish)
    {
        array_push($fishInTank, $fish);
    }
    
  4. 此后有很多步骤,但我希望我能让你走上正确的道路。 OOP起初有点麻烦,编码通常需要更多努力。另一方面,您的代码非常易读,如果操作正确,非常容易扩展和维护。

    祝你好运! (如果有帮助,请不要忘记作为答案;)