- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了好玩,我正在设计一个简单的系统来存储唱片(黑胶唱片)数据和一些其他与唱片相关的一般元素(唱片套、唱片清洁器等)。
由于 90% 的数据将是黑胶唱片,另外 10% 是其他项目,我考虑将它们分为两类:唱片和项目。请记住,虽然最终不同,但两者都是具有某些共同点的产品,例如价格、数量等。
我怀疑我是否应该创建一个抽象类,比如 Item,以及两个扩展 Item 的类:Records 和 NonMusicalProducts。像这样:
abstract class Item {
static function getItemPrice($itemId, $type='record'){
$result = 0;
// query record table if type is record, otherwise query nonmusicalproduct table
return $result;
}
abstract function getItemDetails();
}
鉴于记录:
class Record extends Item {
static function getItemDetails($id) {
// query DB and return record details
}
}
和 NMProduct
class NMProduct extends Item {
static function getItemDetails($id) {
// query DB and return NMProduct details
}
}
是否可以将 NMProduct 和 Record 中的两个方法都定义为静态的?我并不总是要从对象访问该方法。
另一种选择是只有一个 Item 类和一个从 item 继承的 Record 类,但我已经到了一个似乎不正确的地步,尤其是在尝试获取详细信息时:
class Item {
function getItemDetails($id, $type){
if ($type == 'record') {
// query record table using id
}
if ($type == 'nmproduct'){
// query nmproduct table
}
}
感觉不对,因为我认为去记录类获取记录详细信息会更合适,因为这些列与 nmproduct 中的那些列不同。在一个父类中这样做感觉违背了 OO 的目的。
或者我能想到的最后一个选项是一个单一的类项目:
class Item {
function getItemPrice($id, $type) {
// if type is record then query the record table
// if type is nmproduct then query the nmproduct table
}
function getItemDetails($id, $type) {
// if type is record then query the record table
// if type is nmproduct then query the nmproduct table
}
}
同样,最后一个选项感觉不对,因为太多不相关的东西会被浓缩在一个类中。记录属性(即 artist_id、number_of_discs 等)与 nmproduct 不同。
解决这个问题的最佳方法是什么?
最佳答案
还有另一种可能更可取的选择来实现多态性:组合。
但是在你们提供的选项中,我更喜欢第一个。但是让你的基类尽可能通用(因为它是你的基类,你不想缩小使用范围,因为你的基类太具体了):
第一个例子:
abstract class Item
{
int Id;
abstract Decimal GetPrice();
abstract IItemDetail GetDetail();
}
由于在您的第一个示例中从数据库中获取数据是特定于类型的,因此最好将特定于类型的操作移动到类型本身(因此每个派生类型都必须实现其特定行为。这消除了类型检查)。正因为如此,在基类中使这些方法成为静态方法没有多大意义(与始终基于通用类型获取数据的情况相反)。但这是一个设计决定。
您的第二个示例 介绍了之前提到的那些讨厌的类型检查。它们使您的代码难以扩展且难以理解(例如可读性)。这是因为,如前所述,您将特定于类型的操作(不能泛化,例如模板)移动到不具备执行操作所需的所有信息的类型(可以是基本类型)中。此类型对对象一无所知。既不是类型也不是状态。在操作发生之前,此类型需要查询目标以获取所有信息:对象是什么类型?它处于什么状态?这就是为什么我们应用 Tell-Don't-Ask 原则和继承来摆脱这种情况。
第三个例子更像是一个静态帮助类。方法可以是静态的(并且在支持泛型或模板化时),因为此辅助类型旨在对任何类型(或公共(public)基类型)进行操作。但同样,如果操作依赖于类型(如在您的示例中),那么这将引入类型切换和状态检查,这距离编写可扩展/可维护代码是一大步。
您提供的第一个选项是最干净的。它允许多态性。而且它不违反Demeter 法则 或Open-Close。添加新类型(例如“电影”)是通过实现新的“项目”来完成的,这对于可扩展性非常重要。
但是有更多的可能性来实现你想要做的事情。我之前说过作文。但是封装呢?我们可以使用封装 来提取变化的部分。我们可以有一个单一的“项目”类型,它用于所有类型的项目并将类型特定的行为(我们的子类型实现唯一不同的行为,例如您示例中的数据库访问)提取到一个新类型中:
class Item
{
// ctor
Item(IItemPlayer typeSpecificPlayer)
{
this.TypeSpecificPlayer = typeSpecificPlayer;
}
public void Play()
{
this.TypeSpecificPlayer.Play(this);
}
public int Id;
private IItemPlayer TypeSpecificPlayer;
}
interface IItemPlayer
{
void Play(Item itemToPlay);
}
class RecordIItemPlayer implements IItemPlayer
{
void Play(Item item) { print("Put the needle to the groove"); }
}
class MovieIItemPlayer implements IItemPlayer
{
void Play(Item item) { print("Play the video"); }
}
构造函数强制为“Item”类型的Composition 注入(inject)不同的组件。此示例使用了组合 和封装。你会像这样使用它:
var record = new Item(new RecordPlayer());
var movie = new Item(new MoviePlayer());
recordItem.TypeSpecificPlayer.Play();
movieItem.TypeSpecificPlayer.Play();
如果不使用 Composition,“IItemPlayer”将成为关联的外部类型:
interface IItemPlayer
{
void Play(Item item);
}
class RecordIItemPlayer implements IItemPlayer
{
void Play(Item item) { print("Put the needle to the groove"); }
}
class MovieIItemPlayer implements IItemPlayer
{
void Play(Item item) { print("Play the video"); }
}
class Item
{
int Id;
Decimal Price;
IItemDetail Details;
}
并像这样使用它:
var record = new Item();
var recordItemPlayer = new RecordItemPlayer();
var movie = new Item();
var movieItemPlayer = new MovieItemPlayer();
recordItemPlayer.Play(record);
movieItemPlayer.Play(movie);
如果需要额外的变化,例如播放特定类型媒体的方法,我们只需添加一个新的“IItemPlayer”实现。
关于面向对象设计 : Abstract class design vs Regular Inheritance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50246290/
我正在用power designer创建一个物理模型,我想将默认值添加到我的Mysql表中。 有可能吗,有人加了默认值 ? 谢谢 最佳答案 有可能,我发现“列属性”并不容易 方法如下: 选择表格(单击
关闭。这个问题是 opinion-based 。它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文来回答。 2年前关闭。 Improve t
我正在编写一个采用 Material Design 布局的应用程序,但找不到任何关于如何将对话框动画显示到屏幕上的指南。 这表明盒子应该只是“砰”的一声存在,但这似乎违背了设计的精神,包括动画和触觉。
我做了一个巨大的掠夺,不小心丢失了我的*.cs(设计文件)..我刚刚得到了*.designer文件。 我能否反过来,仅使用 .designer 文件以某种方式创 build 计文件 (*.cs),还是
如果 Google 的关键字规划器向我显示关键字“Web Design [city-name]”获得约 880 次搜索,而“Website Design [city-name]”获得约 620 次搜索
首先,代码: $(document).ready(function() { $('#member_pattern').hide(); $('.add-member').click(function()
大型软件公司之一问了这个问题。我想出了一个简单的解决方案,我想知道其他人对该解决方案有何看法。 You are supposed to design an API and a backend for
在最新的 Material Design 文档 (https://www.google.com/design/spec/what-is-material/elevation-shadows.html#
背景 我正在对从我们的 RDBMS 数据库到 MongoDB 的转换进行原型(prototype)设计。在进行非规范化时,似乎我有两种选择,一种会导致许多(数百万)个小文档,另一种会导致更少(数十万)
Qt Designer (5.11.2) 在选择 QWebEngineView-Widget 时崩溃。 我正在创建一个对话框,以将其作为 .ui 文件包含在 QGIS 3 中。在表单中,我想使用 QW
我直接从 getmdl.io(组件页面)和所有设备(多台 PC、浏览器、手机等)复制代码,汉堡菜单不在标题中居中。我似乎无法隔离 css 中的菜单图标来重新对齐它。 getmdl.io 上的所有组件代
如何为 SPA 动态初始化 materialize design lite (google) 的组件?当我在 View 中动态初始化组件时,JS 没有初始化。正如我已经尝试过使用 componentH
我正在使用 Angular 4 构建一个 Web 应用程序。对于设计,我使用的是 Material Design lite。但是,我想使用 MDL 实现一个交互式轮播,它给我流畅的外观和感觉,并且与我
它看起来像 Polymer Starter Kit包含比 Material Design Lite 更多的组件,并且现在可用。由于两者都是符合 Material Design 理念的 Google 项
我在设置 mdl-textfield 样式时遇到了一些困难。 具体来说,设置 float 标签的大小和颜色,以及按下输入字段后动画的高度和颜色。 实际上,这是我从组件列表中获取的起点。 https:/
所以,好友列表的现代概念: 假设我们有一个名为 Person 的表。现在,那个 Person 需要有很多伙伴(其中每个伙伴也在 person 类中)。构建关系的最明显方法是通过连接表。即 buddyI
如何在导航中创建子菜单项? Link Link Link Link 我不能用 用它。什么是正确的类? 最佳答案 MDL 似乎还没有原生支持子菜单。 然而
我想知道我应该遵循哪些步骤来解决设计自动售货机等问题并提出许多设计文档(如用例、序列图、类图)。是否有任何我可以阅读的来源/链接,其中讨论了如何逐步进行。 谢谢。 最佳答案 我不确定是否有任何普遍接受
早在 10 月份,Kristopher Johnson 就询问了 Accounting Software Design Patterns 他收到了几个答案,但基本上都是一样的,都指向Martin Fo
我一直在为我们的产品开发一些组件,其中之一是基于流布局面板。 我想做的是为它提供一个自定义设计器,但不会丢失其默认设计器 (System.Windows.Forms.Design.FlowLayout
我是一名优秀的程序员,十分优秀!