0

条件のクラス図を作成する必要があります。条件は次のようなものでした。

果物には色があり、果物は成長することができます。リンゴは果物です。彼らは種を持っています。種は新しいリンゴを作ることができます。

設計を実装する前に、専門家のアドバイスをもらいたいと思います。

こういうことを考えていたのですが、専門家ではないので、ここで手伝ってください

abstract class Fruit
{
    protected $_color;

    abstract function grow();

    protected function seed(Fruit $fruit)
    {
        return $fruit;
    }
}

class Apple extends Fruit
{

}

構成または継承を使用してこれをどのように達成できますか?

4

3 に答える 3

1

種のある果物があります。アップルはそれらの果物の1つです。したがって、単純な単純な継承設計を使用します。

abstract class Fruit
{
     private $color;

     public function getColor(){/* */};
     public function setColor($color){/* */}

     abstract public function grow();
}

abstract class SeedableFruit extends Fruit
{
    public function seed()
    {
        //Do something
    }
}

class Apple extends SeedableFruit
{
    public function grow()
    {
        //Do something
    }
}

SeedableFruitこのように、シードを使用した非インの成長メソッドの実装を再利用することはできませんが、Fruit自然進化がどのように機能するかを考えると、それは理にかなっています...その場合、2つのフルーツには共通の祖先があります。同じように、クラス階層にその共通の祖先を挿入することで問題を解決できます。

別の設計では、DecoratorPatternを使用して、シードを持つことができるフルーツを記述し、次に、のような呼び出しでシードを使用してAppleを作成しましたnew SeedableFruitDecorator(new Apple());しかし、Appleは常にシードを持っているので、それは正しい方法ではないと思います。

于 2012-04-11T08:28:09.803 に答える
0

そのためのオブジェクトを作成しようとすると、次のオブジェクトが思い浮かびます。

abstract class Fruit{
protected $color;
protected $age;
public $isSeedless;

public function getColor(){
return $this->color;
}

public function getAge(){
return $this->age;
}

public function setColor($color){\
$this->color = $color;
}

public function setAge($age){
$this->age = $age;
}

abstract function getFruit();
}

class Apple extends Fruit{
protected $appleCount;

public function __construct(){
$this->isSeedless = false;
}

public function getFruit(){
$fruit = new Apple();
return $fruit;
}
}

:)

于 2012-04-11T06:53:29.870 に答える
0
    // This is what i would do: 
    using System;
    using System.Collections.Generic;

    namespace ApplesAndFruitsDesign
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> colors = new List<string>() { "red" };

                Fruit apple = new Fruit(colors, true, true, 3, "Apple");
                Console.Write("MyFruit Details: Fruit.Name = {0}", apple.FruitName);
                Console.ReadKey();
            }
        }

        public class Fruit
        {
            public List<string> Colors { get; set; }
            public bool CanGrow { get; set; }
            public bool HasSeeds { get; set; }
            public int SeedsCount { get; set; }
            public string FruitName { get; set; }

            public Fruit(List<string> colors, bool canGrow, bool hasSeeds, int seedsCount, string name)
            {
                this.Colors = colors;
                this.CanGrow = canGrow;
                this.HasSeeds = hasSeeds;
                this.SeedsCount = seedsCount;
                this.FruitName = name;
            }

            public List<Fruit> MakeFruitFromSeeds()
            {
                List<Fruit> fruits = new List<Fruit>();
                for (int i = 0; i < this.SeedsCount; i++)
                {
                    Fruit fruit = new Fruit(this.Colors, this.CanGrow, this.HasSeeds, this.SeedsCount, this.FruitName);

                    fruits.Add(fruit);
                }

                return fruits;
            }
        }
    }
于 2012-04-11T13:20:29.137 に答える