mardi 3 janvier 2017

What design pattern is it, What's the pros and cons of it?

So basically when I was reading through some free assets written by others. I found this class called _StateBase and i can't help wondering what design pattern they used. (If there's any) Shown below.

public abstract class _StatesBase : MonoBehaviour
{
    public abstract void OnActivate();
    public abstract void OnDeactivate();
    public abstract void OnUpdate();
    public override string ToString()
    {
        return this.GetType().ToString();
    }
}

And also, besides this class, I also see a bunch of classes named, GameoverState, GamePlayState, GoalState. All inheriting the _StateBase class.

Managers.Game.SetState(typeof(MenuState));

Any suggestions would be appreciated.

lundi 2 janvier 2017

How to implement Fork-join with rabbitMQ

I want to implement the fork-join pattern in php using rabbitMQ. I managed to split the work to parts and proccess them in parallel. But I don't know how to join the results.

It seems that rabbitMQ has no futures or promisses equivalent.

Any help will be appreciated

MVC: Where to store data?

I like to apply the Model-View-Controller design pattern in a Cocoa application (written in Swift). Let us assume the famous person example: the App should show a text field and an add button. A persons name can be put into the text field and then be stored in an array by clicking the add button (and the arrays content may be shown in a table view). Now, the storyboard contains all my View-related stuff while a class Person (with a variable name) constitutes the Model-class. The view controller would be responsible for receiving an showing the data (table view delegate and data source for example) and has an IBAction which extracts the name and puts it somewhere. But, I am not sure where to actually put the array that holds all the names. Would I have it in the view controller as well (which would be the most easiest solution I think)? Or somewhere else? What if the App becomes more and more elaborate with multiple data holding structures in different views (with different view controllers)?

PHP Objects global

I usually don’t code at all and wouldn’t even consider myself much of a coder. I got a few PHP Classes such as dbHandler, contentHandler etc. und would like to use them global.

For example within my content Class I need to access the contentHandler in order to determine the kind of page etc.

Of course my Objects are non global and I’ve heard about design patterns such as singleton, factory, registry etc.

Which one to use in my case and how to? We are pretty much talking about 10-15 classes / objects, also there is no performance or code beauty required.

Which pattern should I use, why, and how? Thank you a lot!!!!

Python variable acting weird

I am making a pattern recogniser and i run into a VERY strange error, if the pattern is recognised it prints out ["x", "x", "2"] and i set variable worked to t. But when i print out worked on the last line it prints out ["x", "x", "x"]

(See the part of code marked # _____________ IMPORTANT __________ #)

    import random

    def check_pattern(pattern, rule):
        for i, RULE in enumerate(pattern):
            if RULE != "x":
                if rule[i] != RULE:
                    return False
        return True

    if __name__ == "__main__":
        numbers = [002,212,212,432,132,142,322,992,972,692,942,472]

        #numbers = [a.replace("\n", "") for a in numbers]

        print("Finding pattern in {}".format(numbers))

        patterns = []
        possible = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "x"]

        found = False
        pattern = ["x", "x", "x"]
        working = []
        worked = []
        for i, rule in enumerate(pattern):
            for attemptRule in possible:
                testRule = pattern
                testRule[i] = str(attemptRule)
                works = True

                for num in numbers:
                    numList = [a for a in num]
                    if check_pattern(testRule, numList):
                        pass
                        #print("Pattern {} works with {}".format(t, numList))
                    else:
                        works = False
                        #print("Pattern {} doesnt work with {}".format(t, numList))
                if works:
                    # ____________________ IMPORTANT ______________________ #
                    if testRule != ['x', 'x', 'x']:
                        print(testRule)                    
                        worked = testRule
                    # ____________________ IMPORTANT ______________________ #
                    working.append(testRule)
        #print("Pattern {} works".format(working))
    print(worked)

Thanks.

dimanche 1 janvier 2017

Angular JS ui-mask with type="password"

I am developing an app with input field, and for pattern using angular js ui-mask.

HTML

<input type="tel" class="input-field " data-ng-model="registrationField.debitcard" ui-mask="9999    9999    9999    9999" placeholder="Enter ATM/Debit Card Number"  ui-mask-placeholder ui-mask-placeholder-char="space" id="debitCardFld" ui-options="{clearOnBlur: false}"/>

  1. With type="text" the code works fine.
  2. But when we use type="password", on focus of the field, the spaces in pattern is also masked.

Only when the user types, it has to mask the characters.

Is there any way to achieve it?

Ref: http://ift.tt/1SlK9ax

Naming Convention for Adapter Pattern? Why is it TargetAdapter, not AdapteeAdapter?

In client code I program to Vehicle interface, so in my Client I have Vehicle interface, class Car implements Vehicle, class Airplane implements Vehicle.

So Client code is

public class ClientExample1 {

public void appLogic() {

    List<Vehicle> lst = new ArrayList<>();
    lst.add(new Car());
    lst.add(new Airplane());

    //now I want add Spaceship to the list.
    //Spaceship methods do the same logic, but they are named differently
    //so write adapter class and use it
    lst.add(new VehicleAdapter());

    for (Vehicle vehicle : lst) {
        vehicle.go();
        vehicle.stop();
    }

}

public static void main(String[] args) {
    new ClientExample1().appLogic();
}

}

I implement Adapter, for example thru inheritance (Class Adapter type):

    // we want use Spaceship in Client (ClientExample1)
//
// Adapter type: Class Adapter (inheritance of Spaceship)
// Naming convention for Adapter - TargetAdapter
//
// Target - Vehicle interface
// Target methods - methods of Vehicle interface
// Adaptee - Spaceship class
//spelling: adapter = adaptor. Both correct. Adapter - more often, both in US & GB

class VehicleAdapter extends Spaceship implements Vehicle {

    @Override
    public void go() {
        start(30, 30); //Spaceship method, same as go() but different name
    }

    @Override
    public void stop() {
        land(30, 30);
    }

}

My question is: why in all examples all over internet adapter is named after target class/interface (Vehicle in this example) - VehicleAdapter?

I think it is more sense to name adapter like this: class SpaceshipAdapter.

If I need to adapt another class UBoat to be used instead of Vehicle interface with (another) adapter, how shall I name such adapter? VehicleAdapter2 ? UBoatAdapter could have been a reasonable name.

So what are the best practise and why such (informal) naming convention exists, and shall programmers follow this convention?