SlideShare a Scribd company logo
Class report




  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 1 of 10
Class report

                       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 2 of 10
Class report

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 3 of 10
Class report



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 4 of 10
Class report

}}



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 5 of 10
Class report




Likewise,




                           AddCompany



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




Similarly,

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




                              AddUser



       User1                    User2                      .........

                                                                           Page 6 of 10
Class report



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 7 of 10
Class report




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 8 of 10
Class report




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 9 of 10
Class report




               Page 10 of 10

More Related Content

Similar to Observer pattern

Create New Android Activity
Create New Android ActivityCreate New Android Activity
Create New Android Activity
Transpose Solutions Inc
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5Akhil Mittal
 
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docxLab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
smile790243
 
Multi-company configuration with OpenERP 6
Multi-company configuration with OpenERP 6Multi-company configuration with OpenERP 6
Multi-company configuration with OpenERP 6Pexego
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
Mathias Seguy
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
ax2012
 ax2012 ax2012
ax2012
hailtaron
 
Previous weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docxPrevious weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docx
keilenettie
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
Celine George
 
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
rafbolet0
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3Akhil Mittal
 
Learning Robotic Process Automation-81-167
Learning Robotic Process Automation-81-167Learning Robotic Process Automation-81-167
Learning Robotic Process Automation-81-167
Nicolas Benjamin Cruz Carpio
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
Kalluri Vinay Reddy
 
Creating EPiServer Usage Reports
Creating EPiServer Usage ReportsCreating EPiServer Usage Reports
Creating EPiServer Usage Reports
Paul Graham
 
J developer 11g components ppt
J developer 11g components pptJ developer 11g components ppt
J developer 11g components ppt
TUSHAR VARSHNEY
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
Mitchinson
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 

Similar to Observer pattern (20)

Observer pattern
Observer patternObserver pattern
Observer pattern
 
Create New Android Activity
Create New Android ActivityCreate New Android Activity
Create New Android Activity
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docxLab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
Lab StepsSTEP 1 Login Form1. In order to do this lab, we need.docx
 
Multi-company configuration with OpenERP 6
Multi-company configuration with OpenERP 6Multi-company configuration with OpenERP 6
Multi-company configuration with OpenERP 6
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
ax2012
 ax2012 ax2012
ax2012
 
Previous weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docxPrevious weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3
 
Learning Robotic Process Automation-81-167
Learning Robotic Process Automation-81-167Learning Robotic Process Automation-81-167
Learning Robotic Process Automation-81-167
 
Create an other activity lesson 3
Create an other activity lesson 3Create an other activity lesson 3
Create an other activity lesson 3
 
Creating EPiServer Usage Reports
Creating EPiServer Usage ReportsCreating EPiServer Usage Reports
Creating EPiServer Usage Reports
 
J developer 11g components ppt
J developer 11g components pptJ developer 11g components ppt
J developer 11g components ppt
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Clean code
Clean codeClean code
Clean code
 

More from Md. Mahedi Mahfuj (20)

Bengali optical character recognition system
Bengali optical character recognition systemBengali optical character recognition system
Bengali optical character recognition system
 
Parallel computing chapter 3
Parallel computing chapter 3Parallel computing chapter 3
Parallel computing chapter 3
 
Parallel computing chapter 2
Parallel computing chapter 2Parallel computing chapter 2
Parallel computing chapter 2
 
Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
Parallel computing(1)
Parallel computing(1)Parallel computing(1)
Parallel computing(1)
 
Message passing interface
Message passing interfaceMessage passing interface
Message passing interface
 
Advanced computer architecture
Advanced computer architectureAdvanced computer architecture
Advanced computer architecture
 
Parallel searching
Parallel searchingParallel searching
Parallel searching
 
Clustering manual
Clustering manualClustering manual
Clustering manual
 
Matrix multiplication graph
Matrix multiplication graphMatrix multiplication graph
Matrix multiplication graph
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Database management system chapter12
Database management system chapter12Database management system chapter12
Database management system chapter12
 
Strategies in job search process
Strategies in job search processStrategies in job search process
Strategies in job search process
 
Report writing(short)
Report writing(short)Report writing(short)
Report writing(short)
 
Report writing(long)
Report writing(long)Report writing(long)
Report writing(long)
 
Job search_resume
Job search_resumeJob search_resume
Job search_resume
 
Job search_interview
Job search_interviewJob search_interview
Job search_interview
 
Apache hadoop & map reduce
Apache hadoop & map reduceApache hadoop & map reduce
Apache hadoop & map reduce
 
Map reduce
Map reduceMap reduce
Map reduce
 
R with excel
R with excelR with excel
R with excel
 

Recently uploaded

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

Observer pattern

  • 1. Class report 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 1 of 10
  • 2. Class report 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 2 of 10
  • 3. Class report 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 3 of 10
  • 4. Class report 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 4 of 10
  • 5. Class report }} 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 5 of 10
  • 6. Class report Likewise, AddCompany Company1 Company2 ............... Similarly, We can use the same concept in case of multiple users,likewise; AddUser User1 User2 ......... Page 6 of 10
  • 7. Class report 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 7 of 10
  • 8. Class report 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 8 of 10
  • 9. Class report 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 9 of 10
  • 10. Class report Page 10 of 10