将两个类合而为一:设计错误?

发布于 2024-10-10 05:16:44 字数 844 浏览 3 评论 0原文

我编写了这两个类来简单地加密和解密字符串:

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); }
    }

但是我们不能包含多个类,所以:失败。
现在我的问题是:

  1. 这是一个设计问题吗?因为这个场景对我来说并不奇怪,不是吗?
  2. 是否有另一种方法可以让一个对象使用不同类中的两个函数?排序 $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:

  1. Is it a design problem? Cause this scenario doesn't look weird to me, does it?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅冕 2024-10-17 05:16:44

如果您希望 Encode 和 Decode 是单独的类,则可以在 Encrypt 中创建它们的实例。例如:

<?
class Encrypt {
  private $encoder;
  private $decoder;
  public function __construct() {
    $this->encoder = new Encode();
    $this->decoder = new Decode();
  }
  public function encode($string) {
    return $this->encoder->encode($string);
  }
  public function decode($string) {
    return $this->decoder->decode($string);
  }
}
?>

在该示例中,Encode 和 Decode 必须是具体类。但您可能想考虑使用接口。如果您认为在不同情况下可能需要使用不同类型的 Encode 和 Decode 对象,则接口非常有用。例如,也许您有 TripleDESEncode 类和 BlowfishEncode 类。它们都可以实现一个公共接口,如下所示:

<?
interface IEncode {
  public function encode($string);
}
class TripleDESEncode implements IEncode {
  public function encode($string) {...}
}
class BlowfishEncode implements IEncode {
  public function encode($string) {...}
}
?>

在这种情况下,您可能希望首先创建要使用的特定实例,然后将它们传递到 Encrypt 的构造函数中。这称为依赖注入:

<?
class Encrypt {
  public function __construct(IEncode $encoder, IDecode $decoder) {
    $this->encoder = $encoder;
    $this->decoder = $decoder;
  }
  ...
}

$myEncrypt = new Encrypt(new BlowfishEncode(), new BlowfishDecode());
echo $myEncrypt->encode('test');
?>

If you want Encode and Decode to be separate classes, you can create instances of them within Encrypt. For example:

<?
class Encrypt {
  private $encoder;
  private $decoder;
  public function __construct() {
    $this->encoder = new Encode();
    $this->decoder = new Decode();
  }
  public function encode($string) {
    return $this->encoder->encode($string);
  }
  public function decode($string) {
    return $this->decoder->decode($string);
  }
}
?>

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:

<?
interface IEncode {
  public function encode($string);
}
class TripleDESEncode implements IEncode {
  public function encode($string) {...}
}
class BlowfishEncode implements IEncode {
  public function encode($string) {...}
}
?>

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:

<?
class Encrypt {
  public function __construct(IEncode $encoder, IDecode $decoder) {
    $this->encoder = $encoder;
    $this->decoder = $decoder;
  }
  ...
}

$myEncrypt = new Encrypt(new BlowfishEncode(), new BlowfishDecode());
echo $myEncrypt->encode('test');
?>
染火枫林 2024-10-17 05:16:44

将算法封装在单独的类中并没有什么问题(事实上,如果您希望灵活地更改这些算法,尤其是在运行时),那么这是一个很好的做法,但是,您可能想要的是组合而不是继承:

class Encrypt {
    private $encoder;
    private $decoder;
    protected $stuff_for_parents;

    function __construct($configs, $encoder, $decoder) {
        $this->stuff_for_parents = $configs['SomeConf'];
        $this->encoder = $encoder;
        $this->decoder = $decoder;
    }

    public function encode($string) { $this->encoder->do_encode($string); }
    public function decode($string) { $this->decoder->do_decode($string); }
}

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:

class Encrypt {
    private $encoder;
    private $decoder;
    protected $stuff_for_parents;

    function __construct($configs, $encoder, $decoder) {
        $this->stuff_for_parents = $configs['SomeConf'];
        $this->encoder = $encoder;
        $this->decoder = $decoder;
    }

    public function encode($string) { $this->encoder->do_encode($string); }
    public function decode($string) { $this->decoder->do_decode($string); }
}
傲娇萝莉攻 2024-10-17 05:16:44

我认为你应该在加密类中创建两个方法/函数。这是非常合乎逻辑的。

它确实有道理,但可以通过编程语言允许的方式来完成。编码解码是加密类应该执行的不同功能,因此这些应该是方法。

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.

假装不在乎 2024-10-17 05:16:44

我会写的是这样的:

class Encrypt
{
    protected $stuff_for_parents;

    function __construct($configs) {
        $this->stuff_for_parents = $configs['SomeConf'];
    }

    protected funcion encode($string)
    {
    }

    protected funcion decode($string)
    {
    }
}

I would write is somthing like:

class Encrypt
{
    protected $stuff_for_parents;

    function __construct($configs) {
        $this->stuff_for_parents = $configs['SomeConf'];
    }

    protected funcion encode($string)
    {
    }

    protected funcion decode($string)
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文