sábado, 7 de diciembre de 2019

Dependency Injection, IoC (Inversion of Control)

https://github.com/jalbertomr/SimpleDependencyInjectionIoC.git

Without IoC

package com.bext;
public class IoC {

    public static void main(String[] args) {
        IoC container = new IoC(); 
        User user = container.new User(); 
        user.add( "Info to persist"); 
    }

    public class User {
        MySqlDatabase database;
 
        public User() {
            this.database = new MySqlDatabase();         
        }

        void add( String data) {
            database.persist( data);         
        }
    }

    public class MySqlDatabase {
        public void persist(String data) {
            System.out.println("MySqlDatabase persisted [" + data + "]");         
        }
    }

}



Implementing Inversion of Control (IoC) , Now instance of database can be accessed for use for tests.

package com.bext;
public class IoC {

    public static void main(String[] args) {
        IoC container = new IoC(); 
        User user = container.new User( container.new MySqlDatabase()); 
        user.add( "Info to persist"); 
    }

    public class User {
        MySqlDatabase database;
 
        public User( MySqlDatabase database) {
            this.database = database; 
        }

        void add( String data) {
            database.persist( data); 
        }
    }

    public class MySqlDatabase {
        public void persist(String data) {
            System.out.println("MySqlDatabase persisted [" + data + "]"); 
        }
    }

}


Loosely coupled Architecture to define de database to use, in this case MySqlDatabase

package com.bext;
public class IoC {

    public static void main(String[] args) {
        IoC container = new IoC(); 
        User user = container.new User( container.new MySqlDatabase()); 
        user.add( "Info to persist");    }

    public class User {
        Database database; 
        public User( Database database) {
            this.database = database;         
        }

        void add( String data) {
            database.persist( data);         
        }
    }

    interface Database {
        public void persist(String data);     
    }

    public class MySqlDatabase implements Database {
        public void persist(String data) {
            System.out.println("MySqlDatabase persisted [" + data + "]"); 
        }
    }

    public class OracleDatabase implements Database {
        public void persist(String data) {
            System.out.println("OracleDatabase persisted [" + data + "]"); 
        }
    }
}



The Run shows:

MySqlDatabase persisted [Info to persist]

The same  but now for use Oracle Database

package com.bext;
public class IoC {

    public static void main(String[] args) {
        IoC container = new IoC();         
        User user = container.new User( container.new OracleDatabase()); 
        user.add( "Info to persist"); 
    }

    public class User {
        Database database;
        public User( Database database) {
            this.database = database;        }

        void add( String data) {
            database.persist( data);        }
    }

    interface Database {
        public void persist(String data);    }

    public class MySqlDatabase implements Database {
        public void persist(String data) {
            System.out.println("MySqlDatabase persisted [" + data + "]");        }
    }

    public class OracleDatabase implements Database {
        public void persist(String data) {
            System.out.println("OracleDatabase persisted [" + data + "]");        }
    }
}


The Run shows:

OracleDatabase persisted [Info to persist]

eot

No hay comentarios:

Publicar un comentario