maxiscool
maxiscool

Reputation: 537

Object that Works On Other Objects

So I'm working on a project that's goal is to collect data from user's that preform a search. I have modeled the different types of information to be collected from the search as objects. I plan on creating a sort of managing object that will be creating and manipulating these objects.

My question is, are there any patterns or design decisions I should consider when creating this "managing" object? One thing that comes to mind is that this object will be tightly coupled to the "search data" objects, because it will be possibly creating and saving them inside it's methods.

I don't think dependency injection would work in this instance, because multiple copies of the same object may have to be worked on, so it would be impossible to know what the parameter list would be.

Thank you for your insight.

Upvotes: 0

Views: 63

Answers (2)

SISYN
SISYN

Reputation: 2259

I think it would be best to create a parent class or pattern for all of these other objects or children to follow or inherit. For instance, let's say that you want to get or set some data in a particular object. If the data field names are different, you'd run into problems.

Let's say you have an object called GoogleSearch and an object called YahooSearch. If the user inputted query in GoogleSearch can be retrieved by the $google->getQuery() method and the YahooSearch query has to be retrieved using $yahoo->getInput() then obviously you'll run into problems unless you want to manually specify information like this whenever it arises.

However, if you think about things like this prior to developing the framework, it becomes much easier to automate everything. In this case, it's safe to say that if you're building a collection of data from user searches, that all searches will share some attributes.

For instance, - what search engine did they use? - what was the inputted query? - when did the search take place? - from what ip? - did the user select one of the results? - did the user search on a computer, mobile device, other?

Given these minimal considerations, we could design a system to incorporate all of these attributes. For instance, this could be your parent class:

class Search {
    protected $engine, $query, $when, $who, $browserType;
    function __construct() {
        $this->when = time();
        $this->browserType = $_SERVER['HTTP_USER_AGENT'];
        $this->who = $_SERVER['REMOTE_ADDR'];
    // some more class methods
    }
}

then you could inherit from this class to create specific search types like this:

class GoogleSearch extends Search {
    function __construct() {
        // set the engine to Google
        $this->engine = 'Google';
    }
}

Some more things to consider might be the scope or visibility of variables. Here, I've declared them as protected which basically means that they're private but can be accessed by children or extension classes. Hope this helps, if you have any other questions or I didn't understand yours, please let me know and I'll try to help.

Upvotes: 1

jkratz
jkratz

Reputation: 393

I'd suggest taking a look at the creational pattern list here http://en.wikipedia.org/wiki/Software_design_pattern

Abstract Factory, Builder, Factory Method all might apply to what you are looking for. Dependency Injection isn't an appropriate concept for what you're asking. DI makes it easier to wire up things at runtime and remove hardcoding of dependencies. Not really the same thing.

Upvotes: 0

Related Questions