Object-oriented design (OOD) is a technology in its own right, often treated as being separate and apart from OOP, but better to think about OOP as about the implementation of OOD procedure.
Many books have been written describing a variety of methods for accomplishing OOD and here we are not going to discuss and cover all topics of that. As a physicist, I think about programming as a way to help us in some particular problems, not as a theory in general. Standard way, however, to show how to create an OO program is to explain, how to make first pass to identify classes, data members and methods to build the OO program.
One common technique is to create a narrative description of the solution to the problem, then use nouns and the verbs to identify the classes , data members , and methods needed in the design.
Consider the following narrative description of the solution to an object-oriented design problem. We will use this description to design a simple object-oriented program. The nouns will give us hints about the classes and the data members that will be needed, and the verbs will give us an idea of the methods that will be needed.
The output from the test program should be:
00
01
10
11
The program may look like this:
1
2
3 /* Counter designing */
4
5 class Bit{
6 int value;
7 void set(){//method to set the value of the bit to 0
8 value = 0;
9 }//end set()
10
11 int get(){//method to get the value stored in the bit
12 return value;
13 }//end get()
14
15 //method to implement binary addition
16 // we provide the table here:
17 int add(int inValue){
18 int carry = 0;
19 if((value == 0) && (inValue == 0)){ //0+0=0,c=0
20 carry = 0;
21 }else if((value == 0) && (inValue == 1)){//0+1=1,c=0
22 value = 1;
23 carry = 0;
24 }else if((value == 1) && (inValue == 0)){//1+0=1,c=0
25 carry = 0;
26 }else if((value == 1) && (inValue == 1)){//1+1=0,c=1
27 value = 0;
28 carry = 1;
29 }//end if statement
30
31 return carry;
32 }//end add()
33 }//end class Bit
34
35 class Counter{
36 Bit fstbit = new Bit();//instantiate three bit objects
37 Bit scndbit= new Bit();
38
39 //method to initialize the bit objects to 0
40 void initialize(){
41 fstbit.set();
42 scndbit.set();
43 }//end initialize()
44
45 //method to add 1 to lsb and have it ripple up
46 // through all three bits of the counter
47 void increment(){
48 scndbit.add(fstbit.add(1));
49 }//end increment()
50
51 //method to display the value of each bit in the counter
52 void show(){
53 System.out.println("" + scndbit.get() + fstbit.get() );
54 }//end show()
55
56 }//end class Counter
57
58 //controlling class required by Java application
59 class ex5 {
60 public static void main(String[] args){
61 //instantiate a counter object
62 Counter myCounter = new Counter();
63 myCounter.initialize();//initialize the counter object
64 myCounter.show(); //display contents of counter object
65 //increment and display counter object
66 for(int cnt = 0; cnt < 3; cnt++){
67 myCounter.increment();//increment it
68 myCounter.show();//display it
69 }//end for loop
70 }//end main()
71
72 }
The program includes a class named Bit with a data member named value as well as methods to set the instance variable, get the instance variable, and add a 1 to the value of the instance variable returning the carry from the binary addition. These items you can find as nouns or verbs in the description.
Also as you can see, the design resulted in a class named Counter which had three embedded Bit objects as instance variables or data members. This class has a method named initialise that is used to initialise the two-bit counter to 00.
This class also has a method named increment which adds one to the least-significant bit, adds the carry from the least significant bit to the middle bit, and adds the carry from the middle bit to the most-significant bit. This is the typical ripple pattern for a binary counter.
This class also has a show method that displays the values stored in each of the two Bit objects in the order from most-significant to least-significant.
This program also contains a class named ex5 which is not represented by a noun in the narrative description. This is because all applications in JAVA require a controlling class, and in this case the controlling class is named ex5.