Wednesday, 22 January 2014

How To....

The Observer Pattern:
"defines a one-to- many dependency between objects so that when one object change state ,all of its dependents are notified and updated automatically"
Design Priciple used:Strive for loosely coupled designs between objects that interact.
- Head first Design Patterns
The above picture is taken from http://www.oodesign.com/observer-pattern.html

Now is the time to understand life of a celebrity,let us say Wendy. Way back then ,when she wasn't a celebrity, she had only 3 friends. Susy, Loosy(she won't mind if I spelt her name wrong), Jenny. If she had something to say to them she knows how to say to them. Since for Susy, she just have to email her since she works in a office and mobile phone  is not allowed in there. (Her boss is too bossy). For Loosy , she had to text her since she is always outgoing and very busy. For Jenny she has to ring home since she is a house wife. (never checks email or mobile phones).
so if she has to give any updates she has to do
susy.email();
loosy.text();
jenny.ring();
Now she is a celebrity ie she is a subject so that everybody wants to hear her updates. She do not know everyone and she is busy to give everyone her updates. Now what should she do? I think she has to do a "Twitter" account. Everyone who wants to follow her (that is our observers) have to have some common methods so that she doesn't have to know everybody and thier needs. Let us consider one to many relationships and how to make class loosely coupled.Note that this is not our actual real life twitter.(a sort of)
Let us consider "Twitter" is our subject interface(it could be abstract also). Let us say "Follower" is our observer. There might be another interface "Communicator" depending on the needs.
public interface Twitter{
public void follow(Follower fl);
public void removeFollwer(Follower fl);
public void acknowledgeFollowers();
}
public interface Follower{
public void updateStatus(String tweet);
}
public interface Communicator{
public void communicate();
}

public class CelebrityTwitter implements Twitter {
     private ArrayList followers;
      private string tweet;
public CelebrityTwitter() {
    followers = new ArrayList();
}
public void follow(Follower fl){
       followers.add(fl);
}
public void removeFollower(Follower fl){
int k = followers.index(fl);
if (k>=0)
followers.remove(k);
}
public void acknowledgeFollowers(){
for (int i=0;i<observers.size();i++){
Follower follower = (Follower)followers.get(i);
follower.updateStatus(tweet);
}
public void setTweet(String tweet){
this.tweet =tweet;
acknowledgeFollowers();
}
}
Concreate implementation of Follower and Communicator
public class EmailCommunicator implements Follower,Communicator{
private string  tweet;
private Email email;
private Twitter  celebrityTwitter;
public EmailCommunicator(Twitter celebrityTwitter,Email email){
this.celebrityTwitter = celebrityTwitter;
this.email =email;
celebrityTwitter.follow(this);
}
public void updateStatus(String tweet){
this.tweet = tweet;
communicate();
}
public void communicate(){
email.send(tweet);
//phonenumber.sendText(); in case of TextCommunicator()
/**by the way this is not the way you actually send email. for that you have to import all these(
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
and do some more stuff ) **/
}
}
Same way you can have lots of comunicator and Follower combinations.TextCommunicator,VoiceMailCommunicator..... all implement  "Follower" and "Communicator"  with different way of communications in communicate() method.
public class TwitterProfile{
public static void main(String[] args){
Twitter wendyTwitter = new CelebrityTwitter();
Email susyEmail = new Email("susy@email.com");
EmailCommunicator susycommunicator = new EmailCommunicator(wendyTwitter,susyEmail);
..........
TextCommunicator loosyCommunicator = new TextCommunicator(wendyTwitter, loosyphone);
.........
wendyTwitter.setTweet(" I am working on a film");
wendyTwitter.setTweet("I am going home from film  shooting");
........
Now everyone is Happy.   Wendy doesn't have to know everyone, as long as you follow her you get her tweeting updates as you desired.

Hence the title:

How To follow a celebrity without stalking.


No comments:

Post a Comment