Have I understood OOP correctly?

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 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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật