使用特征、接口和抽象类增强面向对象的设计
来源:dev.to
时间:2024-07-30 08:03:49 356浏览 收藏
小伙伴们有没有觉得学习文章很有意思?有意思就对了!今天就给大家带来《使用特征、接口和抽象类增强面向对象的设计》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!
在面向对象编程中,保持简洁和模块化设计对于创建可扩展和可维护的应用程序至关重要。通过利用设计模式和原则,开发人员可以创建灵活且易于扩展的代码。本文探讨了如何使用特征、接口和抽象类来增强您的设计,重点关注数据传输对象 (dto) 作为实际示例。
了解基础知识:特征、接口和抽象类
特质:
traits 是 php 等单继承语言中代码重用的机制。它们允许您定义可在多个类中使用的方法,从而促进代码重用,而无需继承。
接口:
接口定义了类必须遵守的契约。它们指定类必须实现哪些方法,确保一致性并允许多态性。
抽象类:
抽象类提供了其他类可以扩展的基类。它们可以包括抽象方法(必须由子类实现)和具体方法(可以按原样使用或重写)。
实际示例:在 dto 中实现特征、接口和抽象类
为了说明特征、接口和抽象类如何协同工作,让我们使用数据传输对象 (dto) 的示例。 dto 用于在应用程序的不同层之间传输数据,而不包含业务逻辑。我们将利用这些面向对象的原则创建一个灵活且可维护的 dto 系统。
1. 定义抽象基类
basedto 抽象类为所有 dto 提供通用功能,例如将数据转换为数组或 json 格式以及从数组初始化。
app/dto/basedto.php
namespace app\dto; /** * abstract class basedto * * provides common functionality for data transfer objects (dtos). */ abstract class basedto { /** * basedto constructor. * * @param array $data initial data to populate the dto. */ public function __construct(array $data = []) { $this->setfromarray($data); } /** * convert the dto to an array. * * @return array the dto as an associative array. */ public function toarray(): array { $properties = get_object_vars($this); return array_filter($properties, function ($property) { return $property !== null; }); } /** * convert the dto to a json string. * * @return string the dto as a json string. */ public function tojson(): string { return json_encode($this->toarray()); } /** * set the dto properties from an array. * * @param array $data the data to set on the dto. */ protected function setfromarray(array $data): void { foreach ($data as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } } }
2. 创建特定接口
接口定义了我们的dto根据不同的数据源(例如模型、api、csv文件)需要实现的具体方法。
app/contracts/dto/setfrommodel.php
/** * interface setfrommodel * * defines a method for setting dto properties from a model. */ interface setfrommodel { /** * set dto properties from a model. * * @param mixed $model the model to set properties from. * @return self */ public function setfrommodel($model): self; }
app/contracts/dto/setfromapi.php
/** * interface setfromapi * * defines a method for setting dto properties from api data. */ interface setfromapi { /** * set dto properties from api data. * * @param array $data the api data to set properties from. * @return self */ public function setfromapi(array $data): self; }
app/contracts/dto/setfromcsv.php
/** * interface setfromcsv * * defines a method for setting dto properties from csv data. */ interface setfromcsv { /** * set dto properties from csv data. * * @param array $data the csv data to set properties from. * @return self */ public function setfromcsv(array $data): self; }
3. 实现可重用性特征
traits 允许我们定义可重用的方法来设置来自不同来源的数据,从而可以轻松地在不同的 dto 之间共享功能。
app/traits/dto/setfrommodeltrait.php
namespace app\traits\dto; trait setfrommodeltrait { public function setfrommodel($model): self { foreach (get_object_vars($model) as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } return $this; } }
app/traits/dto/setfromapitrait.php
namespace app\traits\dto; /** * trait setfrommodeltrait * * provides a method for setting dto properties from a model. */ trait setfrommodeltrait { /** * set dto properties from a model. * * @param mixed $model the model to set properties from. * @return self */ public function setfrommodel($model): self { foreach (get_object_vars($model) as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } return $this; } }
app/traits/dto/setfromcsvtrait.php
namespace app\traits\dto; /** * trait setfromcsvtrait * * provides a method for setting dto properties from csv data. */ trait setfromcsvtrait { /** * set dto properties from csv data. * * @param array $data the csv data to set properties from. * @return self */ public function setfromcsv(array $data): self { // assuming csv data follows a specific structure $this->name = $data[0] ?? null; $this->address = $data[1] ?? null; $this->price = isset($data[2]) ? (float)$data[2] : null; $this->subscription = $data[3] ?? null; $this->assets = isset($data[4]) ? explode(',', $data[4]) : []; return $this; } }
4. 实现具体的dto类
最后,实现利用抽象类、接口和特征的具体 propertydto 类。
namespace App\DTO; use App\Contracts\SetFromModel; use App\Contracts\SetFromAPI; use App\Contracts\SetFromCSV; use App\DTO\Traits\SetFromModelTrait; use App\DTO\Traits\SetFromAPITrait; use App\DTO\Traits\SetFromCSVTrait; /** * Class PropertyDTO * * Represents a Property Data Transfer Object. */ readonly class PropertyDTO extends BaseDTO implements SetFromModel, SetFromAPI, SetFromCSV { use SetFromModelTrait, SetFromAPITrait, SetFromCSVTrait; /** * @var string The name of the property. */ public string $name; /** * @var string The address of the property. */ public string $address; /** * @var float The price of the property. */ public float $price; /** * @var ?string The subscription type of the property. */ public ?string $subscription; /** * @var ?array The assets of the property. */ public ?array $assets; // Other specific methods can be added here }
使用特征、接口和抽象类的最佳实践
行为封装:使用traits封装常见的行为,可以跨多个类复用,减少重复,提高可维护性。
定义清晰的契约:接口应该为类必须实现的方法定义清晰的契约,确保一致性并允许轻松交换实现。
提供基础功能:抽象类提供共享功能的基础,允许子类根据需要进行扩展和自定义,同时保持通用结构。
增强灵活性:结合这些技术可以实现灵活的设计,其中类可以仅实现必要的接口并使用相关特征,从而更容易扩展和调整代码。
保持一致性:通过使用抽象类和特征,您可以确保代码保持一致并遵循可预测的模式,这对于长期可维护性至关重要。
结论
将特征、接口和抽象类集成到您的设计中提供了一种管理和构建代码的强大方法。通过应用这些原则,您可以创建一个模块化、可维护且可扩展的系统,该系统遵循面向对象编程的最佳实践。无论您使用 dto 还是其他组件,利用这些技术都有助于确保您的代码库保持干净且适应性强。
最后的想法
采用面向对象的原则和模式,例如特征、接口和抽象类,不仅可以提高代码质量,还可以增强管理复杂系统的能力。通过理解和应用这些概念,您可以构建既灵活又可维护的强大应用程序。
到这里,我们也就讲完了《使用特征、接口和抽象类增强面向对象的设计》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
235 收藏
-
489 收藏
-
225 收藏
-
194 收藏
-
483 收藏
-
339 收藏
-
202 收藏
-
130 收藏
-
305 收藏
-
133 收藏
-
302 收藏
-
330 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习