A
basic example of using JAWIRO: BasicExample.java
package test_destek;
import jawiro.*;
public class BasicExample {
public void begin( ) {
Person p, p2;
Employee e;
Teacher t;
DepartmentManager d,d2;
ProfEmeritus pre;
p = new Person( "Yunus Emre
Selçuk", "212-2891990" );
p2 = new Person( "Thomas
Anderson", "216-6287493" );
e = new Employee( "212-2853300" );
t = new Teacher( "Mathematics" );
d = new DepartmentManager(
"Information Technology", "IT" );
d2 = new DepartmentManager(
"Security", "SEC" );
p.addRole( e );
p.addRole( t );
e.addRole( d2 );
e.addRole( d );
p.introduce( );
if(
p.canSwitch("test_destek.BasicExample$Employee") )
((Employee)p.as("test_destek.BasicExample$Employee")).introduce();
if( e.canDelegate(t) )
((Teacher)e.as(t.getClass())).introduce();
if(
p.canSwitch("test_destek.BasicExample$DepartmentManager","IT") )
((DepartmentManager)p.as("test_destek.BasicExample$DepartmentManager","IT")).introduce();
if(
e.canSwitch("test_destek.BasicExample$DepartmentManager","SEC") )
((DepartmentManager)e.as("test_destek.BasicExample$DepartmentManager","SEC")).introduce();
t.resign();
if( !p.canDelegate(t) )
t.retired( );
pre = new ProfEmeritus( t );
p.addRole( pre );
if(
p.canSwitch("test_destek.BasicExample$ProfEmeritus") )
pre.introduce(
);
e.suspend();
if(
!p.canSwitch("test_destek.BasicExample$Employee") )
e.onHold( );
if(
!d.canSwitch("test_destek.BasicExample$DepartmentManager","SEC") )
d.onHold( );
if(
!e.canSwitch("test_destek.BasicExample$DepartmentManager","IT") )
d2.onHold( );
e.resume();
if(
!p.canSwitch("test_destek.BasicExample$Employee") )
e.resumed( );
if(
e.canSwitch("test_destek.BasicExample$DepartmentManager","IT") )
d.resumed( );
if(
!e.canSwitch("test_destek.BasicExample$DepartmentManager","SEC") )
d2.resumed( );
e.transfer( p2 );
if(
!p.canSwitch("test_destek.BasicExample$Employee") )
System.out.println( p.getName() + " is no longer employed.");
if(
p.canSwitch("test_destek.BasicExample$Employee") )
System.out.println( "Something illogical has happened!" );
else if(
p2.canSwitch("test_destek.BasicExample$Employee") )
e.whoami();
else
System.out.println( "Transfer unsuccessful!" );
if( p.canDelegate(d) )
System.out.println( "Something illogical has happened!" );
else if( p2.canDelegate(d) )
d.whoami();
else
System.out.println( "Transfer unsuccessful!" );
if(
p.canSwitch("test_destek.BasicExample$DepartmentManager","SEC") )
System.out.println( "Something illogical has happened!" );
else if(
p2.canSwitch("test_destek.BasicExample$DepartmentManager","SEC") )
d2.whoami();
else
System.out.println( "Transfer unsuccessful!" );
}
public static void main(String[] args) {
BasicExample example = new
BasicExample( );
example.begin( );
}
class Person extends Actor {
//for preventing a warning about
serialization in JDK 1.5
private static final long
serialVersionUID = 1L;
private String name, phone;
public Person( String n, String p
) {
name = n;
phone = p;
}
void setName( String s ) {
name = s;
}
void setPhone( String s ) {
phone = s;
}
public String getName( ) {
return name;
}
public String getPhone( ) {
return phone;
}
public void introduce( ) {
System.out.print( "Hello, my name is " + name );
System.out.println( ". You can call me from this number: " + phone +
"." );
}
}
class Employee extends Role {
private static final long
serialVersionUID = 1L;
private String phone;
public Employee( String p ) {
phone = p;
}
void setPhone( String s ) {
phone = s;
}
public String getPhone( ) {
return phone;
}
public void introduce( ) {
System.out.println( "In work hours, you can call me from " + phone +
"." );
}
public void whoami( ) {
System.out.print( "My name is " + ((Person)Actor()).name );
System.out.println( ", currently employed." );
}
public void onHold( ) {
System.out.println( "I am temporarily unemployed." );
}
public void resumed( ) {
System.out.println( "I have resumed working as an employee." );
}
}
class Teacher extends Role {
private static final long
serialVersionUID = 1L;
protected String course;
public Teacher( String c ) {
course = c;
}
public void setCourse( String s )
{
course = s;
}
public String getCourse() {
return course;
}
public void retired( ) {
System.out.println( "I don't teach " + course + " anymore." );
}
public void introduce( ) {
System.out.println( "I teach " + course + "." );
}
public void whoami( ) {
System.out.print( "My name is " + ((Person)Actor()).name );
System.out.println( ", I teach " + course + "." );
}
}
class DepartmentManager extends AggregateRole {
private static final long
serialVersionUID = 1L;
private String deptName;
public DepartmentManager( String
c, String id ) {
super( id );
deptName = c;
}
public void setDepartment( String
s ) {
deptName = s;
}
public String getDepartment( ) {
return
deptName;
}
public void introduce( ) {
System.out.println( "I am the head of the " + deptName + " department."
);
}
public void whoami( ) {
System.out.print( "My name is " + ((Person)Actor()).name + " and ");
System.out.println( "I am the head of the " + deptName + " department."
);
}
public void onHold( ) {
System.out.println( "I am no longer the head of the " + deptName + "
department." );
}
public void resumed( ) {
System.out.println( "I have resumed working as the head of the " +
deptName + " department." );
}
}
class ProfEmeritus extends Teacher {
private static final long
serialVersionUID = 1L;
public ProfEmeritus( Teacher t ) {
super(
t.course );
}
public ProfEmeritus( String
courseName ) {
super(
courseName );
}
public void introduce( ) {
System.out.println( "I teach " + course + " as Professor Emeritus." );
}
}
}
Output of BasicExample.java:
Hello, my name is Yunus Emre Selçuk. You can call me from this number:
212-2891990.
In work hours, you can call me from 212-2853300.
I teach Mathematics.
I am the head of the Information Technology department.
I am the head of the Security department.
I don't teach Mathematics anymore.
I teach Mathematics as Professor Emeritus.
I am temporarily unemployed.
I am no longer the head of the Information Technology department.
I am no longer the head of the Security department.
I have resumed working as the head of the Information Technology
department.
Yunus Emre Selçuk is no longer employed.
My name is Thomas Anderson, currently employed.
My name is Thomas Anderson and I am the head of the Information
Technology department.
My name is Thomas Anderson and I am the head of the Security department.