Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180722
  • 博文数量: 12
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-04 15:43
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(1)

2008年(10)

我的朋友

分类: 项目管理

2008-08-03 09:58:45

 

2. Object Interaction


In chapter one,we have known that object-oriented software system is composed with a lot of objects.In such a software system,all objects work together ,just as all emloyee in a orgnization,every object finishs some certain job and cooperates with others.

 

2.1  Evnets drive objects-cooperation

A simple and typical process during which a object-oriented software is developed well:

l        Determine what this software need to do and every detailed mission

l        Design suitable classes which can meet all need above

l        Create objects of classes

l        Drive all objects work together through events outside

 

Think about an ant nest at peace,once a picec of suger is placed just in front of it , all ants will begin to work together ,some for searching and some for transportion, and this piece of suger is an event.

It is very similar to object-oriented software,when a software notice some event happening ,it will drive some objects to execute service themselves or request some other objects’ service until the mission is finished.

Users are very lucky, they don’t need to know how all objects work together,they only need press some buttons to finish all they want.All objects will work hard together to meet all users want ,and then keep free state until next mission.

 

2.2         How to Delcare

When a object A request service of another object B, A need to know how to communicate with B:

l        A must know what B can do.In other words ,A must make sence of all operation of object B;

l        A must give some exter information to B, which can help B do service better

l        Whether B should report A when it finish service.

In order to meet need above,we will make use of method header (方法头) and method body(方法体).

 

2.2.1        Method Head

Methode head will give information about how to execute this method,it includes:

l        Menthod name

l        Parameter list

l        Return of method

The following example is a case at point:

bool  RegisterForCours( string CourseID,int secNo)

 

Parameters

 

Parameters can be set as:

l        Something must when finish method:

In RegisterForCourse  method (which is a operation of a Student object),you must tell the Student object what course will be registered,so you should give something must to this method head:

 bool  RegisterForCours( string CourseID,int secNo)

 

l        Instruction of method

Sometimes parameters in method play roles as instruction which tell method how to work(or in which style). For example, in a method named GetAge( int i),

 int GetAge( int i)

when argument is 1,it returns the Student’s age by year,when argument is 2,it returns by months. At this place, parameter i tells method how to return.

 

l        Parameterless(无参)

You can also set a method GetAge() which returns the Student’s age by year only:

 int GetAge()

So this method is parameterless

 

Return

 

l        Return can be any type you want

int GetAge( int i)

bool RegisterForCourse( string CourseID,int secNo)

l        If you want method just work sliently and returns nothing you can set it void :

void SetName( string Name)

l        Return can be use-defined type as well

      Professor GetAdvisor()

   This method inquier who is the Student object’s advisor,it returns a reference variable which handle to a Professor object instead of returns a Professor object.

 

 

2.2.2        Method Body

Only method head is certainly not enough,we should realize all function of the method by method body.

Business logic(业务逻辑) is defined in method body, business logic can be also named business rules(业务规则).The following example show a business rules to decide whether a student is a honored student.

public class Student

{

       //Attributes

double GPA;

……

//methods

bool IsHonorsStudent()

{

      if(GPA>=3.5)

             return true;

   else

      return false;

}

……….

}

We should also pay enough attention on return of a method, we need to know that return can also be seen as a signal representing to jump out of the method.So when a return appears in a method,it is the end of the method( method will execute untile a return). As for method using void, return can also appear just as a end of the method,and a return will be added automatically. So following two methods are same:

public void DoSomething( )

{

       int x=3;

}

 

public void DoSomething( )

{

       int x=3;

   return;

}

if return type is not void , a return must included in method body,and return can be int ,char ,double (any usual type and user-defined type) and even expression:

public int DoSomething( )

{

       int x=3;

   int y=4;

   return x+y;

}

If needed , more than one return can be included in a method,the following case is at point:

bool IsHorneredStudent( )

{

       if(GPA>=3.5)

       {

              return true;

       else

           return false;

}

Please note that a suggested good habit is to use one return in a method,we can use a local variable to record what will be returned,so another way to define IsHonoredStudent( ) can be as following:                                      

bool IsHorneredStudent( )                                                 

{                                                                    

    bool result=false;                                                                 

       if(GPA>=3.5)                                                                                                                     

              result= true;                                                      

       else                                                               

           result= false; 

    return result                                                        

}                                                                      

 

 

2.3         How to Use Dot Notation(点操作符)

We have known how to define a method, then, how to drive an object to exectute this method? We create Message(消息) to solve this problem,message is an expression which composed with an object and a dot followed by this method.eg:

Student x=new Student( );

//drive object x to execute RegisterForCourse method

x.RegisterForCourse(“Math 101” , 10) //this is a message

The dot followed by a method of the object is called dot notation,

x.RegisterForCourse(“Math 101” , 10)

sentence above can be described as “a way to drive object x to execute RegisterForCourse method” or “give a message to object x to execute RegisterForCourse method”.

 

2.3.1        Messages Transmit among Objects

 

Let us consider a case, a student object S what to register for a course object C1,and it isn’t allowed unless S has finished another course object C2. And methods of tow classes are described in the following table:

       Method

           Description

Student Class:

bool Completed( Course c)

 whether a Student has finished a

 course c sucessfully

Course Class

bool Register( Student s)

 Whether Student s has register for a the course successfully

 

So messages transmitting between these two objects can be seen as following:

 

 Object C

( a course)

 Object S

( a Student)

 Object C

( a course)

 Object S

( a Student)

                     C.Register(S)                      S.Completed(C2)

 

return true                                 return true

 

Following will demostrate progcess of picture above:

l        A course object C receives a message:

  C.Register(S);

  //S is a cretain Student object

l        In order to judge whether the Sutdent object S is allowed to take object C,object C send following message to S:

  S.Completed(C2) ;

l        Student object S return true to C,which indicate that S has finished Course object C2

l        After Course object C has received a true from Object S,it also return a true, which incate that the registeration is allowed.

The case above is a simple one,in fact, Course object may communicate with many other objects by message.

 

2.3.2        Get and Set Attribute by Dot Notation

 

It is the same with message transmit, we can get and set some attributes by dot notation conveniently, take Student object for example:

Student x=new Student( );

Student y=new Student( );

//set attribute by dot notation

x.name=”John Smith”;

//get attribute from x and set it to y attribute

y.name=x.name

 

2.3.3        Delegation (委托)

 

In order to finish some service,object A ask help from object B,this can be called delegation from A to B. It is very like degelation in real life, eg. one day your husband/wife want to you trim lawns,but you have something emergent,so you will employee a boy living nearby to do that, if he is not too lazy,everything is OK without anybody know this delegation.

Delegation between objects also is not known by service-requesting object.In the case in 2.3.1,we can regard Course object C as you , and Student object S as the boy, so you just return a TRUE answer to your husband/wife ( registeration )and he/she (it) don’t know how you(object C) to finishe that.

 

 

 

2.3.4        How to Send Message

 

If object A wants to send a message to object B,A should get handle to object B, it must meet one of following need:

l        There is an attribute in object A which is a reference variable to object B

public class Student

{

       //attribute

              string name;

              Professor Advisor; // a reference variable to a Professor object

     }

l        The reference variable to object B is a parameter of method in A,

C.RegisterForCourse(S);

l        B is a global variable in the program.

A can get handle to Object C,and C can get handle to Object B, so A can get handle to B indirectly ,this is the most complex case
阅读(737) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~