Interfaces

// http://www.yusufaytas.com/achieving-abstraction-in-javascript/
//ItemRepo interface 
var ItemRepo = {
    addItem: function(item) {
    },
    removeItem: function(id) {
    },
    getItem: function(id) {
    }};
 
 
var ItemRepoAjax = function(url) {
    this.url = url;
};
//Extend the ItemRepo for Ajax 
ItemRepoAjax.prototype = Object.create(ItemRepo);
ItemRepoAjax.prototype.addItem = function(item) {
    window.console.log("adding item");
};
 
 
var ItemController = function(itemRepo) {
    this.itemRepo = itemRepo;
 
    this.add = function(item) {
        this.itemRepo.addItem(item);
    }
};
 
var itemController = new ItemController(new ItemRepoAjax("url"));
itemController.add({item: "myItem"});
 
 
var Logger = {
    log: function(log) {
    }
};
var ConsoleLogger = function() {
};
ConsoleLogger.prototype = Object.create(Logger);
ConsoleLogger.prototype.log = function(log) {
    //actual logging code
}
 
// Define new Controller 
var LoggingItemController = function(itemRepo, logger) {
    this.itemRepo = itemRepo;
    this.logger = logger;
}
LoggingItemController.prototype = Object.create(ItemController);
LoggingItemController.prototype.add = function(item) {
    this.logger.log("start");
    this.itemRepo.addItem(item);
    this.logger.log("stop");
};
 
 
var loggingItemController = new LoggingItemController(new ItemRepoAjax("url"), new ConsoleLogger());
loggingItemController.add("item");
 
/disk_1/www/cz/ladariha/wiki/data/pages/programovani/javascript/interface.txt · Poslední úprava: 2013/07/07 22:27 autor: ladariha
 
Kromě míst, kde je explicitně uvedeno jinak, je obsah této wiki licencován pod následující licencí:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki