簡體   English   中英

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

[英]What's the best way to complete this task in php oop

規格

我被要求創建一個新的PHP類庫(任何PHP版本)以幫助我管理魚缸。 該庫應滿足以下用戶案例並證明測試能力。

以下是用戶案例:

•用戶應能夠在水箱中添加3種魚(金魚,天使魚,通天魚)並命名魚•用戶應能夠使用Feed()查看將多少魚放入水箱中方法。 這應該返回所需魚類食物總量的克數。
•用戶應該能夠將魚缸中的魚的數據保存到JSON文件中。•確保設計使我將來可以添加更多類型的魚,而不必更改魚缸庫。 o每只金魚0.1克o每只天使魚0.2克o每只通天魚0.3克

我的嘗試

## 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();

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

提前道歉成為新手

OOP涉及負責(實際)事物的類。 Tank課就是這樣,沒關系。

但是, Fish永遠不會成為Tank的子類,因為它們與幾乎相同的東西並不相似。 Gold FishAngel Fish或者Babel Fish可能是子類Fish ,因為他們實際上是魚不僅僅是一條魚更詳細一點的信息。 Fish不是詳細的Tank ,所以它不能是子類。

我將通過執行以下操作來重構您的代碼:

  1. Fish成為自己的課程,讓Gold FishAngel FishBabel Fish擴展它
  2. 在您的Tank類中添加一個字段,以跟蹤其中包含哪些Fish 例如: protected $fishInTank = Array();
  3. 添加一種方法,使您可以在魚缸中添加和刪除魚(從而在數組中添加一條記錄!):

     public addFishToTank(Fish $fish) { array_push($fishInTank, $fish); } 

在這之后當然還有很多步驟,但是我希望我為您設定了正確的方向。 首先,OOP有點麻煩,並且編寫代碼通常會花費更多精力。 另一方面,您的代碼可讀性強,如果操作正確,則很容易擴展和維護。

祝好運! (如果有用的話,請不要忘記勾選它;))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM