OO Design of elevator system

Source: Internet
Author: User
Tags volatile

In theory, we should first black box use case, analysis needs, system boundary input and output, and then white box class diagram. But for the real-world simulation of OO, the personal feeling first emulate the real world, initially identify the relationship between classes and classes, and then use the use cases and sequence diagrams to enrich and modify the class diagram.

The primary principle of identifying classes is encapsulation, the manipulation of data and data encapsulated into a class:

Car box: The status of the package compartment: position, direction, stationary or motion, capacity, Pickup list, destination list

Lou building: The user requests the medium of the elevator, and the car box has a one-on relationship, to the car box call message, the car reached a certain level, to the building to send a layer of open message.

From the control-driven angle, there are 2 control flows (active objects)

1) Car box: Keep running, run or stop waiting for the signal according to the request

2) People: 1 is a person to building message, building forward to box;2) is a person to the car message, set to go to the destination floor

Similar to Vodcontroller, a resident thread inside, similar to a state machine running, and multiple points can receive asynchronous message update data.

The highlight is an event mechanism that triggers a positionchanged event every passing layer.

Summary: OO system is an object system that sends messages to each other, and objects interact with each other by message.

Common control Flow models:

1) sequential execution, exit

2) No dedicated resident thread, full message-driven, similar to the general Web Service,request/response mode.

3) There is a resident thread in run, (timed or controlled by the signal), but also can accept the asynchronous message Update data, for the community is called asynchronous message, because it only through the update data to affect the state machine operation, do not need to return what results. Work queue-based processing systems also belong to this model, but more simple, FIFO processing, the complexity of the elevator model is that the system will be based on all the current request to do a scheduling, not a simple FIFO.

Class Box {int direction;//0 up, 1 down, 2 stopped int currentpos;//current position int capacity;
	Max load final static int max_floor = 100; 
	Volatile Boolean destinations[] = new Boolean[max_floor]; Volatile Boolean pickups[][] = new BOOLEAN[MAX_FLOOR][2]; 0 for up, 1 for down int highestto =-1; The farest upper floor to go int lowestto =-1; The farest lower floor to go volatile boolean on = true;
	On or off Building Building;
	int numrequest = 0;
	
	Object signal = new Object (); public void call (int floor, int direction) {synchronized (signal) {if (!pickups[floor][direction]) {PICKUPS[FL
				Oor][direction] = true;
				++numrequest;
				if (Highestto < 0 | | floor > Highestto) Highestto = floor;
				if (Lowestto < 0 | | Floor < lowestto) Lowestto = floor;
			Signal.notify (); }}} public void setdestination (int floor) {synchronized (signal) {if (!destinations[floor]) {Destinat
				Ions[floor] = true; ++numrequest;
			Signal.notify (); 
		}}} public void Shutdown () {on = false;
		Synchronized (signal) {signal.notify (); }} private void Open () {building.open (currentpos);/*first Open the building door, then the box ' s*/} private void
	Close () {building.close (currentpos);} private void Stop () {} private void positionchanged () {if (Pickups[currentpos][direction] | | destinations[currentpos]
			) {Stop ();
			Open ();
			Thread.Sleep (5000);
			Close ();
				Synchronized (signal) {if (Pickups[currentpos][direction]) {pickups[currentpos][direction] = false;--numrequest;}
				if (Destinations[currentpos]) {Destinations[currentpos] = false;--numrequest;}
				if (Highestto = = currentpos) Highestto =-1;
			if (Lowestto = = currentpos) Lowestto =-1;
		}//resume (); }} private void Run () {while (on) {synchronized (signal) {if (numrequest = = 0) {direction = 2;//sto Pped signal.wait (); Wait signal here if no jobs to do}//decide up or down, handle upper request first. if (Highestto > 0) direction = highestto > Currentpos?
				0:1; else if (Lowestto > 0) Direction = Lowestto < Currentpos?
			1:0;
					} if (direction = = 0) {//up for (int i = Currentpos + 1; I <= Highestto; ++i) {++currentpos;
				PositionChanged ();
					}} else if (direction = = 1) {//down for (int i = currentPos-1; I >= Lowestto; i.) {--currentpos;
				PositionChanged ();
}}}}}//Building building: The user requests the medium of the elevator, and the car box has a one-to-two relationship, to the car box call message, the car reached a certain level, to the buildng to send a layer of open message.
	Class Building {box box;
	public void call (Int. floor, Int. direction) {Box.call (floor, direction);} public void open (int floor) {} public void close (int floor) {}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.