下载 23种设计模式源码 :
http://download.csdn.net/download/knight_black_bob/8936043
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
创建型模式,共五种:
工厂方法模式 抽象工厂模式单例模式建造者模式原型模式
结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式组合模式享元模式
行为型模式,共十一种:
策略模式 模板方法模式观察者模式迭代子模式责任链模式命令模式
备忘录模式状态模式 访问者模式中介者模式 解释器模式
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 1package 设计模式.外观模式;
2
3public class Product {
4
5 private String name;
6 private double price;
7 private int count;
8
9
10 public Product(){}
11 public Product(String name){
12 this.name = name;
13 }
14 public Product(String name, double price, int count) {
15 this.name = name;
16 this.price = price;
17 this.count = count;
18 }
19
20
21 public int getCount() {
22 return count;
23 }
24 public void setCount(int count) {
25 this.count = count;
26 }
27 public String getName() {
28 return name;
29 }
30 public void setName(String name) {
31 this.name = name;
32 }
33 public double getPrice() {
34 return price;
35 }
36 public void setPrice(double price) {
37 this.price = price;
38 }
39
40
41
42
43 @Override
44 public boolean equals(Object obj) {
45 if (obj != null)
46 if (obj instanceof Product)
47 return this.name == ((Product) obj).getName();
48 return false;
49 }
50
51
52 @Override
53 public String toString() {
54 StringBuffer sb =new StringBuffer();
55 sb.append(getClass().getName().substring(getClass().getName().lastIndexOf(".")+1,getClass().getName().length()));
56 sb.append("[");
57 sb.append("\"name\":\""+name+"\",");
58 sb.append("\"count\":"+count+",");
59 sb.append("\"price\":"+price);
60 sb.append("]");
61 return sb.toString();
62 }
63}
64
65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 1package 设计模式.外观模式;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class Shop {
7
8 static List<Product> plist = new ArrayList<Product>();
9
10 static {
11 Product apple = new Product("apple",2.5,200);
12 Product pear = new Product("pear",1.0,200);
13 plist.add(apple);
14 plist.add(pear);
15 }
16
17 public double getProPrice(String name){
18 Product pro =null;
19 for (Product p : plist) {
20 if (p.equals(new Product(name)) == true) {
21 pro =p;
22 break;
23 }
24 }
25 if (pro !=null)
26 return pro.getPrice();
27 else
28 return 0.0;
29 }
30
31 public boolean rebuy(String name ,int count ){
32 boolean flag =false;
33 Product pro =null;
34 for (Product p : plist) {
35 flag = flag | p.equals(new Product(name));
36 if (flag == true) {
37 pro =p;
38 break;
39 }
40 }
41 if (flag == true && pro !=null)
42 if (pro.getCount() >= count)
43 flag = true;
44 else flag = false;
45 else flag =false;
46 return flag;
47 }
48
49 public boolean buy(String name ,int count ){
50 boolean flag =false;
51 Product pro =null;
52 for (Product p : plist) {
53 flag = flag | p.equals(new Product(name));
54 if (flag == true) {
55 pro =p;
56 break;
57 }
58 }
59 plist.remove(pro);
60 pro.setCount(pro.getCount()-count);
61 plist.add(pro);
62
63 return flag;
64 }
65
66 public List<Product> getList(){
67 return plist;
68 }
69}
70
71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 1package 设计模式.外观模式;
2
3public class Wallet {
4
5 static double money = 10000.00;
6
7 public boolean check(double price){
8 boolean flag =false;
9 if (money > price ){
10 flag = true;
11 }else
12 flag =false;
13 return flag;
14 }
15
16 public boolean pay(double price){
17 boolean flag =false;
18 if (money > price ){
19 money -= price;
20 flag = true;
21 }else
22 flag =false;
23 return flag;
24 }
25
26 public double balance(){
27 return money;
28 }
29}
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1package 设计模式.外观模式;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class Order {
7 static List<Product> plist = new ArrayList<Product>();
8
9 public boolean addOrder(Product p){
10 plist.add(p);
11 return true;
12 }
13
14 public boolean addOrder(String name, double price, int count){
15 Product p = new Product ( name ,price ,count) ;
16 return this.addOrder(p);
17 }
18
19 public List<Product> getOrderList(){
20 return plist;
21 }
22}
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 1package 设计模式.外观模式;
2
3import java.util.List;
4
5public class Pay {
6
7 Shop shop = new Shop();
8 Wallet mWallet = new Wallet();
9 Order order= new Order();
10
11 public boolean doPay(String name ,int count){
12 boolean flag = false;
13
14 flag = flag | shop.rebuy(name, count);
15 if (flag == false) return flag;
16
17 flag = flag & mWallet.check(shop.getProPrice(name) * count);
18 if (flag == false) return flag;
19
20 flag = flag &shop.buy(name, count);
21 if (flag == false) return flag;
22
23 flag = flag &mWallet.pay(shop.getProPrice(name) * count);
24 if (flag == false) return flag;
25
26 flag = flag & order.addOrder(name, shop.getProPrice(name) * count, count);
27 return flag;
28 }
29
30 public double balance(){
31 return mWallet.balance();
32 }
33
34 public String myOrder(){
35 StringBuffer sb = new StringBuffer();
36 sb.append("{");
37 List<Product> list = order.getOrderList();
38 for (int i = 0; i < list.size(); i++) {
39 Product p = list.get(i);
40 sb.append("[\"name\":\""+p.getName()+"\"");
41 sb.append("\"price\":\""+p.getPrice()+"\"");
42 sb.append("\"count\":\""+p.getCount()+"\"],");
43 if (i == list.size()-1)
44 sb = sb.replace(sb.length()-1, sb.length(), "");
45 }
46 sb.append("}");
47 return sb.toString();
48 }
49
50 public String productList(){
51 StringBuffer sb = new StringBuffer();
52 sb.append("{");
53 List<Product> list = shop.getList();
54 for (int i = 0; i < list.size(); i++) {
55 Product p = list.get(i);
56 sb.append("[\"name\":\""+p.getName()+"\"");
57 sb.append("\"price\":\""+p.getPrice()+"\"");
58 sb.append("\"count\":\""+p.getCount()+"\"],");
59 if (i == list.size()-1)
60 sb = sb.replace(sb.length()-1, sb.length(), "");
61 }
62 sb.append("}");
63 return sb.toString();
64 }
65
66}
67
68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package 设计模式.外观模式;
2
3public class FacadeTest {
4
5 //简化外部客户程序和系统间的交互接口
6 //为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。[GOF 《设计模式》]
7 public static void main(String[] args) {
8 Pay pay = new Pay();
9 System.out.println("余额:" +pay.balance());
10 System.out.println("商品信息:" +pay.productList());
11 System.out.println("我的购物车:" +pay.myOrder());
12
13 System.out.println("支付是否成功:" + pay.doPay("apple", 2));
14 System.out.println("支付是否成功:" + pay.doPay("apple", 200000));
15 System.out.println("支付是否成功:" + pay.doPay("banana", 2));
16
17 System.out.println("余额:" +pay.balance());
18 System.out.println("商品信息:" +pay.productList());
19 System.out.println("我的购物车:" +pay.myOrder());
20 }
21}
22
23
捐助开发者
在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!