????????????ж???????и???????????setter??????

???????????ж???????и??????????????

???????????setter???????????????????????仯???????

public class Duck{
FlyBehavior flyBehavior;//???
QuackBehavior quackBehavior;//???
public void setFlyBehavior(FlyBehavior flyBehavior){
this.flyBehavior = flyBehavior;
}
public void setQuackBehavior(QuackBehavior quackBehavior  {
this.quackBehavior= quackBehavior;
}
}

????------------------------- static Factory Method(???????) -------------------------

????(1)????????У?Factory Method?????????????????÷??????EJB??RMI??COM??CORBA??Swing?ж???????????????????????????????.???????????????xxxFactory????????????.

????(2)????????

????FactoryMethod??????????????????????????????????????????????????????????????????????.

????????????Factory Method???????????????????????

??????÷??棺

??????????????????????????????????????????????????????????????????????????Factory Method ????.

????-------------------------------- singelton(??????) --------------------------------

????????????:

????Singleton ??????????????????????????????????????????????????????????.???Щ?????????????????????????????????е?????????????? Socket ?????????????????????????????????????????????.

?????????

???????????static??????

??????????????????????????У????????????????????????????????У??????????ж???ɡ?

?????????????????????

???????????????????????????????

?????????????????????

????????????.??????????У??????getInstace?????У??????ж??????????????????????????д?????

????????????????????

????????????????????????hashtable?л???????????????????????????????????????hashtable?У????????????????????????

??????2????????

public class Singleton {
private static Singleton s;
public static Singleton getInstance() {
if (s == null)
s = new Singleton();
return s;
}
}
// ??????
class singletonTest {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1==s2)
System.out.println("s1 is the same instance with s2");
else
System.out.println("s1 is not the same instance with s2");
}
}

????singletonTest???н?????

????s1 is the same instance with s2

??????3?????????

class Singleton {
static boolean instance_flag = false; // true if 1 instance
public Singleton() {
if (instance_flag)
throw new SingletonException("Only one instance allowed");
else
instance_flag = true; // set flag for 1 instance
}
}

????-------------------------------- ???????(Observer) --------------------------------

????(1)????????:

????????????????????????????????????????????????????????????????????????????????????????????????????????????????

????????????????????????Subject?????????Observer????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????

???????ó?????

???????????????????????????????????????????????仯?????????????????и?????????????????????????/?????????????????????????????????????????У???????????????????????и????

????(2)???:

????(?)???????(subject)??????(observer)???:

???????(subject)???:

????????????????????????; public void attach(Observer o);

?????????????????????????; public void detach(Observer o);

???????????????????????????????????????????????; public void notice();

?????????(observer)???:

?????????????????????????????: public void update();

????(3)?????

?????????綽??????????????????綽????????????????????????????????У?????????????????Subject????????????????????????????????綽?????????????????????????????????綽?????

???????????????:

Subject????
public interface Subject{
public void attach(Observer o);
public void detach(Observer o);
public void notice();
}

Observer????
public interface Observer{
public void update();
}

Teacher????
import java.util.Vector;
public class Teacher implements Subject{
private String phone;
private Vector students;
public Teacher(){
phone = "";
students = new Vector();
}
public  void attach(Observer o){
students.add(o);
}
public void detach(Observer o){
students.remove(o);
}
public void notice(){
for(int i=0;i<students.size();i++)
((Observer)students.get(i)).update();
}
public void setPhone(String phone){
this.phone = phone;
notice();    --???
}
public String getPhone(){
return phone;
}
}

Student????
public class Student implements Observer{
private String name;
private String phone;
private Teacher teacher;
public Student(String name??Teacher t){
this.name = name;
teacher = t;
}
public void show(){
System.out.println("Name:"+name+" Teacher'sphone:"+phone);
}
public void update(){
phone = teacher.getPhone();
}
}

Client????
package observer;
import java.util.Vector;
public class Client{         -->??????????????????????????vector????????????.
public static void main(String[] args){
Vector students = new Vector();
Teacher t = new Teacher();
for(int i= 0 ;i<10;i++){
Student st = new Student("lili"+i??t);
students.add(st);
t.attach(st);
}
t.setPhone("88803807");
for(int i=0;i<10;i++)
((Student)students.get(i)).show();
t.setPhone("88808880");
for(int i=0;i<10;i++)
((Student)students.get(i)).show();
}
}

???????Observer????????????????MVC????Observer???????????????????????????????С?