(Ebook) C Sharp - Introduction To Design Pattern In C Sharp Vol_2 - Ibm.pdf

(1541 KB) Pobierz
newmaster.PDF
101
Figure 6-1– The Person class, showing private, protected, and public variables, and
static and abstract methods
The top part of the box contains the class name and package name (if any).
The second compartment lists the class’s variables, and the bottom
compartment lists its methods. The symbols in front of the names indicate
that member’s visibility, where “+” means public, “- ” means private, and
“#” means protected. Static methods are shown underlined. Abstract
methods may be shown in italics or, as shown in Figure Figure 6-1, with
an “{abstract}” label.
You can also show all of the type information in a UML diagram where
that is helpful, as illustrated in Figure 6-2a.
a
b
Figure 6-2 - The Person class UML diagram shown both with and without the
method types
UML does not require that you show all of the attributes of a class, and it
is usual only to show the ones of interest to the discussion at hand. For
example, in Figure 6-2 b, we have omitted some of the method details.
Copyright © , 2002 by James W Cooper
295657802.006.png 295657802.007.png 295657802.008.png 295657802.009.png
102
Inheritance
Let’s consider a version of Person that has public, protected, and private
variables and methods, and an Emplo yee class derived from it. We will
also make the getJob method abstract in the base Person class, which
means we indicate it with the MustOverride keyword.
public abstract class Person {
protected string name;
private int age;
//-----
public Person( string nm, int ag) {
name = nm;
age = ag;
}
public string makeJob() {
return "hired";
}
public int getAge() {
return age;
}
public void splitNames() {
}
public abstract string getJob(); //must override
}
We now derive the Employee class from it, and fill in some code for the
getJob method.
public class Employee : Person {
public Employee( string nm, int ag): base (nm, ag){
}
public override string getJob() {
return "Worker";
}
}
You represent inheritance using a solid line and a hollow triangular arrow.
For the simple Employee class that is a subclass of Person, we represent
this in UML, as shown in Figure 6-3
Copyright © , 2002 by James W Cooper
103
Figure 6-3 – The UML diagram showing Employee derived from Person
Note that the name of the Employee class is not in italics because it is now
a concrete class and because it includes a concrete method for the formerly
abstract getJob method. While it has been conventional to show the
inheritance with the arrow pointing up to the superclass, UML does not
require this, and sometimes a different layout is clearer or uses space more
efficiently.
Interfaces
An interface looks much like inheritance, except that the arrow has a
dotted line tail, as shown in Figure 6-4. The name <<interface>> may
also be shown, enclosed in double angle brackets, or guillamets.
Figure 6-4 – ExitCommand implements the Command interface.
Composition
Much of the time, a useful representation of a class hierarchy must include
how objects are contained in other objects. For example, a small company
might include one Employee and one Person (perhaps a contractor).
Copyright © , 2002 by James W Cooper
295657802.001.png 295657802.002.png
104
public class Company {
private Employee emp;
private Person prs;
public Company() {
}
}
We represent this in UML, as shown in Figure 6-5.
Figure 6-5 – Company contains instances of Person and Employee.
The lines between classes show that there can be 0 to 1 instances of Person
in Company and 0 to 1 instances of Employee in Company. The diamonds
indicate the aggregation of classes within Company.
If there can be many instances of a class inside another, such as the array
of Employees shown here
public class Company {
private Employee[] emps;
private Empoyee emp;
private Person prs;
public Company() {
}
}
we represent that object composition as a single line with either a “*” on it
or “0, *” on it, as shown in Figure 6-6.
Copyright © , 2002 by James W Cooper
295657802.003.png
105
Figure 6-6 – Company contains any number of instances of Employee.
Some writers have used hollow and solid diamond arrowheads to indicate
containment of aggregates and circle arrowhead for single object
composition, but this is not required.
Annotation
You will also find it convenient to annotate your UML or insert comments
to explain which class calls a method in which other class. You can place
a comment anywhere you want in a UML diagram. Comments may be
enclosed in a box with a turned corner or just entered as text. Text
comments are usually shown along an arrow line, indicating the nature of
the method that is called, as shown in Figure 6-7.
Copyright © , 2002 by James W Cooper
295657802.004.png 295657802.005.png
Zgłoś jeśli naruszono regulamin