2012-04-11 71 views
0

我不得不产生一个条件的类图,条件是这样的:类的设计继承/作曲

水果有颜色和水果可以生长。苹果是水果。 他们有种子。种子可以制造新的苹果。

我想在实施任何设计之前请一些专家建议。

我想一些这样的事情,但我不是专家,所以请使用成分或继承帮我在这里

abstract class Fruit 
{ 
    protected $_color; 

    abstract function grow(); 

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

class Apple extends Fruit 
{ 

} 

如何才能实现这一目标?

+1

听起来像功课,我... – liquorvicar 2012-04-11 06:27:51

+0

但它在工作 – 2012-04-11 06:33:13

+1

给了我作为一个测试工作由我的良师益友,它不是一个功课,如果它适合更好,我将其标记为功课:)(但它的不) – 2012-04-11 07:53:36

回答

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执行,但如果你认为自然的进化是如何工作的,它这很有意义......因为在这种情况下,两种成果具有相同方式增长的共同祖先,因此可以通过在类层次结构中插入该共同祖先来解决问题。

另一种设计是使用DecoratorPattern来描述可以种子的水果,然后用类似new SeedableFruitDecorator(new Apple());的呼叫创建带有种子的苹果。但我认为这不是正确的方法,因为一个苹果总是种子

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; 
} 
} 

:)

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; 
      } 
     } 
    }