Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 14: Java Code Generation and Reverse Engineering

public Child()

{

}

}

In the code for the parent class, there is no mention of the child class. This helps keep the parent generic; many classes can inherit from it without affecting its code. In the child class, the code is generated to support its inheritance from the parent class. The class declaration will look like this:

Public class Child extends Parent

Interfaces

In Rose, a Java interface is modeled as a class with a stereotype of Interface. It contains operation signatures, but does not contain any implementation for the operations. The implementation of the operations is contained in other classes. The interface is connected with a realization relationship to the class(es) that implement the interface:

When you generate code, Rose will generate a Java interface. The code for the interface and implementation classes from the example above is shown here:

This is the code for the interface class:

//Source file: C:\\Interface.java

public interface Interface

{

/**

@roseuid 3945AB7201CA */

public void DoSomething();

}

and for the implementation class:

//Source file: C:\\Class.java

public class Class implements Interface

{

public Class()

{

}

480

Chapter 14: Java Code Generation and Reverse Engineering

/**

@roseuid 3945AC1B032C */

public void DoSomething()

{

}

}

As you can see, Rose places the "implements" keyword in the implementation class to set the interface for that class. Once the code has been generated, you can program the DoSomething() operation in the implementation class.

Java Beans

You can model a Java Bean in Rose as an attribute. The attribute will have all of the standard specifications available in Rose, with three additional fields: the type of bean (Bound, Constrained, Simple, or Not a Bean); a Read/Write field, which controls the Get and Set operations generated; and a flag for change management. Figure 14.12 shows the Java attribute specification window, in which you can set these three properties.

Figure 14.12: Setting Java Bean properties

The Read/Write field controls whether a bean Get and/or bean Set operation will be generated. The Read & Write option will create both a Get and a Set operation. Read will create only a Get operation, and Write will create only a Set operation.

There are three types of beans available. A simple bean creates just a Get and Set method, depending on the value of the Read/Write field. In the above example, the code will look like this:

//Source file: C:\\SampleClass.java

public class SampleClass

{

private int SampleBean;

public SampleClass()

{

}

481

Chapter 14: Java Code Generation and Reverse Engineering

/**

* Access method for the SampleBean property.

*

* @return the current value of the SampleBean property @roseuid

*/

public int getSampleBean()

{

return SampleBean;

}

/**

*Sets the value of the SampleBean property.

*@param aSampleBean the new value of the SampleBean property @roseuid

*/

public void setSampleBean(int aSampleBean)

{

SampleBean = aSampleBean;

}

}

With a bound bean, Rose will include an import java.beans.* statement in the generated code. In addition, it will generate a declaration, a Set operation, an optional Get operation (depending on the value of the Read/Write field), a PropertyChangeSupport attribute, an addPropertyChange−Listener operation, and a removePropertyChangeListener operation. The code will now look like this:

//Source file: C:\\SampleClass.java

import java.beans.*; public class SampleClass

{

private int SampleBean;

/**

* common PropertyChangeSupport instance */

protected PropertyChangeSupport commonPCS = new PropertyChangeSupport(this);

public SampleClass()

{

}

/**

* Access method for the SampleBean property.

*

* @return the current value of the SampleBean property @roseuid

*/

public int getSampleBean()

{

return SampleBean;

}

/**

*Sets the value of the SampleBean property.

*@param aSampleBean the new value of the SampleBean property

482

Chapter 14: Java Code Generation and Reverse Engineering

@roseuid

*/

public void setSampleBean(int aSampleBean)

{

SampleBean = aSampleBean;

}

public void addPropertyChangeListener(PropertyChangeListener listener)

{

commonPCS.addPropertyChangeListener(listener);

}

public void removePropertyChangeListener(PropertyChangeListener listener)

{

commonPCS.removePropertyChangeListener(listener);

}

}

Finally, with a constrained bean, Rose will include an import java.beans.* statement in the code. It will also create a declaration, a Set operation, an optional Get operation (depending on the value of the Read/Write field), a VetoableChangeSupport property, an addVetoableChangeListener operation, and a removeVetoableChangeListener operation. The code in this situation will look like this:

//Source file: C:\\SampleClass.java

import java.beans.*; public class SampleClass

{

private int SampleBean;

/**

* common VetoableChangeSupport instance */

protected VetoableChangeSupport commonVCS = new VetoableChangeSupport(this);

public SampleClass()

{

}

/**

* Access method for the SampleBean property.

*

* @return the current value of the SampleBean property @roseuid

*/

public int getSampleBean()

{

return SampleBean;

}

/**

*Sets the value of the SampleBean property.

*@param aSampleBean the new value of the SampleBean property @roseuid

*/

public void setSampleBean(int aSampleBean) throws PropertyVetoException

{

SampleBean = aSampleBean;

483