- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我很好奇是否有针对以下行为的“更好”设计:
<?php
class Foo {
public function foo() {
// Foo-specific foo stuff.
}
}
class Bar extends Foo {
public function foo() {
// Bar-specific foo stuff.
parent::foo();
}
}
class Baz extends Bar {
public function foo() {
// Baz-specific foo stuff.
parent::foo();
}
}
$boz = new Foo();
$boz->foo(); // should do the stuff in Foo::foo()
$biz = new Bar();
$biz->foo(); // should do the stuff in Bar::foo() and Foo::foo()
$buz = new Baz();
$buz->foo(); // should do the stuff in Baz::foo(), Bar::foo(), and Foo::foo()
// etc...
本质上,我有一个基类 Foo
,它有一个方法 Foo::foo()
,其中包含一些应该始终运行的通用代码。我还有各种继承自 Foo
的子类,每个子类都有自己的特定代码,这些代码也应该始终运行。
我在这里使用的设计使用 DRY 原则来确保 Foo::foo()
中的代码不会在 Bar::foo()
中重复> 和 Baz::foo()
,Bar::foo()
中的代码在 Baz::foo()
中没有重复> 等等。
这种设计的问题(?)是我依赖于子类在每种情况下总是显式调用 parent::foo()
,以及扩展这些类的类来执行同理,以此类推。但是,(据我所知)没有办法实际执行此操作。
所以我的问题是 - 是否有更好的设计来实现相同的行为,或者是否有某种方式在父类/子类之间强制执行这种“契约”?
更新
有些人要求提供用例。多年来我在几个项目中遇到过这种范例,但由于 NDA 等原因无法给出真实世界的例子,所以这里有一个 super 基本的例子,可能有助于更好地说明这个问题:
<?php
// Vehicle
class Vehicle {
public function start() {
// Vehicle engines are on when you start them.
// Unless they belong to me, that is :-(
$this->setEngineStatus(Vehicle::ENGINE_ON);
}
}
// Vehicle > Automobile
class Automobile extends Vehicle {
public function start() {
// Automobile engines are on when you start them.
parent::start();
// Automobiles idle when you start them.
$this->setEngineRpm(Automobile::RPM_IDLE);
}
}
// Vehicle > Airplane
class Airplane extends Vehicle {
public function start() {
// Airplane engines are on when you start them.
parent::start();
// Airplanes also have radios that need to be turned on when started.
$this->setRadioStatus(Airplane::RADIO_ON);
}
}
// Vehicle > Automobile > Car
class Car extends Automobile {
public function start() {
// Cars engines are on and idle when you start them.
parent::start();
// Cars also have dashboard lights that turn on when started.
$this->setDashLightsStatus(Car::DASH_LIGHTS_ON);
}
}
// Vehicle > Airplane > Jet
class Jet extends Airplane {
public function start() {
// Jet engines and radios are on when you start them.
parent::start();
// Jets also arm their weapons when started.
$this->setWeaponsHot(true);
}
}
// Vehicle > Automobile > BobsSuperAwesomeCustomTruck
class BobsSuperAwesomeCustomTruck extends Automobile {
public function start() {
// Uh-oh... Bob didn't call parent::start() in his class, so his trucks
// don't work, with no errors or exceptions to help him figure out why.
// Bob's trucks also need to reset their pinball machine highscores when started.
$this->resetPinballScores();
}
}
最佳答案
我不认为这更好,但这是一种可能的方式。
class abstract Foo {
public function foo() {
// Foo-specific foo stuff.
$this->_foo();
}
// Might be abstract, might be an empty implementation
protected abstract function _foo();
}
class Bar extends Foo {
protected function _foo() {
// Bar-specific foo stuff.
}
}
就个人而言,我更喜欢你的方式,因为我认为它更具可读性。这也意味着 child 不必拥有自己的 foo()
实现。似乎更面向对象。但是,如果您需要每个子类都有自己添加的 foo()
实现,这可能会为您解决问题。
关于PHP/OOP 方法覆盖 DRY 方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7394986/
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
假设我有一个稍微复杂的 for 循环,用于不同的情况。有没有一种方法可以提取该 forloop 并仍然保持代码可读性? 例如: private function bar(){ for(i=0;
JSData 是替代还是补充 Sequelize.js? 我试图了解这两个库如何一起玩以及是否应该一起玩。 从我的阅读来看,JSData 主要处理访问数据,而 Sequelize.js 将创建和修改
我有 aws-eks 集群,下面是我替换现有配置的命令。 kubectl create configmap flink-config --from-file=./config -o yaml --dr
我正在设计一个可重用的类库,其中包含 2 个名为 core.xml.dll 和 core.string.dll 的程序集(其中包括)。 xml 程序集引用字符串程序集,以便使用一些字符串辅助方法。 但
我正在尝试找到一种更 DRY 的方式来使用 docker-compose env。 docker-compose-base.yml base: image: reactjs_web v
我有一些看起来像这样的类(class): struct A { void *stuff; int x; int foo() const; } 我有一些采用这种类型参数的函数,例如 int
我目前正在 Silverlight 中构建一些自定义控件。我希望这些控件能够响应验证错误。我想要做的是在我的控件周围设置红色边框,就像默认的 Silverlight 控件一样。 我的理解是我需要将它添
我正在处理一些服务器端代码,在将它们传递给客户端之前包装所有异常,因此所有面向客户端的方法都有以下代码 try{ DoSomething(); } catch (ExceptionA e) {
“纵深防御”原则指出,应该在多个地方实现约束,这样如果一条数据绕过或漏过一层,就会在下一层被捕获。一个很好的例子是在网络应用程序中——你将验证放在客户端 javascript 中,放在服务器端代码中(
使用这种方法在 MVC 中查看模型:http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view
我想将 jquery 脚本优化为多个元素(单个页面上有许多幻灯片)。我可以添加一些功能,但这并不干燥(不要重复自己)。 Pr
我有一些代码(不是我的),其中包含带有声明的范围映射的指令。我的愿望是在其他地方使用该指令并传递将在模板中使用的附加变量。 我要传递的变量是theVar。 我发现唯一可行的解决方案看起来很噩梦:
我将数据存储在数组中,并从该数组中使用循环创建表。对于一个表,我需要两列,另外 30 列(取决于数组项)。这将有三列。 var prodej = [ /*First column, second,
有没有更好的方法来声明来自同一组(例如“com.android.support”)具有相同版本(例如“23.4.0”)的多个软件包(例如“appcompat-v7”)? 实际申报: ... def a
我有一个看起来像这样的对象: var myObj = { _fooCon: function(f) { if (typeof f != 'function') throw
这就是我所拥有的。 Map data = new HashMap<>(); // assume this has been populated public int getLastestVersion
我在尝试维护 DRY 概念时遇到了 AngularJS 的问题 [不要重复自己]。我希望我做错了什么,有人可以指出我的错误。 一家公司销售卡车和汽车。 这两个项目具有相似和不同的属性。添加到其中一种的
我最终在几个类中得到了几个遵循相同模式的方法(每个类中总是有一堆这些方法): private void updateFoo() { String newFoo = fooTextField.g
假设我有一个字典config,其中包含键username和password。我想创建一个新字典,仅包含 config 中的username 和 password 键值对。一种方法是: new_dict
我是一名优秀的程序员,十分优秀!