mercredi 1 juin 2016

Actionscript 3, help required, pattern over drawn circle

http://ift.tt/1UeIm4u

This is what I am currently working on, and currently, I'm looking for a way (if any) to basically draw automatic, randomised asteroid belts.

The main idea I have, currently, would be to simply draw a circle with a pattern/stroke of asteroid sprites, but I have no idea how this could be achieved.

Ideally, the width and length of a circle of pattern would be randomised, then the sprites drawn.

Help would be greatly appreciated.

Current code to be found within the deviantart page.

Mostly looking for a way to paste objects over a circle I guess - such a function could be randomised, the circle behind "asteroid belt" wouldn't be particularly necessary. So either ability to draw a circle stroke of patterned sprites, or spawning sprites in a circular manner.

MVC 6 how to share Layout between projects?

I would like to create a base MVC 6 project (like Master Page) and reuse it in many child MVC 6 projects. In this base project i want to keep common JavaScript, CSS, Header, Footer, Authorization, etc.

How can I achieve it?

Thanks in advance for help.

PHP how to implement the Data Mapper pattern in MVC

I'm trying to implement and get my grasp on the Data Mapper pattern in my MVC project. I know this has been asked before, but I'm still not sure if I'm moving in the right direction. I've pasted my Controller, Datamapper and Domain Object/Entity code right below:

Controller

The user lands on http://ift.tt/14iuKTs and gets to see a list of events from the event log database table. The index() function creates an Datamapper instance and passes a the database wrapper. Then an HTML table is dynamically generated with data fetched from the Data mapper.

class DashboardController extends BaseController { 
    public function index() {
        require_once '../app/domain/mappers/EventMapper.php';
        require_once '../app/include/HTMLTableCreator.php';

        try {
            $eventMapper = new EventMapper(new Database('db'));
            $table = new HTMLTableCreator($eventMapper->findByLimit(20));
        } catch (DatabaseException $e) {

        }

        $this->view('/home/dashboard', ['table' => $table->toString()]);
    }   
}

Datamapper

This class contains the findByLimit() method for retrieving the last 20 log events from the database. The fetchCollection() method populates the Domain Entities and collects these in an array to be returned to the controller.

Should I create a generic collection function to be used by multiple Data mappers when the SQL query returns multiple rows (= multiple domain objects)? And can a Data mapper use any type of Data Source?

require_once '../app/domain/entities/Event.php';
require_once '../app/domain/interfaces/EventMapperInterface.php';

/**
 * EventMapper class
 */
class EventMapper implements EventMapperInterface {

    protected $dbh;
    protected $elements;

    public function __construct(Database $dbh) {
        $this->dbh = $dbh;
    }

    public function fetchCollection($objects) {
        foreach($objects as $object) {
            $event = new Event();
            $event->setId($object['id']);
            $event->setDateTime($object['datetime']);
            $event->setMessage($object['message']);
            $event->setHostname($object['hostname']);

            $this->elements[] = $event;
        }

        return $this->elements;
    }

    public function findByLimit($limit = 20) {
        $events = $this->dbh->sql_select('SELECT * FROM eventlog LIMIT ' . $limit, []);

        return $this->fetchCollection($events);
    }   
}

Domain Object/Entity

At last the domain object with some simple getters and setters. Would this be the place to do validation on the properties of a Domain Object?

class Event {

    protected $id;
    protected $dateTime;
    protected $message;
    protected $hostname;

    public function setId($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }

    public function setDateTime($dateTime) {
        $this->dateTime = $dateTime;
    }

    public function getDateTime() {
        return $this->dateTime;
    }  

    // rest omitted for clarity
}

Am I missing something in my implementation?

Is there a creational pattern for creating data objects based on a dependency?

I have a class that only contains data, but depends on a connection object from which the data is fetched. The connection object manages a low level socket connection to a much bigger API (old single header C API). Currently i have this:

public class MyData {
    private int data1;

    public MyData(Connection connection) {
         data1 = connection.getLowLevelConnection().someDataFetchCall();
    }

    // getters, setters
}

It works, but feels wrong. Intuitively i would say, that the data should be acquired in a function, which returns my initialized data object:

public class DataFetcher {
    public static MyData getMyDataFromConnection(Connection connection) { ... }
    // ... more data fetching functions
}

Is there a named pattern for this kind of task? That would make naming these classes easier.

How to implement abstract factory method in JavaScript?

So how to use abstract factory method in JavaScript? For example in Java:

public abstract class SuperClass {
    abstract String bar();

    public String foo() {
        return bar();
    }
}

public class SubClass extends SuperClass{
    @Override
    String bar() {
        return "bar";
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println(new SubClass().foo());
    }
}

It shows bar and just fine. But when I tried this in JavaScript:

var SuperClass = function () {};
SuperClass.prototype.foo = function () {
    return this.prototype.bar();
};

var SubClass = function () {};
SubClass.prototype = Object.create(SuperClass.prototype);
SubClass.prototype.constructor = SubClass;

SubClass.prototype.bar = function () {
    return "bar";
};

var myClass = new SubClass();
console.log(myClass.foo());

I get Uncaught TypeError: Cannot read property 'bar' of undefined. I tracked the bug and turns out, when SuperClass.prototype.foo is being executed, SubClass.prototype is still undefined.

So, what's the right way to do that? Thanks you!

"State pattern" vs "one member function per state"?

My class has 3 states. In each state it does some work, and goes to other state, or remains in the same state (in 95% or more cases it will stay in the same state). I can implement state pattern (I assume you know it). The alternative, which I pretty like, is this:

I have a member function per state, and also a pointer to member function, which points to the current state function. When in a state I want to go to another state, I just point that function pointer to another state function. (maybe this isn't completely equivalent to state pattern, but in my case it works fine).

Those two ways are almost identical, I think.

So, my questions are:

  1. Which solution is better (depends on what)?
  2. Is it worth to declare a class per state (which will have only one function)? I think that would be artificial.
  3. What about performance? isn't creating new object of state class (in case of state pattern) bring with it a slight overhead? (Sure state classes shouldn't have members, but anyway it should cost something)

XML schema restriction pattern for not allowing specific string

I have implement XSD schema with restriction in a field. Field doesn't a value with string FILENAME:

eg:

FILENAME

ORIGINFILENAME

FILENAMETEST

123FILENAME456

This values doesn't valid values.

This regular expression is valid for my case /^((?!FILENAME).)*$ but the XSD pattern does not support Negative Lookahead.

How implement XSD pattern restriction equal /^((?!FILENAME).)*$ ?

Thank you