[Java] Using the interface

There are many issues raised, such as user interface to do, why use interface, How to use interface,… In this article I hope to be able to answer somewhat understandable as possible because it is usually relatively abstract =)) and also say what is written below is part of my limited understanding, if you can supplement is something really great for me as well as you read tracking.

1. Use Interface joint action to create objects

For example, always offline. Imagine that every day you learn a lot of subjects, Mathematics, Ly, Chemistry, Born, English, Van, History,… each subject a book or play, so really inconvenient when you “composed of” to class. Now I present to you a way that does not need to know today what is studied with the goal of which come to class, that use of versatile that can accommodate all your subjects. Themselves are made that way by all subjects in 1 versatile notebook, school only just picked it off is finished =)).

In programming, the book (register) versatile interface that is your. Now start trial offline. You Mathematics, Ly, Chemistry, Born,.. like I just mentioned, each module is a class. You have an array of books and need to bring it to school and we try to see how we normally?

package cachhoc.net.useinterface.method;

class Toan {
	public void mangSachToiTruong() {
		System.out.println("Sach Toan duoc mang toi truong");
	}
}

class Ly {
	public void mangSachToiTruong() {
		System.out.println("Sach Ly nhe");
	}
}

class Hoa {
	public void mangSachToiTruong() {
		System.out.println("Sach Hoa ok");
	}
}

class Sinh {
	public void mangSachToiTruong() {
		System.out.println("Sach Sinh day roi");
	}
}

public class TruongHoc {
	public static void main(String[] args) {
		Object[] sach = { new Toan(), new Ly(), new Hoa(), new Sinh() };
		for (Object s : sach) {
			s.mangSachToiTruong(); // loi roi nhe
		}
	}
}

You go try code, you will see is to create arrays sach we no other way to put it is the Object array and call the method mangSachToiTruong() will not be possible.

Now the interface will help us to solve this problem. First we create an interface Books there is method mangSachToiTruong();

package cachhoc.net.useinterface.method;

public interface Sach {
	public void mangSachToiTruong();
}

Then we create the class Toan, Glass, Flower, Born and executable implements methods Books mangSachToiTruong and problems are resolved as follows.

package cachhoc.net.useinterface.method;

class Toan implements Sach {
	@Override
	public void mangSachToiTruong() {
		System.out.println("Sach Toan duoc mang toi truong");
	}
}

class Ly implements Sach {
	@Override
	public void mangSachToiTruong() {
		System.out.println("Sach Ly nhe");
	}
}

class Hoa implements Sach {
	@Override
	public void mangSachToiTruong() {
		System.out.println("Sach Hoa ok");
	}
}

class Sinh implements Sach {
	@Override
	public void mangSachToiTruong() {
		System.out.println("Sach Sinh day roi");
	}
}

public class TruongHoc {
	public static void main(String[] args) {
		Sach[] sach = { new Toan(), new Ly(), new Hoa(), new Sinh() };
		for (Sach s : sach) {
			s.mangSachToiTruong();
		}
	}
}

2. Data transmission, messages between the class

It is often said that the class data exchange, message through the interface. In this section we will learn it. This issue will be clearer if we do on the interface, ie using the JFrame. But if you do not interface well with no stars.

You have 2 class is GiaoDien and Download. GiaoDien class has a button, Clicking on the download method call in class Download to download the file, and during download, class download to get back to class GiaoDien know how much is download and. You see 2 the following methods of yourselves.

How to 1 (How old corn or used batons): Communication objects for the class interface to update Download percent downloaded.

package cachhoc.net.useinterface.senddata;

public class GiaoDien {
	private Download download;

	/**
	 * phuong thuc khoi tao Giao dien, dong thoi khoi tao doi tuong download va
	 * truyen chinh giao dien sang class Download de cap nhat giao dien
	 */
	public GiaoDien() {
		download = new Download(this);
	}

	private void nhanDownload() {
		download.download();
	}

	public void capNhatGiaoDien(int phanTramDownload) {
		System.out.println("class giao dien cap nhat duoc: " + phanTramDownload
				+ "%");
	}

	public static void main(String[] args) {
		GiaoDien giaoDien = new GiaoDien();
		giaoDien.nhanDownload();
	}
}
package cachhoc.net.useinterface.senddata;

public class Download {
	private GiaoDien giaoDien;

	/**
	 * @param giaoDien
	 *            de cap nhat giao dien
	 */
	public Download(GiaoDien giaoDien) {
		this.giaoDien = giaoDien;
	}

	public void download() {
		for (int i = 0; i < 100; i++) {
			System.out.println("Dang download...");
			giaoDien.capNhatGiaoDien(i);
		}
		System.out.println("Ket thuc download");
	}
}

As you have seen, the updated interface as simple proved that we just passed the interface to the class object is finished Download.
However, many problems occur when the object is passed to giaoDien, download class is allowed to call a method of the class of which it is not necessary GiaoDien, updated interface methods required to allow calls in class Download it can be called that here alone to the public and not the private. Or suppose that the class we have to go through a certain class X in the middle of class, the method download Download new call and so we have to transfer to the X GiaoDien, X then transmitted to Download, Such complex… and many other restrictions that will actually make you a better understanding.

How to 2: Use Interface

We create an interface called CapNhat contains methods capNhatGiaoDien(); GiaoDien then implements the following CapNhat:

package cachhoc.net.useinterface.senddata;

public interface CapNhat {
	public void capNhatGiaoDien(int giaTri);
}
package cachhoc.net.useinterface.senddata;

public class Download {
	private CapNhat capNhat;

	/**
	 * @param capNhat
	 *            lang nghe su kien download
	 */
	public void addDownloadListener(CapNhat capNhat) {
		this.capNhat = capNhat;
	}

	public void download() {
		for (int i = 0; i < 100; i++) {
			System.out.println("Dang download...");

			/**
			 * Khi goi capNhat.capNhatGiaoDien(i); thi phuong thuc nay se tu
			 * dong duoc goi trong giao dien
			 * */

			capNhat.capNhatGiaoDien(i);
		}
		System.out.println("Ket thuc download");
	}
}
package cachhoc.net.useinterface.senddata;

public class GiaoDien implements CapNhat {
	private Download download;

	/**
	 * phuong thuc khoi tao Giao dien, dong thoi dat lang nghe su kien download
	 */
	public GiaoDien() {
		download = new Download();

		/** dat lang nghe su kien download */
		download.addDownloadListener(this);
	}

	private void nhanDownload() {
		download.download();
	}

	/**
	 * Tu dong duoc goi trong qua trinh download thong qua interface CapNhap
	 */
	@Override
	public void capNhatGiaoDien(int giaTri) {
		System.out.println("class giao dien cap nhat duoc: " + giaTri + "%");
	}

	public static void main(String[] args) {
		GiaoDien giaoDien = new GiaoDien();
		giaoDien.nhanDownload();
	}
}

You can see that the way 2 This overcomes the disadvantages of the classic way 1. Class does not need to download TV audience that just one interface GiaoDien CapNhat, therefore the method, calculation of non-drug GiaoDien “exposed” or may be referred to the Download. If you have passed a certain class X and then download the new implementation does not need to pass through the object GiaoDien intermediate class that has been updated interface during download. Or suppose there are many interfaces need to be updated during the download, it is indifferent as to how 1 would be more difficult because you need to pass all classes that interface to Download…

Above is 2 Interface applications they use most visible. We hope it can be helpful for you in the process learn how to use interface.

Source article at cachhoc.net