Object-oriented encapsulation

Keywords: less

Encapsulation is to hide implementation details and provide public access. Encapsulation can bring many benefits, improve code reusability and security. His design principle is to hide details that do not want to be known to the outside world and provide public access.

private is an embodiment of packaging.

Let's take an example to abstract an analog radio class, which has many attributes (simplification):

package com.henu;
/**
 * A simulated radio
 * @author Administrator
 *
 */
public class Radio {
	// Current Channel
	private static int currentChannel = 1;
	// The Last Channel
	private static int lastChannel;
	// Battery Percentage
	private static double powerPercent = 1.0;
	// Current volume
	private static int currentVolume = 5;

 

You have this radio, observe the appearance of the radio, there are many buttons, including many functions, buttons may be turned on, off, pause, restore, switch, charge and so on. Click on the phone, the radio will turn on the power and start to play the program according to your instructions. However, what he does in it is far less simple than "press the button". However, you don't need to know so much. You just press the button, and the radio will serve you. The buttons you press are the access interface provided by the radio object to the outside world (without discussing Iterface). What functions the radio has, how many external access interfaces it has. This interface to the user is a bridge built by the radio to the user and the internal structure of the radio. These user-to-user interfaces are methods declared as public, and the outside world (users) can call these public methods through the radio class.

Following are some of the external public methods of radio (simplification, examples): _____________

// Turn on
	public void powerOn() {
		// 1. Initialization
		init();
		// 2. Hardware Detection
		hardWareCheck();
		// 3. Restore to the last shutdown channel
		recoverLastChannel();
	}

	// Shut down
	public void powerOff() {
		// Save Channel Records
		saveChannel();
		// Cut off the power supply
		cutOffPower();
	}

	// suspend
	public void pause() {
		// Save Channel Records
		saveChannel();
	}

	// recovery
	public void recover() {
		// Restore to the last channel
		recoverLastChannel();
	}

	// Changing stations
	public void switchChannel(int channel) { // Parameters: Selected platform
		// Method of Choosing Platform
		chooseChannel(channel);
	}

	// Charge
	public void charge() {
		// Save information
		saveChannel();
		// Shut down
		cutOffPower();
		// Charge
		pushPowerUp();
	}

	// increase volume
	public void increaseVolume() {
		// Volume rise
		pushVolumeUp();
	}

	// Reduce volume
	public void decreaseVolume() {
		// Lower volume
		pushVolumeDown();
	}

	// Soutai
	public void searchChannels() {
		// The Specific Method of Searching Taiwan
		searchAllChannels();
	}

As you can see, many methods are called in these public methods that can be called by users, and these methods are private methods in this radio class. These private methods are not exposed to users, and users can not access these private methods directly.

Some private methods (simplification, example):

	// Private initialization methods
	private void init() {
		// Initialization method of simulation
		System.out.println("---Start initialization...---");
	}

	// Private hardware detection
	private void hardWareCheck() {
		// Analog Hardware Detection Method
		System.out.println("---Initial Hardware Detection...---");
	}

	// A Private Way to Restore the Last Channel
	private void recoverLastChannel() {
		// A Method of Simulating the Recovery of Last Record
		System.out.println("---Restore the last listening record and enter the default channel...---");
		if (lastChannel == 1) {
			System.out.println("Welcome to the Central Set........");
		} else {
			System.out.println("Welcome to the Second Set of the Central Committee........");
		}
	}

	// Keep a private record of the last shutdown
	private void saveChannel() {
		// A Method of Simulating the Record of the Last Shutdown
		System.out.println("---Keep this record...---");
		// The last one is the present one.
		lastChannel = currentChannel;
	}

	// Private power cut-off
	private void cutOffPower() {
		// simulation
		System.out.println("---Cut off the power supply...---");
	}

	// Private charging methods
	private void pushPowerUp() {
		System.out.println("---Charging...---");
		while (powerPercent != 100.0) {
			currentVolume += 0.1;
		}
		System.out.println("---Charging completed!---");
	}

	// Jump to the appropriate channel according to the selected station
	private void chooseChannel(int channel) {
		// simulation
		currentChannel = channel;
		switch (currentChannel) {
		case 1:
			System.out.println("Welcome to the Central Set........");
			break;
		case 2:
			System.out.println("Welcome to the Second Set of the Central Committee........");
			break;
		default:
			break;
		}
	}

	// Volume Up
	private void pushVolumeUp() {
		if (currentVolume < 10) {
			currentVolume++;
		}
	}

	// Reduce volume
	private void pushVolumeDown() {
		if (currentVolume > 0) {
			currentVolume--;
		}
	}

	// Soutai
	private void searchAllChannels() {
		//... Some methods of searching Taiwan
		System.out.println("Search is over!There are one central set and two central sets.");
	}

These private methods encapsulate some actions that directly operate the radio. For example, if the user wants to turn on, press the boot button and call the powerOn() method, in the view of the outside user, to turn on is to press a button, but inside the radio, he may first initialize, then check his own hardware, restore to the channel when the last shutdown, and so on. Maybe these actions are: Dozens of hundreds, very complex, these complex modules are written in a private method one by one, but the user can not see, this is encapsulation.

So why do we encapsulate it? Why can't we write the boot method directly into powerOn()? Why bother to package them and call them one by one?

First, security.

Assuming that the user is a small white one using the radio, then you put all these methods of initialization, hardware detection and so on in powerOn() public method, it is as if you have exposed many lines inside the radio to the user, the user does not know what your lines are used for and does not know what they are used for. If he operates forcibly, he may destroy the radio. It is very dangerous to do so. Therefore, the wires, capacitors and circuit boards with internal functions must be encapsulated inside the radio and become the private method of each module. Users don't need to know that you want to initialize, that you want to detect the circuit, they just need to know, press the boot button, boot, that's all.

Second, reusability

For example, the radio and restore function on boot and restore function on pause, both of which need to be restored to the channel when the radio was last saved, then the private method restored to the channel recoverLastChannel() listened to last time can be invoked simultaneously by the powerOn() boot method and the restore () restore method, which is called at the same time by the powerOn() boot method and the restore () restore method. In this way, code reuse can be achieved. If you write the recovery channel code directly in powerOn(), you still need to write it again in the recovery () method.

As an object, as a finished object or system, its interior may be very complex, but its interface exposed to users must not be complex. Users are not concerned about how complex and amazing your system is. Users only care about what can be as simple as possible. In this case, use this item to use the system to fulfill the user's desired needs. Furthermore, the user presses a button, or clicks a button, and the subsequent function is not provided by the button itself, but the button acts as a central trigger to trigger, dispatch collaboration between other modules to complete this function. This button is the external method of public. It is a bridge that connects users and systems, user interface method and internal module.

As a developer, we want a system to run safely and efficiently. We want users to operate the system according to the rules we set. We need to encapsulate the system and use the external interface of public to regulate users'behavior. For example, radio, we will not let users directly. Switching on the operating resistance changes the volume of the radio. We require the user to adjust the volume through the volume key. Then we will set the volume current Volume as private. Only by accessing the increaseVolume () and decreaseVolume () users can change the volume. The radio is the developer. Designed, so developers must not let users directly change the radio properties and content, only through the developer authorized safe way, users can change. Ensure the normal and safe operation of the whole system.

These are some ideas about packaging.

Posted by curtis_b on Wed, 17 Jul 2019 16:42:12 -0700