ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# (\*)工厂模式 工厂模式的作用: 避免直接实例化对象,如果我们在很多地方都要实例化此对象,但是某一天对象的名字需要更改时,所有实例化的地方都要修改,而有了工厂模式就只修改工厂模式代码里的类名就可以了 ****简单工厂--静态工厂方法模式**** > 我们把被创建的对象称为“产品”,把创建产品的对象称为“工厂”。如果要创建的产品不多,只要一个工厂类就可以完成 > 它的缺点是增加新产品时会违背“开闭原则” ``` <pre class="calibre10">``` <span class="token">//抽象产品 </span> interface <span class="token4">Fruit</span> <span class="token3">{</span> # 种植 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 收获 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">//具体产品实现 </span> class <span class="token4">Apple</span> implements <span class="token4">Fruit</span> <span class="token3">{</span> # 种植 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"苹果种下啦"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"苹果收获啦"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">Banana</span> implements <span class="token4">Fruit</span> <span class="token3">{</span> # 种植 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"香蕉种下啦"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"香蕉收获啦"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//简单工厂--静态工厂方法模式</span> class <span class="token4">SimpleFactory</span> <span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">getFruitByParam</span><span class="token3">(</span>$type<span class="token3">)</span> <span class="token3">{</span> switch <span class="token3">(</span>$fruitName<span class="token3">)</span> <span class="token3">{</span> case <span class="token2">'apple'</span><span class="token3">:</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Apple</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> case <span class="token2">'banana'</span><span class="token3">:</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Banana</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> default<span class="token3">:</span> <span class="token5">throw</span> <span class="token5">new</span> <span class="token4">Exception</span><span class="token3">(</span><span class="token2">"Error no the fruit"</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">// 如果不用工厂模式,则需要提前指定具体类 </span> <span class="token">// $fruit= new Apple(); </span> <span class="token">// echo $fruit->plant(); </span> <span class="token">// $fruit= new Banana(); </span> <span class="token">// echo $fruit->plant(); </span> <span class="token">// 用工厂模式,则不需要知道对象由什么类产生,交给工厂去决定 </span> $fruit <span class="token1">=</span> SimpleFactory<span class="token3">:</span><span class="token3">:</span><span class="token4">getFruitByParam</span><span class="token3">(</span><span class="token2">'Apple'</span><span class="token3">)</span><span class="token3">;</span> echo $fruit <span class="token1">-</span><span class="token1">></span><span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $fruit <span class="token1">=</span> SimpleFactory<span class="token3">:</span><span class="token3">:</span><span class="token4">getFruitByParam</span><span class="token3">(</span><span class="token2">'Banana'</span><span class="token3">)</span><span class="token3">;</span> echo $fruit <span class="token1">-</span><span class="token1">></span><span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ****工厂方法--多态性工厂模式②**** > **主要优点有:** > > - 用户只需要知道具体工厂的名称就可得到所要的产品,无须知道产品的具体创建过程; > - 在系统增加新的产品时只需要添加具体产品类和对应的具体工厂类,无须对原工厂进行任何修改,满足开闭原则; > > **其缺点是:** > > - 每增加一个产品就要增加一个具体产品类和一个对应的具体工厂类,这增加了系统的复杂度。 ![](https://img.kancloud.cn/37/74/3774f86ccb714b82e76d9e6ddcc66d6b_810x394.png) ``` <pre class="calibre10">``` <span class="token">/** * 虚拟产品接口类 * 定义好需要实现的方法 */</span> interface <span class="token4">Fruit</span><span class="token3">{</span> # 种植 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 生长 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 收获 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 吃 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 定义具体产品类 苹果 * 首先,我们要实现所继承的接口所定义的方法 * 然后定义苹果所特有的属性,以及方法 */</span> class <span class="token4">Apple</span> implements <span class="token4">Fruit</span><span class="token3">{</span> # 苹果树有年龄 private $treeAge<span class="token3">;</span> # 苹果有颜色 private $color<span class="token3">;</span> # 种植 继承fruit接口 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape plant"</span><span class="token3">;</span> <span class="token3">}</span> # 生长 继承fruit接口 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape grow"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 继承fruit接口 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape harvest"</span><span class="token3">;</span> <span class="token3">}</span> # 吃 继承fruit接口 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape eat"</span><span class="token3">;</span> <span class="token3">}</span> # 取苹果树的年龄 public <span class="token5">function</span> <span class="token4">getTreeAge</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>treeAge<span class="token3">;</span> <span class="token3">}</span> # 设置苹果树的年龄 public <span class="token5">function</span> <span class="token4">setTreeAge</span><span class="token3">(</span>$age<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>treeAge <span class="token1">=</span> $age<span class="token3">;</span> <span class="token5">return</span> trie<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 定义具体产品类 葡萄 * 首先,我们要实现所继承的接口所定义的方法 * 然后定义葡萄所特有的属性,以及方法 */</span> class <span class="token4">Grape</span> implements <span class="token4">Fruit</span><span class="token3">{</span> # 葡萄是否有籽 private $seedLess<span class="token3">;</span> # 种植 继承fruit接口 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape plant"</span><span class="token3">;</span> <span class="token3">}</span> # 生长 继承fruit接口 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape grow"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 继承fruit接口 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape harvest"</span><span class="token3">;</span> <span class="token3">}</span> # 吃 继承fruit接口 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape eat"</span><span class="token3">;</span> <span class="token3">}</span> # 获取葡萄有籽无籽 public <span class="token5">function</span> <span class="token4">getSeedLess</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>seedLess<span class="token3">;</span> <span class="token3">}</span> # 设置葡萄有籽无籽 public <span class="token5">function</span> <span class="token4">setTreeAge</span><span class="token3">(</span>$age<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>treeAge <span class="token1">=</span> $age<span class="token3">;</span> <span class="token5">return</span> trie<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 农场主<span class="token4">Farmer</span><span class="token3">(</span>工厂<span class="token3">)</span>类接口 用来获取实例化的水果 interface <span class="token4">CommFactory</span> <span class="token3">{</span> # 定义个静态工厂方法 public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> # 苹果农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">AppleFarm</span> implements <span class="token4">CommFactory</span><span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Apple</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 葡萄农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">GrapeFarm</span> implements <span class="token4">CommFactory</span><span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Grape</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 调用 $appleFarm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">AppleFarm</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $apple<span class="token1">=</span>$appleFarm<span class="token1">-</span><span class="token1">></span><span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $apple<span class="token1">-</span><span class="token1">></span><span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $grapeFarm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">GrapeFarm</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $grape<span class="token1">=</span>$grapeFarm<span class="token1">-</span><span class="token1">></span><span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ****抽象工厂--工具箱模式(ToolKit):**** 使用抽象工厂模式一般要满足以下条件。 - 系统中有多个产品族,每个具体工厂创建同一族但属于不同等级结构的产品。 - 系统一次只可能消费其中某一族产品,即同族的产品一起使用。 抽象工厂模式除了具有工厂方法模式的优点外,其他主要优点如下。 - 可以在类的内部对产品族中相关联的多等级产品共同管理,而不必专门引入多个新的类来进行管理。 - 当增加一个新的产品族时不需要修改原代码,满足开闭原则。 其缺点是:当产品族中需要增加一个新的产品时,所有的工厂类都需要进行修改。 ![](https://img.kancloud.cn/3a/9b/3a9b701a75e0ba7971e862d89a354b3f_662x635.png) ``` <pre class="calibre17">``` <span class="token">/** * 虚拟产品接口类 * 定义好需要实现的方法 */</span> interface <span class="token4">Fruit</span><span class="token3">{</span> # 种植 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 生长 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 收获 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> # 吃 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 定义具体产品类 苹果 * 首先,我们要实现所继承的接口所定义的方法 * 然后定义苹果所特有的属性,以及方法 */</span> class <span class="token4">Apple</span> implements <span class="token4">Fruit</span><span class="token3">{</span> # 苹果树有年龄 private $treeAge<span class="token3">;</span> # 苹果有颜色 private $color<span class="token3">;</span> # 种植 继承fruit接口 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape plant"</span><span class="token3">;</span> <span class="token3">}</span> # 生长 继承fruit接口 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape grow"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 继承fruit接口 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape harvest"</span><span class="token3">;</span> <span class="token3">}</span> # 吃 继承fruit接口 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape eat"</span><span class="token3">;</span> <span class="token3">}</span> # 取苹果树的年龄 public <span class="token5">function</span> <span class="token4">getTreeAge</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>treeAge<span class="token3">;</span> <span class="token3">}</span> # 设置苹果树的年龄 public <span class="token5">function</span> <span class="token4">setTreeAge</span><span class="token3">(</span>$age<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>treeAge <span class="token1">=</span> $age<span class="token3">;</span> <span class="token5">return</span> trie<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 定义具体产品类 葡萄 * 首先,我们要实现所继承的接口所定义的方法 * 然后定义葡萄所特有的属性,以及方法 */</span> class <span class="token4">Grape</span> implements <span class="token4">Fruit</span><span class="token3">{</span> # 葡萄是否有籽 private $seedLess<span class="token3">;</span> # 种植 继承fruit接口 public <span class="token5">function</span> <span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape plant"</span><span class="token3">;</span> <span class="token3">}</span> # 生长 继承fruit接口 public <span class="token5">function</span> <span class="token4">grow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape grow"</span><span class="token3">;</span> <span class="token3">}</span> # 收获 继承fruit接口 public <span class="token5">function</span> <span class="token4">harvest</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape harvest"</span><span class="token3">;</span> <span class="token3">}</span> # 吃 继承fruit接口 public <span class="token5">function</span> <span class="token4">eat</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"grape eat"</span><span class="token3">;</span> <span class="token3">}</span> # 获取葡萄有籽无籽 public <span class="token5">function</span> <span class="token4">getSeedLess</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>seedLess<span class="token3">;</span> <span class="token3">}</span> # 设置葡萄有籽无籽 public <span class="token5">function</span> <span class="token4">setTreeAge</span><span class="token3">(</span>$age<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>treeAge <span class="token1">=</span> $age<span class="token3">;</span> <span class="token5">return</span> trie<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> #业务扩张 农场遍布不同区域 interface <span class="token4">Area</span><span class="token3">{</span> public <span class="token5">function</span> <span class="token4">showArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> class <span class="token4">SichuanArea</span> implements <span class="token4">Area</span><span class="token3">{</span> public <span class="token5">function</span> <span class="token4">showArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"四川地区"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">GuizhouArea</span> implements <span class="token4">Area</span><span class="token3">{</span> public <span class="token5">function</span> <span class="token4">showArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo <span class="token2">"贵州地区"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 抽象农场主<span class="token4">Farmer</span><span class="token3">(</span>工厂<span class="token3">)</span> interface <span class="token4">AbstractFactory</span> <span class="token3">{</span> # 定义个静态工厂方法 public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> # 四川的苹果农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">SichuanAppleFarm</span> implements <span class="token4">AbstractFactory</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Apple</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">SichuanArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 四川的葡萄农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">SichuanGrapeFarm</span> implements <span class="token4">AbstractFactory</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Grape</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">SichuanArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 贵州的葡萄农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">GuizhouGrapeFarm</span> implements <span class="token4">AbstractFactory</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Grape</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">GuizhouArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 贵州的苹果农场<span class="token3">(</span>工厂<span class="token3">)</span> class <span class="token4">GuizhouAppleFarm</span> implements <span class="token4">AbstractFactory</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">Apple</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> <span class="token5">new</span> <span class="token4">GuizhouArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> # 调用 $gaf<span class="token1">=</span><span class="token5">new</span> <span class="token4">GuizhouAppleFarm</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//贵州葡萄园</span> $gaf<span class="token1">-</span><span class="token1">></span><span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token1">-</span><span class="token1">></span><span class="token4">showArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $gaf<span class="token1">-</span><span class="token1">></span><span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token1">-</span><span class="token1">></span><span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $sgf<span class="token1">=</span> <span class="token5">new</span> <span class="token4">SichuanGrapeFarm</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//四川葡萄园</span> $sgf<span class="token1">-</span><span class="token1">></span><span class="token4">getArea</span><span class="token3">(</span><span class="token3">)</span><span class="token1">-</span><span class="token1">></span><span class="token4">showArea</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $sgf<span class="token1">-</span><span class="token1">></span><span class="token4">getFruit</span><span class="token3">(</span><span class="token3">)</span><span class="token1">-</span><span class="token1">></span><span class="token4">plant</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ```