observer pattern

10
Class report Page 1 of 10 CLASS ASSIGNMENT- 2 SHARE PRICE NOTIFICATION (OBSERVER PATTERN) Submitted by: Md.Mahedi Mahfuj -BIT 0207 Submitted to: Maeenul Islam Lecturer,IIT Date of submission: 25 th January,2012 Institute of Information Technology

Upload: md-mahedi-mahfuj

Post on 18-Jul-2015

230 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Observer pattern

Class report

Page 1 of 10

CLASS ASSIGNMENT- 2

SHARE PRICE NOTIFICATION

(OBSERVER PATTERN)

Submitted by:

Md.Mahedi Mahfuj -BIT 0207

Submitted to:

Maeenul Islam

Lecturer,IIT

Date of submission:

25th January,2012

Institute of Information Technology

Page 2: Observer pattern

Class report

Page 2 of 10

University of Dhaka

SHARE PRICE NOTIFICATION:

Introduction:

At first, we have to see what happens in this sort of share price notification

system, i.e. what features we have to focus on.

Now,let’s see the criteria that we have to maintain in our program. When a

user or client is registering ,he/she has to be added to the userlist. At the same

time, the preservation of the very information comes in mind.

But,if we think deeply, we can see that it’s not the responsibility of the user to

keep the registration information always in mind.Rather, the company is fully

responsible in respect of holding its users’ information.Now, the question

comes where will I keep the subscription info?So, let,s go through the whole

problem in brief.

There must be a Company class.Let’s, see what may be there…..

Company:

Company name:

Supportive information:

Page 3: Observer pattern

Class report

Page 3 of 10

Userlist;

Shareprice:

So, some methods may be present like;

Registration();

ChangeShare();

NotifyUsers();

Again, there must be a User class.The features may be ….

User:

Name:

Description:

Functions like;

ReceiveNotification();

-may be present.

Another class named Share may be present. But, it’s not mandatory to take it

as a class .Rather, we can easily include it in the Company class.

Actually, what functions will play the key role can easily be detected.As,

Company User

NotifyUser(); ReceiveNotification();

Page 4: Observer pattern

Class report

Page 4 of 10

However;

let’s focus, how we will troubleshoot the problem.

Company Class:

At first , we have to create a registration method in Company class in order to

maintain the generic registration issues.As for example;

Registration(User us){

Userlist.add(us);

}

Now, we have to maintain a suitable connection with the users who are added

to the userlist. If there is any change found in the share price then the

company has to notify the users about the possible change. As,

ChangeShare(double price){

If (shareprice !=price){

Shareprice=price;

NotifyUser();

}

}

NotifyUser(){

For all users in userlist{

//loop;

User.ReceiveNotification(shareprice,company name);

Page 5: Observer pattern

Class report

Page 5 of 10

}}

User Class:

Now, after being called by NotifyUser() method, the ReceiveNotification()

method becomes active. So, it somehow sends the message that it has been

notifed. As,

ReceiveNotification(double price,company name){

Print(“I have been notified”);

}

So, what we clearly see here is that ,

Company notifies user.

User observe notification.

Hence, company – user observation process.

That’s why, this way of solving a problem is termed as “Observer Pattern”.

Now,

We have described the criteria in respect to one company only until now. If

we have to do the same thing in respect to many companies, then we just have

to add an abstract class namely AddCompany with some common features

extending the companies seperately with the extra features.

Page 6: Observer pattern

Class report

Page 6 of 10

Likewise,

Similarly,

We can use the same concept in case of multiple users,likewise;

AddCompany

Company1 Company2 ...............

AddUser

User1 User2 .........

Page 7: Observer pattern

Class report

Page 7 of 10

The main code to solve the problem is shown below:

Main Code:

User Class:

public class User { public static void main(String[ ] args) { Company cmp1 = new Company("Yahoo", 5000000); Company cmp2 = new Company("Apple", 4000000); ShareHolder sh1 = new ShareHolder("Mahedi", cmp1); cmp1.addShareHolder(sh1); ShareHolder sh2 = new ShareHolder("Mahfuj", cmp1); cmp1.addShareHolder(sh2); ShareHolder sh3 = new ShareHolder("Anik", cmp2); cmp2.addShareHolder(sh3); cmp1.changePrice(7000000); cmp2.changePrice(6000000); } }

Page 8: Observer pattern

Class report

Page 8 of 10

Company Class:

import java.util.ArrayList; public class Company { private double sharePrice; private ArrayList<ShareHolder> sh; private String companyName; public Company(String cn,double sPrice){ companyName = cn; sharePrice = sPrice; sh = new ArrayList<ShareHolder>(); } public void addShareHolder(ShareHolder sh){ this.sh.add(sh); } public void changePrice(double sPrice){ if(this.sharePrice != sPrice){ java.util.Iterator<ShareHolder> itr = sh.iterator(); while(itr.hasNext()){ itr.next().receiveNotification(getCompanyName(),sPrice); } } } public String getCompanyName() { return companyName; } }

Page 9: Observer pattern

Class report

Page 9 of 10

Shareholder Class:

//This class is not mandatory.We can add the shareholder information in the company class even.// import java.util.ArrayList; public class ShareHolder { private String holderName; private ArrayList<Company> regCom; public ShareHolder(String hn,Company cm){ holderName = hn; regCom = new ArrayList<Company>(); } public void registerTo(Company cm){ this.regCom.add(cm); } public void receiveNotification(String cName,double sPrice){

System.out.println(this.holderName + " is notified as sharePrice of " + cName + " is changed to " + sPrice);

} }

……………………………………………………………………………..X…………………………………………………………………………………

Page 10: Observer pattern

Class report

Page 10 of 10