Sunday, March 6, 2011

Designing Java applications using Object Orientation

Now we will consider a java example under object-oriented definitions by building up the HelloWorld2 application.
Architecture & code of HelloWorld3

Following is the class diagram of the HelloWorld3 application:



The GreetingFactory class is designed to generate a new greeting object base on input type. The HelloWorld3 class then gets a greeting interface referencing to a corresponding concrete class by using the factory class. This means it invokes methods of the interface only and does not need to know what greeting concrete classes are and how they are implemented.

With this architecture,you also can add a new different greeting class implementing IGreeting interface such as ItalianGreeting but do not need to update the HelloWorld3 again.

Followings are screenshots of class details and running results:
• Define the interface below for Greeting object,this interface has only one method



• Create two greeting classes to implement the interface above,one for French greeting,another for English greeting



• Define the factory,GreetingFactory,to produce greeting



• Create the entry point of this application,it checks input params and call GreetingFactory to produce the corresponding greeting



Run HelloWorld3
Followings are steps help you to compile and run the HelloWorld3 application:

• Go to the source code folder of HelloWorld3 and type command javac *.java to compile all source code files,as following:



• The console below show the way to run HelloWorld3 with value of input parameter passed as french to receive a French greeting:



• By passing input parameter with value different to french you will receive a English greeting as following:

0 comments: