开发者

Use two classes in one: design mistake?

I wrote these two classes that simple encrypt and decrypt strings:

Encode.php

    class Encode {
        protected funcion do_enc开发者_运维知识库ode($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);


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');
?>


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


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:

class Encrypt
{
    protected $stuff_for_parents;

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

    protected funcion encode($string)
    {
    }

    protected funcion decode($string)
    {
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜