1 import Java. util. arraylist; 2 3 class manager 4 {5 private string name; // manager name 6 Public Manager (string) {7 name = string; 8} 9 Public void Update (string ID) 10 {11 system. out. println ("Haha" + ID + "the house has been bought, so happy "); 12} 13 14 15} 16 // abstract house 17 abstract class house 18 {19 private string ID; 20 private int price; 21 private int status = 0; // status = 0 not sold 22 private arraylist <Manager> M = new arraylist <Manager> (); 23 public void setstatus () 24 {25 status = 1; // 26 for (Manager Ma: m) 27 {28 Ma. update (ID); 29} 30} 31 Public House (string ID, int price) 32 {33 this. id = ID; 34 this. price = price; 35} 36 Public void addobserver (Manager Ma) 37 {38 m. add (MA); 39} 40 41 42} 43 // house details 44 class bighouse extends house 45 {46 Public bighouse (string ID, int price) {47 super (ID, price); 48 // todo auto-generated constructor stub 49} 50 51 public void method () 52 {53 system. out. println ("bighouse"); 54 55} 56} 57 // specific house 58 class Smallhouse extends house 59 {60 61 Public Smallhouse (string ID, int price) {62 super (ID, price); 63 // todo auto-generated constructor stub 64} 65 66 public void met () 67 {68 system. out. println ("Smallhouse"); 69} 70} 71 // factory interface 72 interface factory 73 {74 public house getinstance (string ID, int price ); 75 76} 77 // large house factory 78 Class bigfactory implements factory 79 {80 81 @ override 82 public house getinstance (string ID, int price) {83 // todo auto-generated method stub 84 return New bighouse (ID, price ); 85} 86 87 88} 89 class smallfactory implements factory 90 {91 92 @ override 93 public house getinstance (string ID, int price) {94 // todo auto-generated method stub 95 return New Smallhouse (ID, price ); 96} 97 98 99 100} 101 102 103 public class design mode 1 {104 105/** 106 * @ Param args107 */108 public static void main (string [] ARGs) {109 // todo auto-generated method stub110 // create a large house object 111 House H1 = new bigfactory (). getinstance ("0001", 10000000); 112 // a small house object 113 (bighouse) H1 ). method (); 114 house H2 = new smallfactory (). getinstance ("333", 100); 115 Manager Ma = new manager ("zhangsan"); 116 h2.addobserver (MA); 117 h2.setstatus (); 118 119 120 121 122 123 124 // sell 125 126 127 128 129} 130}
Http://blog.csdn.net/lovelion/article/details/7430515