2

I am not good at OOD yet so it would nice to receive some advices. I am going to write wrapper class for working with many social networks and services through it's APIs. I want my wrapper could hide specifics for every service/social network behind one interface. It should be flexible and easy to reuse.

For example:

$sn = new SocialNetworks();
$sn->post(new Twitter('some post body'));
$sn->post(new Facebook(array('photo'=>'blabla.jpg')));
$sn->post(new Tumblr('long text'))->attach('blabla.jpg');

Well, something like this. So what could be the best design solution for this? Thank you

j0k
  • 22,600
  • 28
  • 79
  • 90
VitalyP
  • 1,867
  • 6
  • 22
  • 31

2 Answers2

2

You are probably better off defining an interface for all actions and then implement that interface in all the socialnetwork classes. Something along the lines of


interface ISocialNetwork {
   public function post();
}
class Twitter implements ISocialNetwork {
   public function post() {
   }
}
class Facebook implements ISocialNetwork {
   public function post() {
   }
}

$Tw = new Twitter('some post body');
$Tw->post();
Srisa
  • 967
  • 1
  • 7
  • 17
0

I think you should think of a factory : http://en.wikipedia.org/wiki/Factory_method_pattern

Maybe something like this :

<?php
class Service
{
    public static function build($type)
    {
        $class = 'Service' . $type;
        if (!class_exists($class)) {
            throw new Exception('Missing format class.');
        }
        return new $class;
    }
}

class ServiceTwitter {}
class ServiceFacebook {}

try {
    $object = Service::build('Twitter');
}
catch (Exception $e) {
    echo $e->getMessage();
}
Brice Favre
  • 1,511
  • 1
  • 15
  • 34