将两个类合而为一:设计错误?
我编写了这两个类来简单地加密和解密字符串:
Encode.php class Encode { protected funcion do_encode($string) { .. } } Decode.php class Decode { protected funcion do_decode($string) { .. } }
我想要想做的是:
Encrypt.php class Encrypt extends Encode, Decode { protected $stuff_for_parents; function __construct($configs) { $this->stuff_for_parents = $configs['SomeConf']; } public function encode($string) { $this->do_encode($string); } public function decode($string) { $this->do_decode($string); } }
但是我们不能包含多个类,所以:失败。
现在我的问题是:
- 这是一个设计问题吗?因为这个场景对我来说并不奇怪,不是吗?
- 是否有另一种方法可以让一个对象使用不同类中的两个函数?排序
$encrypt->encode($str);
$encrypt->decode($str);
I wrote these two classes that simple encrypt and decrypt strings:
Encode.php class Encode { protected funcion do_encode($string) { .. } } Decode.php class Decode { protected funcion do_decode($string) { .. } }
What I would like to do is:
Encrypt.php class Encrypt extends Encode, Decode { protected $stuff_for_parents; function __construct($configs) { $this->stuff_for_parents = $configs['SomeConf']; } public function encode($string) { $this->do_encode($string); } public function decode($string) { $this->do_decode($string); } }
But we cannot include more than one class, so: fail.
Now my questions are:
- Is it a design problem? Cause this scenario doesn't look weird to me, does it?
- Is there another way to have one object that uses both the functions in the different classes? Sort of
$encrypt->encode($str);
$encrypt->decode($str);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您希望 Encode 和 Decode 是单独的类,则可以在 Encrypt 中创建它们的实例。例如:
在该示例中,Encode 和 Decode 必须是具体类。但您可能想考虑使用接口。如果您认为在不同情况下可能需要使用不同类型的 Encode 和 Decode 对象,则接口非常有用。例如,也许您有 TripleDESEncode 类和 BlowfishEncode 类。它们都可以实现一个公共接口,如下所示:
在这种情况下,您可能希望首先创建要使用的特定实例,然后将它们传递到 Encrypt 的构造函数中。这称为依赖注入:
If you want Encode and Decode to be separate classes, you can create instances of them within Encrypt. For example:
In that example Encode and Decode must be concrete classes. But you might want to consider using interfaces instead. Interfaces are useful if you think you might need to use different types of Encode and Decode objects, in different situations. For example, maybe you have a TripleDESEncode class and a BlowfishEncode class. They can both implement a common interface, like this:
In that case, you may want to create the particular instances you want to use first, and then pass them into the constructor of Encrypt. This is called dependency injection:
将算法封装在单独的类中并没有什么问题(事实上,如果您希望灵活地更改这些算法,尤其是在运行时),那么这是一个很好的做法,但是,您可能想要的是组合而不是继承:
There is nothing wrong with encapsulating your algorithms in separate classes (in fact it is good practice if you would like the flexibility to change these, especially at runtime), however, what you probably want is composition rather than inheritance:
我认为你应该在加密类中创建两个方法/函数。这是非常合乎逻辑的。
它确实有道理,但可以通过编程语言允许的方式来完成。编码解码是加密类应该执行的不同功能,因此这些应该是方法。
I think you should just creat two methods/functions in Encryption class. It is very logical.
It does make sense but it can be done in the way allowed by programming languages. Encoding decoding are different functions that encryption class should perform so these should be methods.
我会写的是这样的:
I would write is somthing like: