How would you solve this?

envo

Expert Member
Joined
Jan 14, 2014
Messages
3,265
Reaction score
437
My brain is a bit shot on a Friday afternoon, so need some input :)

This is for PHP stuff I'm doing.

I have several data feeds from companies using their API's

I've written a wrapper class that will return their datasets in an array or json. I have 4 classes for 4 data feeds.

Instead of having to call each individually, I want to have a "dispatcher sort of" class which will prepare and load all 4 and give me back 1 full dataset (all 4 combined)

How would you approach this and have it dynamic enough to just add another data feed class and have it, automatically, added to the current system?

I really have no idea where to even begin. Trying to challenge myself and get my PHP skilled up :)
 
Last edited:
I assume this is data which goes to some storage, and not a response to a user request?

how important is it that you have results from all 4(n) feeds? what happens when feed 1 is down?

best would be to switch to java and use spring integration, which is an implementation of Enterprise Integration Patterns book :D

Its as simple as adding a channel adapter, that feeds into a transformer, and feeds into an aggregator.
 
lol, no jumping to java isn't an option right now.

The feeds will return data if they can connect, so obviously there should be compensated for failure.

I think the factory pattern might fit in here, I'd still need a controlling class that will instantiate each of those classes, so I can do something like this:

$whatsup = SuperClass();

And SuperClass will then instantiate and run the 4 Factory pattern classes and return data. So I can go:

$whatsup['feed1']

And get data :)

The data gets saved and the end user will see it. On 2nd query of the data within a specific time frame I show a cached copy. I just need it to be easily extended by adding another feed and defining it somewhere without changing core code each time.
 
using the factory:

PHP:
abstract class AbstractFactoryMethod {
  
    abstract function makeStuff($param);
  
  }

Can I have that and then in the class that extends it, have this:

PHP:
 function makeStuff($param,$param2)
?

and can I get an abstract class to implement a function that can be used by all the children but not overloaded so, like, x+y=z function output will be valid for all classes getting this factory abstract? or is that for interface?
 
I have this:
PHP:
abstract class AbstractFactoryMethod {
	abstract function makeStuff();
  	public function doStuff($p) {
  		return $p.'sucks';
  	}
}

class Example extends AbstractFactoryMethod {
	public function makeStuff($p = ''){
		echo 'Book'.$this->doStuff($p);
	}
}

$class = new Example;

$class->makeStuff('help');
echo $class->doStuff('stuff'); //don't want people able to do this!

I want to only have doStuff being called by the extended class, but if I put it as a private function, it moans about it not being in context?

PHP:
abstract class AbstractFactoryMethod {
	abstract function makeStuff();
  	private function doStuff($p) {
  		return $p.'sucks';
  	}
}

class Example extends AbstractFactoryMethod {
	public function makeStuff($p = ''){
		echo 'Book'.$this->doStuff($p);
	}
}

$class = new Example;

$class->makeStuff('help');

Call to private method AbstractFactoryMethod::doStuff() from context 'Example'

As you can see I figured out how to overload an abstract function, but I want to now have a defined function in the abstract class callable only by the class extending it, and not directly
 
PHP:
abstract class AbstractFactoryMethod {
    abstract function makeStuff();
      protected function doStuff($p) {
          return $p.'sucks';
      }
}

class Example extends AbstractFactoryMethod {
    public function makeStuff($p = ''){
        echo 'Book'.$this->doStuff($p);
    }
}

This works so that doStuff can't be called from outside :)

Thanks Ixeus and everyone who offered help and guidance
 
Arg, php lets you do that....

That sure makes for confusing code.

Essentially you never implemented the abstract method, you just overloaded it.

The above code is also an anti pattern.

Your base class should have a concrete method that calls an abstract method, that decendants must implement. You cannot expect the implementor to know that they must call doStuff from makeStuff.
It is called the template method pattern

PHP:
abstract class AbstractFactoryMethod {
    public function process() {
          //preProcess some stuff;
          internalProcess();
          //postProcess some stuff;
    }
    protected abstract function internalProcess();
}

class Example extends AbstractFactoryMethod {
    protected function internalProcess(){
        //do some processing unique to this type/implementation
    }
}
 
Last edited:
anti-pattern sounds like a pattern :p

Thanks for the correction
 
Top
Sign up to the MyBroadband newsletter
X