SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Class report




                   CLASS ASSIGNMENT- 2
                    OBSERVER PATTERN




         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…..




                                                                         Page 1 of 9
Class report

Company:
Company name:

Supportive information:

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,


                                                                        Page 2 of 9
Class report



Company                                           User

NotifyUser();                                     ReceiveNotification();




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();

}

}


                                                                      Page 3 of 9
Class report

NotifyUser(){

For all users in userlist{

//loop;

User.ReceiveNotification(shareprice,company name);

}}



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




Likewise,




                           AddCompany



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




Similarly,

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




                                                                           Page 5 of 9
Class report




                               AddUser



      User1                      User2                  .........




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);

                                                                    Page 6 of 9
Class report

            ShareHolder sh3 = new ShareHolder("Anik", cmp2);
            cmp2.addShareHolder(sh3);

            cmp1.changePrice(7000000);
            cmp2.changePrice(6000000);

      }
}




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);
      }

                                                               Page 7 of 9
Class report

      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;
      }

      }



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);
      }


                                                                          Page 8 of 9
Class report

    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 9

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (12)

Map reduce
Map reduceMap reduce
Map reduce
 
Apache hadoop & map reduce
Apache hadoop & map reduceApache hadoop & map reduce
Apache hadoop & map reduce
 
R with excel
R with excelR with excel
R with excel
 
Nmdl final pp 1
Nmdl final pp 1Nmdl final pp 1
Nmdl final pp 1
 
Big data
Big dataBig data
Big data
 
Icons presentation
Icons presentationIcons presentation
Icons presentation
 
Understanding Your Credit Report
Understanding Your Credit ReportUnderstanding Your Credit Report
Understanding Your Credit Report
 
Interviews 1
Interviews 1Interviews 1
Interviews 1
 
New microsoft office word 97 2003 document
New microsoft office word 97   2003 documentNew microsoft office word 97   2003 document
New microsoft office word 97 2003 document
 
Strategy pattern.pdf
Strategy pattern.pdfStrategy pattern.pdf
Strategy pattern.pdf
 
Job search_resume
Job search_resumeJob search_resume
Job search_resume
 
Twitter
TwitterTwitter
Twitter
 

Ähnlich wie Observer pattern

srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxrafbolet0
 
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.docxsmile790243
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleMathias Seguy
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the orgAbbyWhyte974
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the orgMartineMccracken314
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5Akhil Mittal
 
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.docxkeilenettie
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletMitchinson
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchartfika sweety
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3Akhil Mittal
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces HomeWork-Fox
 
Leverage StandardSetController in Apex and Visualforce
Leverage StandardSetController in Apex and VisualforceLeverage StandardSetController in Apex and Visualforce
Leverage StandardSetController in Apex and VisualforceSalesforce Developers
 

Ähnlich wie Observer pattern (20)

Observer pattern
Observer patternObserver pattern
Observer pattern
 
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
 
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
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
 
1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org1-What are the opportunities and threats that could impact the org
1-What are the opportunities and threats that could impact the org
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Diving into VS 2015 Day5
Diving into VS 2015 Day5Diving into VS 2015 Day5
Diving into VS 2015 Day5
 
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
 
Intake 38 5 1
Intake 38 5 1Intake 38 5 1
Intake 38 5 1
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
Diving into VS 2015 Day3
Diving into VS 2015 Day3Diving into VS 2015 Day3
Diving into VS 2015 Day3
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces
 
Leverage StandardSetController in Apex and Visualforce
Leverage StandardSetController in Apex and VisualforceLeverage StandardSetController in Apex and Visualforce
Leverage StandardSetController in Apex and Visualforce
 

Mehr von Md. Mahedi Mahfuj

Mehr von Md. Mahedi Mahfuj (18)

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
 
Matrix multiplication graph
Matrix multiplication graphMatrix multiplication graph
Matrix multiplication graph
 
Strategy pattern
Strategy patternStrategy pattern
Strategy pattern
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16
 
Database management system chapter15
Database management system chapter15Database management system chapter15
Database management system chapter15
 
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_interview
Job search_interviewJob search_interview
Job search_interview
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
R language
R languageR language
R language
 
Chatbot Artificial Intelligence
Chatbot Artificial IntelligenceChatbot Artificial Intelligence
Chatbot Artificial Intelligence
 
Cloud testing v1
Cloud testing v1Cloud testing v1
Cloud testing v1
 
Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
 
Paper review
Paper review Paper review
Paper review
 

Kürzlich hochgeladen

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Observer pattern

  • 1. Class report CLASS ASSIGNMENT- 2 OBSERVER PATTERN 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….. Page 1 of 9
  • 2. Class report Company: Company name: Supportive information: 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, Page 2 of 9
  • 3. Class report Company User NotifyUser(); ReceiveNotification(); 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(); } } Page 3 of 9
  • 4. Class report NotifyUser(){ For all users in userlist{ //loop; User.ReceiveNotification(shareprice,company name); }} 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 4 of 9
  • 5. Class report Likewise, AddCompany Company1 Company2 ............... Similarly, We can use the same concept in case of multiple users,likewise; Page 5 of 9
  • 6. Class report AddUser User1 User2 ......... 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); Page 6 of 9
  • 7. Class report ShareHolder sh3 = new ShareHolder("Anik", cmp2); cmp2.addShareHolder(sh3); cmp1.changePrice(7000000); cmp2.changePrice(6000000); } } 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); } Page 7 of 9
  • 8. Class report 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; } } 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); } Page 8 of 9
  • 9. Class report 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 9