开发者

PHP wrapper module (for working with many social networks) design

开发者 https://www.devze.com 2023-03-06 21:22 出处:网络
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 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开发者_StackOverflow 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


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


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

精彩评论

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