www.webdeveloper.com
Results 1 to 6 of 6

Thread: Using Interfaces to design an application

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    Question Using Interfaces to design an application

    HI

    I am trying to implement a Short Message Service or as you call it Texting service for mobiles into my PHP application that will send sms to my customers using various SMS providers (the real case senario is diff but here I am trying my best to explain my question, so pls bear with this SMS example only).

    Now, I am told to follow design patterns (not sure which one as I am new to patterns) to integrate it.

    My SMS class should have the following methods

    PHP Code:
    <?php
    class Verizon
    {
            public function 
    __construct($apiKey){
                    
    // class constructor code...
            
    }

            public function 
    AddNumber(array $numbers)
            {
                    
    // ...
            
    }

            public function 
    Send()
            {
                    
    // ...
            
    }
    }
    Now, just like a verizon, i will have different providers in future like AT&T, Cellcom, T-Mobile US etc...

    So here are the questions:

    Q1. Is it really a good idea to have a different class for different providers? Means If i need to implement a new provider, I have to write a new class? If no, then what?

    Q2. How do I make use of interfaces in this case? (i am told to use interfaces)

    Q3. If you could please draft me a skeleton of the structure of the best way to implement with an interface, i will really appreciate that.




    many thanks in advance

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    I would create an abstract SMS class (or whatever it makes sense to call it) which might implement one or more interfaces if it makes sense -- so far I've not seen any reason to have any interfaces. (That does not mean there is not any reasons, just that I'm not yet aware of them from the info provided.)

    Your concrete Verizon, ATT, etc. classes would extend the abstract class, overriding any methods/properties that need to be different than what is defined in the abstract class, or defining any methods defined as "abstract" in that abstract class.

    You could then look up the Factory Pattern (or Abstract Factory Pattern) and look at how to use that to instantiate the proper concrete class at run-time.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2006
    Posts
    655
    Thanks NogDog for the theoretical explanation. Can u please write the skeleton code to explain this?


    Thanks in advance.
    Last edited by phantom007; 08-04-2014 at 10:12 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    PHP Code:
    interface SomeInterface
    {
        public function 
    foo($arg);
        public function 
    bar();
    }

    abstract class 
    SMS implements SomeInterface
    {
        public function 
    foo($arg)
        {
            
    // default behavior
        
    }

        abstract public function 
    bar();
        
    // must be defined by any concrete child class
    }

    class 
    SMS_Verizon extends SMS
    {
        public function 
    foo($arg)
        {
            
    // only needed if foo() does something different than
            // what is defined in the parent SMS class
        
    }

        public function 
    bar()
        {
            
    // must be defined in this concrete instance of SMS
        
    }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Sep 2006
    Posts
    655
    Hi NogDog

    Appreciate your reply.

    One thing that I forgot mentioning, and that is to use a provider we have to use their library through which we interact with their servers.

    So whats the best approach in doing it? where do we load the third party library?

    Any guidance will be appreciated.


    Thanks

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Interfaces are much like abstract classes but with one major difference. They doesn't have a parent-child relationship. So what that means is that you can create classes of totally different ancestry and yet they will contain the same methods defined by the interface they implement.

    One example. You can have two objects, let's say a pc and a watch. This two objects have nothing in common. But let's say that you create an online store and want to be able to sell those two products. You can implement Sellable interface and define a method which both classes (pc class, and watch class) must implement.

    Example:

    PHP Code:
    interface Sellable
          
    {
             public function 
    sell();
          }
          
          class 
    PC implements Sellable
          
    {
             private 
    $_os "GNU/Linux Debian Wheezy";
             private 
    $_cpu "Core i7 4930 K";
             private 
    $_ram "DDR 3 8GB";
             private 
    $_q 10;
             
             public function 
    sell() {
                if (
    $this->_q 1) {
                   
    $this->_q--;
                   return 
    true;
                } else {
                   return 
    false;
                }
             }
          }
          
          class 
    Watch implements Sellable
          
    {
             private 
    $_waterResistant true;
             private 
    $_stopwatch true;
             private 
    $_digital false;
             private 
    $_q 2;
             
             public function 
    sell() {
                if (
    $this->_q 1) {
                   
    $this->_q--;
                   return 
    true;
                } else {
                   return 
    false;
                }
             }
          } 
    NOTE - Interfaces can not have any properties. They can contain only method declarations (without any code) plus all methods have to be defined as public.
    Last edited by tech_soul8; 08-05-2014 at 01:18 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles