Hi…
Not sure what this means. Communication means passing data, right?
Not sure either of these should be classes, but I don’t know enough aboput your problem yet. Right now your problem is simple enough that procedural will work just fine.
What’s caused you to look at OO?
You should have some kind of class with a register() method on it. That register method will take the username, password and whatever else it needs for the registration process.
Hm…I just said “registration process”. That sounds more like an object. Objects are things that do stuff. “Roles” or “actors” are alternate words.
How about…?
class RegistrationProcess {
private $customers, $mailer;
function __construct($customers, $mailer) {
list($this->customers, $this->mailer) = array($customers, $mailer);
}
function register($username, $password, $name, $email, $address) {
$this->throwIfInsecure($username, $password);
$change = $this->customers->transactionalOperation();
$person = $change->createPerson($name, $email, $address);
$person->addCredentials($username, $password);
$change->commit();
$this->mailer->mail($email, 'Welcome', $this->welcomeEmail($name));
}
private function throwIfInsecure($username, $password) { ... }
private function welcomeEmail($name) { ... }
}
Do the same, but don’t write any methods over 10 lines. This will force you to write local private methods to break the problem down, or to use external objects to do the work for you.
Which to choose? Above, the validation rules and the e-mail title and content were part of the registration process. It’s right that these remain local to this class. The rest of the process, transactions, talking to the database, sending mails, etc. is mechanical. Anything mechanical you want to push down into worker objects. Your method should describe the solution in a language appropriate to the problem.
Here the Mailer class will twiddle with mail headers and the ChangeSet class will keep track of the DB connection for us (assuming it is a DB). If you were describing the registration process to someone and you started to talk about mail headers their eyes would glaze over. That’s a sign it should be pushed down into another class with it’s own language.
yours, Marcus