1

I had originally posed this on StackOverflow. The question was closed and I was asked to post this here instead. Here is the original question I had posted there:


I have been programming using procedural method for a while now. I have done some good amount of reading pertaining to OOP and I know how to use it. It's just that I am not sure if whether I still know how to use it correctly? So to put this to a test I am presenting a skeleton of a plugin which I have developed using OOP. The plugin is supposed to fetch values like subscribers, follows etc from various social networks.

// This is an abstract class used by all Social Network classes
abstract class Social_Network {
    protected $service_url,$service_identifier,$icon_url,$refresh_data;

    function __construct($arguments) {
        // Code to populate the protected variables declared above
    }

    protected function get_value_internal() {
        // Internal here means get data from cache.

        // Call get_value_external If cache data has expired or does not exist
    }

    protected function parse_json_data($data) {
        // This function parses data and gets the requested value from it
    }

    public function display_value() {
        // This is a public function which outputs the value

        // Call get_value_internal

        // Display the value with icon, etc.
    }

    //This will be different for all networks because every network would have a different method for retrieving data
    abstract function get_value_external();
}

class Facebook_Social_Network extends Social_Network {
    function get_value_external() {
        // Unique code for retrieving data from Facebook

        // Process the data fetched above and parse it using the parse_json_data function defined in our abstract class
        return $this - > parse_json_data($data);
    }
}

class Twitter_Social_Network extends Social_Network {
    function get_value_external() {
        // Unique code for retrieving data from Twitter

        // Process the data fetched above and parse it using the parse_json_data function defined in our abstract class
        return $this - > parse_json_data($data);
    }
}

class YouTube_Social_Network extends Social_Network {
    function get_value_external() {
        // Unique code for retrieving data from YouTube

        // Data returned by YouTube is processed differently so this doesn't use the shared method parse_json_data

        // Process the data and put it inside a variable named $value which will be returned below

        return $value;
    }
}

I will appreciate if someone good at OOP can take a quick look and tell me if I am on target with my understanding, close to the target or way off?

One thing that's bothering me is whether I should have just used one class which would handle all Networks?


2
  • The design and layout of your objects to some degree depends on the language you choose to use. Some languages support different things better than others, so what to recommend is partly based on that. Dec 21, 2012 at 17:09
  • 2
    I can appreciate your question regarding your understanding of OOP, but given that you're asking us to review your code I would actually suggest that part go to CodeReview.stackexchange.com and the rest of the question be more specific about where you feel uncertain in your OOP approach Dec 21, 2012 at 17:54

2 Answers 2

2

The basic design looks OK, except for having get_value_internal() and get_value_external() as separate public methods, one getting cached data and one getting the value from the net. A client should not have to decide which to call - it should be handled internally by the class (maybe have a public method invalidate_cache() if needed).

Additionally, it might be better to separate the functionalities for getting the data and displaying it - display_value() would be better as method of the object returned by the get_value(), or perhaps in a Formatter class of its own, if the display functionality is complex.

0

One thing that's bothering me is whether I should have just used one class which would handle all Networks?

That will become really bad as soon as your code will grow enough. You definitely should have base class and/or some interfaces/traits.

Your code sample looks OK, but it's way too small to judge how well you've grasped the OOP concepts.

IMO, you understand OOP deep enough to design big systems (this knowledge is required but not sufficient) if you understand:

  • use cases for single/multiple inheritance, composition, interfaces and traits
  • polymorphism in general (just basic understanding)
  • SOLID principles
  • design patterns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.