Design patterns for code that breaks regularly?

I have this class trying several methods to get data from google maps web services API.

If one of the methods fails, it tries the other. and etc.

Something like this (pseudocode):

FUNCTION FIND_ADDRESS( house_number, postcode )

    get location co-ordinates for postcode from local database

    if location returns false, try getting location from maps service

    if map service fails, return "postcode not found", exit

    get address components using location co-ordinates

    if address components doesn't contain street name, return street name not found, exit

    if street name exists, get all address_components + location for house number, street_name and postcode

    if no results, try again without the postcode,

    if still no results, return location co-ordinates for postcode found earlier in code



As you can see, this is very procedural!

I'm trying to figure out how to improve the code, and I've implemented all the reusable code, added exception handling to know exactly where the code is failing if it does.

But I was wondering if anyone knows of a design pattern or similar solution.

Because I basically tried something, if he didn't try something else, if he didn't try something else, and so on until I get the full address

Any ideas?


source to share

5 answers

You might want to explore the Chain of Responsibility.

In object-oriented design, the chain of responsibility pattern is a design pattern that consists of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can process; the rest are passed on to the next processing object in the chain. There is also a mechanism for adding new processing objects to the end of this chain.

So instead of having many if / else or try / catch blocks, you do something like

$finderChain = new AddressFinder;
    ->add(new LocalFinder)
    ->add(new MapsService)

$result = $finder->find($houseNo, $postCode);


Internally, you will post $ LocalNo and $ postCode to LocalFinder. If he does not find the required data, the next element in the chain will be tasked with finding the required data. This is repeated until the end of the chain is reached or the required data is received.



I would try something like:

public function getAddress($houseNumber,$postCode){

    // as far as i know, if the function returns a LOOSE true, the condition is true
    if($data = location.coordinates()){ 

        // $data was returned a value,  do additional parsing here

        // if you need to return early because of an error, you can in here

        //if all proccesses deem your data valid, return the data you want returned
            return $some_value;

    //if the previous didn't return, it goes here. $data is overwritten
    //or you can use some other variable name
    if($data = maps.service()){

        //some more parsing

        return $some_other_data;

    // if non of the above was satisfied (and thus none returned yet), return a FALSE
    return FALSE;





It's not a matter of whether it's procedural / oop / whatever.

If you understand and maintain the code well, then great. If you can do it after 6 months, then even better.

Your function block looks the way it is - just keep an eye on how deep your nesting is. Less is better.



Use nested try{} catch{}




You only have 6 ifs, which is not the case at all, I worked with code that requires up to 50 if the program works fine. The software pattern concept is not right for your current problem. DP is not a solution to a specific problem, it is a concept of solutions to a repetitive problem. The reading patterns help me personally discover more solutions even to social problems, coding patterns, process modeling, social problems in the Couples Program and more, it offers many thoughtful ideas.



All Articles