JAVA uml Based cording <<< by jps sasadara >>>
COMPOSITION TUTORIAL
The term composition is not unique to Java, composition is a
concept of object-oriented programming. By now you should be familiar with the
concept of inheritance -
inheritance requires the use of the Java keywords extends or implements. When your class inherits members from either
a superclass or a superinterface, you can directly invoke or access those
members.
Consider the following code that demonstrates inheritance:
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class Computer extends OperatingSystem {
}
class TestMe {
public static void main(String args[]){
Computer c = new Computer();
c.bootUp();
c.shutDown();
}
}
In the example above how many objects are created in the statement Computer c = new Computer();? The answer is just one single instance of a Computer object - pretty basic.
Consider the following code that demonstrates inheritance:
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class Computer extends OperatingSystem {
}
class TestMe {
public static void main(String args[]){
Computer c = new Computer();
c.bootUp();
c.shutDown();
}
}
In the example above how many objects are created in the statement Computer c = new Computer();? The answer is just one single instance of a Computer object - pretty basic.
COMPOSITION
In the real world objects are often composed of other objects. A
computer is composed of many objects: case, motherboard, power supply, drive
(ssd, platter, etc), memory, OS, etc. Composition, simply put, is when an
object is composed of other objects.
Consider the following code that demonstrates composition:
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class PowerSupply {
void turnOn() { ... }
void turnOff() { ... }
}
class Computer{
private OperatingSystem os = new OperatingSystem();
private PowerSupply ps = new PowerSupply();
Computer() {
ps.turnOn();
os.bootUp();
}
void allDone() {
os.shutDown();
ps.turnOff();
}
}
class TestMe {
public static void main(String args[]){
Computer c = new Computer();
// use computer...
c.allDone();
c = null; // critical for future aggregation tutorial
}
}
In the example above how many objects are created in the statement Computer c = new Computer();? There are a total of three objects created after that statement executes. The computer object is the sum of all of its parts, in other words, the computer is composed of its parts - hence the meaning of composition. When the statement c = null; is executed I am destroying the computer object, since the computer is composed of other objects (OperatingSystem and PowerSupply) they are destroyed right along with the computer. There are no references pointing to the the Computer object, OperatingSystem object, or the PowerSupply object, so all three objects are eligible for garbage collection at that point. The concept of composition is a critical piece of the puzzle when I begin to introduce you to design patterns.
Consider the following code that demonstrates composition:
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class PowerSupply {
void turnOn() { ... }
void turnOff() { ... }
}
class Computer{
private OperatingSystem os = new OperatingSystem();
private PowerSupply ps = new PowerSupply();
Computer() {
ps.turnOn();
os.bootUp();
}
void allDone() {
os.shutDown();
ps.turnOff();
}
}
class TestMe {
public static void main(String args[]){
Computer c = new Computer();
// use computer...
c.allDone();
c = null; // critical for future aggregation tutorial
}
}
In the example above how many objects are created in the statement Computer c = new Computer();? There are a total of three objects created after that statement executes. The computer object is the sum of all of its parts, in other words, the computer is composed of its parts - hence the meaning of composition. When the statement c = null; is executed I am destroying the computer object, since the computer is composed of other objects (OperatingSystem and PowerSupply) they are destroyed right along with the computer. There are no references pointing to the the Computer object, OperatingSystem object, or the PowerSupply object, so all three objects are eligible for garbage collection at that point. The concept of composition is a critical piece of the puzzle when I begin to introduce you to design patterns.
C:\Windows\System32>cd
\
C:\>md
Java
C:\>cd
Java
C:\Java>
C:\Java>md
Composition
C:\Java>cd
Composition
C:\Java\Composition>Notepad
Composition.java
Copy and Paste, or
type the following code into Notepad and be sure to save the file when you are
done.
class Composition {
public static void main(String args[]){
Computer c = new Computer();
System.out.println("\nBrowing the internet...");
System.out.println("Writing some source code...");
System.out.println("Saving work...\n");
c.allDone();
c = null; // critical for future aggregation tutorial
}
}
class OperatingSystem {
void
bootUp() { System.out.println("OS is booting up"); }
void
shutDown() { System.out.println("OS is shutting down"); }
}
class PowerSupply {
void
turnOn() { System.out.println("Powering on"); }
void
turnOff() { System.out.println("Powering off"); }
}
class Computer {
private OperatingSystem os = new OperatingSystem();
private PowerSupply ps = new PowerSupply();
Computer() {
ps.turnOn();
os.bootUp();
}
void
allDone() {
os.shutDown();
ps.turnOff();
}
}
Now switch back to the command prompt (CMD) and type in javac Composition.java and press Enter.
Now type in java Composition and press Enter.
Now type in java Composition and press Enter.
C:\Java\Composition>javac
Composition.java
C:\Java\Composition>java
Composition
Powering
on
OS
is booting up
Browing
the internet...
Writing
some source code...
Saving
work...
OS
is shutting down
Powering
off
FINAL THOUGHTS
As you learn more
about design patterns you will discover that composition is more versatile than
inheritance and that you should use composition instead of inheritance whenever
possible. Stay tuned for my next tutorial on Aggregation. Aggregation is very
similar to Composition only with one very distinct difference.
AGGREGATION TUTORIAL
I highly recommend watching my Composition Tutorial prior to watching this one. We learned
that an object demonstrating composition is composed of other objects that are
created inside of the object
during instantiation. The lifetime of those "internal" objects are
entirely dependent upon the enclosing object. When the instance of the
enclosing object is destroyed, those "internal" objects are destroyed
as well. Aggregation is very similar to composition only with one key
difference. An object demonstrating aggregation is composed of other objects
that are created outside of the object
during instantiation. In aggregation, the lifetime of those
"internal" objects are not dependent upon the enclosing object. When
the instance of the enclosing object is destroyed, those "internal"
objects are still alive because they were created elsewhere. Consider the
following code that demonstrates aggregation:
class MegaOfficeSuiteDVD {
void installSoftware( ... );
void viewShinySurface() { ... }
}
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class PowerSupply {
void turnOn() { ... }
void turnOff() { ... }
}
class DVDDrive {
void open() { ... }
void close() { ... }
}
class Computer{
private OperatingSystem os = new OperatingSystem(); // composition
private PowerSupply ps = new PowerSupply(); // composition
private DVDDrive drive = new DVDDrive(); // composition
private MegaOfficeSuiteDVD dvd; // reference only - potential for aggregation
Computer(MegaOfficeSuiteDVD dvd) {
this.dvd = dvd; // aggregation
ps.turnOn();
os.bootUp();
}
void dvdStuff() {
//open dvd, place dvd in drive, close dvd
dvd.installSoftware();
//open dvd, remove dvd, close dvd
}
void allDone() {
os.shutDown();
ps.turnOff();
}
}
class TestMe {
public static void main(String args[]){
MegaOfficeSuiteDVD dvd = new MegaOfficeSuiteDVD();
Computer c = new Computer(dvd);
// Browing the internet....
// "Writing some source code...
c.dvdStuff();
c.allDone();
c = null; // The computer object and all the composition objects are destroyed and eligible for garbage collection
dvd.viewShinySurface(); // it's alive!
}
}
In the example above the difference between composition and aggregation becomes apparent when the statement c = null; is invoked. The MegaOfficeSuiteDVD object continues to live after the Computer object and its composition objects are destroyed. At one time the Computer object did consist of a MegaOfficeSuiteDVD, but it was merely just a reference to the actual object created outside of the computer object.
class MegaOfficeSuiteDVD {
void installSoftware( ... );
void viewShinySurface() { ... }
}
class OperatingSystem {
void bootUp() { ... }
void shutDown() { ... }
}
class PowerSupply {
void turnOn() { ... }
void turnOff() { ... }
}
class DVDDrive {
void open() { ... }
void close() { ... }
}
class Computer{
private OperatingSystem os = new OperatingSystem(); // composition
private PowerSupply ps = new PowerSupply(); // composition
private DVDDrive drive = new DVDDrive(); // composition
private MegaOfficeSuiteDVD dvd; // reference only - potential for aggregation
Computer(MegaOfficeSuiteDVD dvd) {
this.dvd = dvd; // aggregation
ps.turnOn();
os.bootUp();
}
void dvdStuff() {
//open dvd, place dvd in drive, close dvd
dvd.installSoftware();
//open dvd, remove dvd, close dvd
}
void allDone() {
os.shutDown();
ps.turnOff();
}
}
class TestMe {
public static void main(String args[]){
MegaOfficeSuiteDVD dvd = new MegaOfficeSuiteDVD();
Computer c = new Computer(dvd);
// Browing the internet....
// "Writing some source code...
c.dvdStuff();
c.allDone();
c = null; // The computer object and all the composition objects are destroyed and eligible for garbage collection
dvd.viewShinySurface(); // it's alive!
}
}
In the example above the difference between composition and aggregation becomes apparent when the statement c = null; is invoked. The MegaOfficeSuiteDVD object continues to live after the Computer object and its composition objects are destroyed. At one time the Computer object did consist of a MegaOfficeSuiteDVD, but it was merely just a reference to the actual object created outside of the computer object.
C:\Windows\System32>cd
\
C:\>md
Java
C:\>cd
Java
C:\Java>
C:\Java>md
Aggregation
C:\Java>cd
Aggregation
C:\Java\Aggregation>Notepad
Aggregation.java
Copy and Paste, or
type the following code into Notepad and be sure to save the file when you are
done.
class Aggregation {
public static void main(String args[]){
MegaOfficeSuiteDVD dvd = new MegaOfficeSuiteDVD();
Computer c = new Computer(dvd);
System.out.println("\nBrowing the internet...");
System.out.println("Writing some source code...");
c.dvdStuff();
System.out.println("Saving work...\n");
c.allDone();
c = null; // The computer object
and all the composition objects are destroyed and eligible for garbage
collection
dvd.viewShinySurface(); // it's alive!
}
}
class OperatingSystem {
void
bootUp() { System.out.println("OS is booting up"); }
void
shutDown() { System.out.println("OS is shutting down"); }
}
class PowerSupply {
void
turnOn() { System.out.println("Powering on"); }
void
turnOff() { System.out.println("Powering off"); }
}
class DVDDrive {
void
open() { System.out.println("DVD drive is open"); }
void
close() { System.out.println("DVD drive is closed"); }
}
class MegaOfficeSuiteDVD {
void
installSoftware() { System.out.println("Installation of Mega Office Suite
complete!"); }
void
viewShinySurface() { System.out.println("\nJust look at that shiny prismy
reflection ..."); }
}
class Computer {
private OperatingSystem os = new OperatingSystem(); // composition
private PowerSupply ps = new PowerSupply(); // composition
private DVDDrive drive = new DVDDrive(); // composition
private MegaOfficeSuiteDVD dvd; // reference only - potential for
aggregation
Computer(MegaOfficeSuiteDVD dvd) {
this.dvd = dvd; // aggregation
ps.turnOn();
os.bootUp();
}
void
dvdStuff() {
drive.open();
System.out.println("Placing DVD in tray...");
drive.close();
dvd.installSoftware();
drive.open();
System.out.println("Removing DVD from tray...");
drive.close();
}
void
allDone() {
os.shutDown();
ps.turnOff();
}
}
Now switch back to the command prompt (CMD) and type in javac Aggregation.java and press Enter.
Now type in java Aggregation and press Enter.
Now type in java Aggregation and press Enter.
C:\Java\Aggregation>javac
Aggregation.java
C:\Java\Aggregation>java
Aggregation
See
video for results
DZone uml 2.0
Car.java
public class Car {
private String carColor;
private double carPrice = 0.0;
public String getCarColor(String model) {
return carColor;
}
public double getCarPrice(String model) {
return carPrice;
}
}
The above example of Car
class is self explanatory. The Car class has private instance variables
carColor, carPrice denoted by (-) in the UML Class diagram. Similarly if this
was public then it would have been represented as (+), if was protected then it
is denoted by (#). The package visibility is defined by (~).
Java visibility
|
UML Notation
|
public
|
+
|
private
|
-
|
Protected
|
#
|
package
|
~
|
The return type of the
instance variables or the methods are represented next to the colon (:) sign.
Structure:
[visibility] [multiplicity] [:type [=default value]] {property string}
Example: carPrice : double = 0.0
Structure:
[visibility] [multiplicity] [:type [=default value]] {property string}
Example: carPrice : double = 0.0
Representing Static
variable or static operation:
The static data is represented with an underline. Let’s take the below example.
The static data is represented with an underline. Let’s take the below example.
[a
href="http://idiotechie.com/wp-content/uploads/2012/12/classdiagram.jpg"]Class
diagram
Code:
public class Employee {
private static String department = "R&D";
private int empId;
private Employee(int employeeId) {
this.empId = employeeId;
}
public static String getEmployee(int emplId) {
if (emplId == 1) {
return "idiotechie";
} else {
return "Employee not found";
}
}
public static String getDepartment() {
return department;
}
}
Association:
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
0..1
|
No instances or one instance
|
A flight seat can have no or
one passenger only
|
1
|
Exactly one instance
|
An order can have only one
customer
|
0..* or *
|
Zero or more instances
|
A class can have zero or more
students.
|
1..*
|
One or more instances (at least
one)
|
A flight can have one or more
passenger
|
The unidirectional
relationship shows that the source object can invoke methods of the destination
class. In Java a possible example can be the instance variable of source class
referencing the destination class.
Association Example
Association:
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
The association represents the static relationship between two classes along with the multiplicity. E.g. an employee can have one primary address associated with it but can have multiple mobile numbers.
Association are represented as thin line connecting two classes. Association can be unidirectional (shown by arrow at one end) or bidirectional (shown by arrow at both end).
Multiplicity defines how many instances can be associated at any given moment.
0..1
|
No instances or one instance
|
A flight seat can have no or
one passenger only
|
1
|
Exactly one instance
|
An order can have only one
customer
|
0..* or *
|
Zero or more instances
|
A class can have zero or more
students.
|
1..*
|
One or more instances (at least
one)
|
A flight can have one or more passenger
|
The unidirectional
relationship shows that the source object can invoke methods of the destination
class. In Java a possible example can be the instance variable of source class
referencing the destination class.
Association Example
public class Customer {
private String name;
private String address;
private String contactNumber;
}
public class Car {
private String modelNumber;
private Customer owner;
}
Let’s look at an example
of bidirectional association:
Bidirectional association
public class Customer {
private String name;
private String address;
private String contactNumber;
private Car car;
}
public class Car {
private String modelNumber;
private Customer owner;
}
In the bidirectional
association each of the class in this relationship refers to each other by
calling each others method. In the above Java example it is depicted as
instance variable of Car class in called inside the Customer class and vice
versa.
In the above example the car and owner refers to the roles and is depicted by the name of instance variable in the code.
Multiplicity:
Assume a scenario where a customer has multiple cars. How do we represent this situation in Java and UML?
In the above example the car and owner refers to the roles and is depicted by the name of instance variable in the code.
Multiplicity:
Assume a scenario where a customer has multiple cars. How do we represent this situation in Java and UML?
Multiplicity in
association
The above diagram
explains a unidirectional association with a one to may relationship. Both use
of ArrayList and Array is for illustration purposes only.
Car.java
public class Car {
private String brand;
public Car(String brands){
this.brand = brands;
}
public Car() {
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
Customer.java
public class Customer {
private Car[] vehicles;
ArrayList<Car> carList = new ArrayList<Car>();
public Customer(){
vehicles = new Car[2];
vehicles[0] = new Car("Audi");
vehicles[1] = new Car("Mercedes");
carList.add(new Car("BMW"));
carList.add(new Car("Chevy"));
}
}
Generalization
This property represents the inheritance feature of the object oriented concept. In Java this can relate to the “extends” keyword. The inheritance should ideally follow the Liskov Substitution Principle i.e. the subtype should be able to substitute for its supertype. It helps to make the code implicitly follow the Open Close Principle i.e. Open for extension but closed for modification.
This property represents the inheritance feature of the object oriented concept. In Java this can relate to the “extends” keyword. The inheritance should ideally follow the Liskov Substitution Principle i.e. the subtype should be able to substitute for its supertype. It helps to make the code implicitly follow the Open Close Principle i.e. Open for extension but closed for modification.
Generalization
public class Car {
private String model;
public void printPrice() {
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
public class hatchback extends Car {
private String model;
public void printPrice() {
System.out.println("Hatchback Price");
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
Realization:
This is related to the
relationship between the class and the interface. The realization is equivalent
to the “implements”
keyword in Java.
Realization in Java
Realization can also be
represented as :
Realization – alternative
This is very straight
forward implementation so hopefully there will ne no code provided. Unlike
Generalization in this case the arrow is dashed.
Dependency
Dependency is a relationship that shows that a class is dependent on another class for its existence or implementation. Dependency relationship is shown as a dotted line with an arrow from source class to the dependent class.
In Java we can consider the dependency relationship if the source class has a reference to the dependent class directly or source class has methods through which the dependent objects are passed as a parameter or refers to the static operation’s of the dependent class or source class has a local variable referring to the dependent class etc.
Dependency
Dependency is a relationship that shows that a class is dependent on another class for its existence or implementation. Dependency relationship is shown as a dotted line with an arrow from source class to the dependent class.
In Java we can consider the dependency relationship if the source class has a reference to the dependent class directly or source class has methods through which the dependent objects are passed as a parameter or refers to the static operation’s of the dependent class or source class has a local variable referring to the dependent class etc.
Dependency
The above diagram
satisfies dependency relationship as the source class Order passes the
PaymentSystem reference through the processPayment().
public class PaymentSystem {
}
public class Order {
public void
processPayment(PaymentSystem ps){
}
}
Aggregation:
This shows “has a” relationship. It is a form of association relationship. This relationship highlights that a whole is made of its parts. So if a whole is destroyed the part still remains.
In UML this is represented through a hollow diamond with the diamond symbol pointing towards the whole.
In case of Java the aggregation follows the same structure as association. It is represented through the instance variables of a class.
This shows “has a” relationship. It is a form of association relationship. This relationship highlights that a whole is made of its parts. So if a whole is destroyed the part still remains.
In UML this is represented through a hollow diamond with the diamond symbol pointing towards the whole.
In case of Java the aggregation follows the same structure as association. It is represented through the instance variables of a class.
Aggregation
public class Student {
}
public class School {
private Student student;
}
In this case a student is
a part of the School. However during design it is preferred to use association
instead of aggregation as it is not a recommended option.
___ JPS Sasadara ___
Very informative. Now I got an idea about this topic. thanks for the content.
ReplyDeleteNeuromorphic Computing Applications
What is Neuromorphic Computing
Nice Post!!! thanks for sharing this great information with us.
ReplyDeleteImportance of Data Science
Why is Data Science Important?
Thanks for this blog, keep sharing your thoughts like this...
ReplyDeleteInformatica Training in Chennai
Informatica Training in Bangalore