SEEM4570 System Design and Implementation Lecture 11 From Design to Implementation
Introduction We learned programming and we learned UML, but separately. Now, the question is how can we translate a design specification from UML to Implementation (i.e. programming)? In this lecture, we will focus on class diagram and use PHP for demonstration. PHP is chosen because its syntax is easy to understand and initiative. 2017. Gabriel Fung. 2
Example 1 class { private $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; - name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // Error! name is private! 2017. Gabriel Fung. 3
Example 2 class { public $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; + name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // output John 2017. Gabriel Fung. 4
Example 3 class { protected $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; # name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // Error! name is protected! 2017. Gabriel Fung. 5
Note 1 By setting the variables into different kinds of visibilities (i.e. private, protected and public), we can control how the other programs can access the variables, and avoid other programs accidentally changed the variable. In Example 1, this will return an error $person->name = Mary ; So there is no valid way to change the name of the person. However, in Example 2, this is valid $person->name = Mary ; // the name is changed to Mary now For protected, we will explain it very soon. 2017. Gabriel Fung. 6
Example 4 class Staff extends { private $salary; public function construct(salary, name){ $this->salary= $salary; $this->name = $name; public function getsalary(){ return $this->salary; $staff = new Staff( John ); echo $staff->getname(); // output John Staff - salary int echo $staff->name; // Error! name is private! # name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 7
Example 5 class Staff extends { private $salary; public function construct(salary, name){ $this->salary= $salary; $this->name = $name; // invalid! public function getsalary(){ return $this->salary; Staff - salary int - name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 8
Note 2 Did you know why it is valid to use $this->name (line 6) in Example 4? It is because name is a protected variable in the parent class, so that any classes that extend it can access this protected variable directly. To make Example 5 valid, we need to change the code a little bit. See the next eample. 2017. Gabriel Fung. 9
Example 6 class Staff extends { private $salary; public function construct(salary, name){ $this->salary = $salary; parent construct(name); // valid public function getsalary(){ return $this->salary; Staff - salary int - name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 10
Example 7 interface { public function getname(); class Staff implements { private $name; private $salary; public function construct(salary, name){ $this->salary = $salary; $this->name = $name; public getname(){ return $this->name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 11
Example 8 interface { public function getname(); // The class below is invalid! It did not // have the method getname() class Staff implements { private $name; private $salary; public function construct(salary, name){ $this->salary = $salary; $this->name = $name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 12
Example 9 // The class below is invalid! // An interface cannot contain // implementation detail interface { private $name; public function getname(){ return $this->name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 13
Note 3 In interface, we should not have any variable and should not have any constructor. We should not have any implementation detail as well. If you need to contain variables and implementation detail, you should write abstract class. Why interface is useful? It can make sure all classes that implemented a particular interface must contain all the methods specified in the implemented interface. 2017. Gabriel Fung. 14
Example 10 abstract class { private $name; public function getname(){ return $this->name; class Staff extends { Note Italic! + getname() string Staff - name string - salary int + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 15
Note 4 Why interface and abstract class are useful? It can make sure all classes that implemented a particular interface must contain all the methods specified in the implemented interface. 2017. Gabriel Fung. 16
Example 11 class { class Building{ private $securityguard; public function construct($guard){ $this->securityguard = $guard; Building - securityguard $person = new ( John ); $building = new Building($person); 2017. Gabriel Fung. 17
Example 12 class Room{ class Building{ private $room; public function construct($room){ $this->room = new Room(); Room Building - room Room 2017. Gabriel Fung. 18
Note 5 Can you identify the major difference between aggregation and composition? In aggregation, when an object that contains other objects is destroyed, those objects that are used inside will not destroy. In composition, when an object is destroyed, all associated objects will be destroyed as well. 2017. Gabriel Fung. 19
Example 13 // This is correct class {... class Printer{... public function print(person){... Printer + print(person ) 2017. Gabriel Fung. 20
Example 14 // This is incorrect class {... public function print(printer){... class Printer{... public function print(person){... + print(printer Printer) Printer + print(person ) 2017. Gabriel Fung. 21
Example 15 // This is also incorrect class {... public function x(){ $printer = new Printer(); class Printer{... public function print(person){... + x() Printer + print(person ) 2017. Gabriel Fung. 22
Note 6 When the relationship is uni-directional from A to B (i.e. A > B), you should never refer to B from A. 2017. Gabriel Fung. 23
Example 16 class {... public function getname(){ return $this->name; Professor class Professor{... + getname() string // override the previously defined method public function getname(){ return Prof.. $this->name; + getname() string 2017. Gabriel Fung. 24
Visibility and Encapsulation In UML class diagram, the symbol #, + and are known as visibility. In OOP, it is usually called Encapsulation. The ability of an object to hide its data and methods from the rest of the world The internal representation of an object is generally hidden from view outside of the object's definition. One of the fundamental principles of OOP. 2017. Gabriel Fung. 25
Realization and Data Abstraction Example 7 and Example 10 are two examples of realization. In OOP, we usually call this as Data Abstraction 2017. Gabriel Fung. 26
Generalization and Inheritance Example 6 and Example 10 are two examples of generalization. You can use one class as the basic for another class. Multiple classes may share some of the same characteristics, and have things in common with one another, but also may have certain properties that make them different. In OOP, this is usually known as Inheritance. 2017. Gabriel Fung. 27
Polymorphism In Example 16, we override a method that is defined in the parent class. In OOP, this is known as Polymorphism. Polymorphism is the ability to create a variable, a function, or an object that has more than one form. The most common cases are Method Overriding Re-define the method in a subclass (Example 16) Method Overloading (Not in PHP) Define some methods with the same name but different number of arguments 2017. Gabriel Fung. 28
More About OOP Encapsulation, Data Abstraction, Inheritance and Polymorphism are four major properties of OOP. Note that not all OOP must necessarily contains all these properties. 2017. Gabriel Fung. 29