设计模式学习
1 策略设计模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package thinkJava; 2 3 /* 4 * 策略设计模式 5 * 像本例一样,能够根据所传递的参数对象的不同而具有不同的行为的方法。 6 * Processor对象就是一个策略,有3中不同的策略应用到了String类型的对象上。 7 */ 8 import java.util.Arrays; 9 10 class Processor { 11 public String name() { 12 return getClass().getSimpleName(); 13 } 14 15 Object process(Object input) { 16 return input; 17 } 18 } 19 20 class Upcase extends Processor { 21 String process(Object input) { 22 return ((String) input).toUpperCase(); 23 } 24 } 25 26 class Downcase extends Processor { 27 String process(Object input) { 28 return ((String) input).toLowerCase(); 29 } 30 } 31 32 class Splitter extends Processor { 33 String process(Object input) { 34 return Arrays.toString(((String) input).split(" ")); 35 } 36 } 37 38 public class Apply { 39 40 public static void process(Processor p, Object s) { 41 System.out.println("Using Processor " + p.name()); 42 System.out.println(p.process(s)); 43 } 44 45 public static String s = "Disagreemment with beliefs is by definition incorrect"; 46 47 public static void main(String[] args) { 48 process(new Upcase(), s); 49 process(new Downcase(), s); 50 process(new Splitter(), s); 51 } 52 53 }
运行结果
2 单例模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package singleton; 2 3 class Soup { 4 private Soup() { 5 } 6 7 private static Soup pSoup = new Soup(); 8 9 public static Soup getInstance() { 10 return pSoup; 11 } 12 13 public void f() { 14 System.out.println("f() in pSoup is working"); 15 } 16 } 17 18 public class Lunch { 19 static void testSingleton() { 20 Soup.getInstance().f(); 21 } 22 23 public static void main(String[] args) { 24 testSingleton(); 25 } 26 }
运行结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 f() in pSoup is working
3 代理模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package reusing; 2 3 public class SpaceShipControls { 4 void up(int velocity){ 5 6 } 7 void down(int velocity){ 8 9 } 10 void left(int velocity){ 11 12 } 13 void right(int velocity){ 14 15 } 16 void forward(int velocity){ 17 18 } 19 void back(int velocity){ 20 21 } 22 void turnBoost(){ 23 24 } 25 }
代理模式开始:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package reusing; 2 /* 3 * SpaceShip并非真正的SpaceShipControls类型。 4 * 可以采用中庸之道,介于组合和继承之间的模式 5 * SpaceShip包含SpaceShipControls,同时,SpaceShipControls 6 * 的所有方法在SpaceShip中都暴露出来了。代理解决了此问题。 7 */ 8 public class SpaceShipDelegation { 9 private String name; 10 private SpaceShipControls controls = new SpaceShipControls(); 11 public SpaceShipDelegation(String name){ 12 this.name = name; 13 } 14 public void back(int velocity){ 15 controls.back(velocity); 16 } 17 public void forward(int velocity){ 18 controls.forward(velocity); 19 } 20 public void down(int velocity){ 21 controls.down(velocity); 22 } 23 public void up(int velocity){ 24 controls.up(velocity); 25 } 26 public void right(int velocity){ 27 controls.right(velocity); 28 } 29 public void left(int velocity){ 30 controls.left(velocity); 31 } 32 public void turnboBoost(){ 33 controls.turnBoost(); 34 } 35 public static void main(String[] args){ 36 SpaceShipDelegation protector = new SpaceShipDelegation("NAEA Protector"); 37 protector.forward(100); 38 } 39 }
4 工厂模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package ploymorphism; 2 3 import java.util.Random; 4 5 public class Shape { 6 public void draw() { 7 8 } 9 10 public void erase() { 11 12 } 13 } 14 15 public class Square extends Shape { 16 public void draw() { 17 System.out.println("Square draw()"); 18 } 19 20 public void erase() { 21 System.out.println("Square erase()"); 22 } 23 } 24 25 public class Circle extends Shape { 26 public void draw() { 27 System.out.println("Circle draw()"); 28 } 29 30 public void erase() { 31 System.out.println("Circle erase()"); 32 } 33 } 34 35 public class Triangle extends Shape { 36 public void draw() { 37 System.out.println("Triangle draw()"); 38 } 39 40 public void erase() { 41 System.out.println("Triangle erase()"); 42 } 43 } 44 45 public class RandomShapeGenerator { 46 private Random rand = new Random(47); //工厂模式 47 48 public Shape next() { 49 switch (rand.nextInt(3)) { 50 default: 51 case 0: 52 return new Circle(); 53 case 1: 54 return new Square(); 55 case 2: 56 return new Triangle(); 57 } 58 } 59 } 60 61 public class Shapes { 62 private static RandomShapeGenerator gen = new RandomShapeGenerator(); 63 64 public static void main(String[] args) { 65 Shape[] s = new Shape[9]; 66 for (int i = 0; i < s.length; i++) { 67 s[i] = gen.next(); 68 } 69 for (Shape shp : s) { 70 shp.draw(); 71 } 72 } 73 } 74 75 /* 76 * Triangle draw() 77 * Triangle draw() 78 * Square draw() 79 * Triangle draw() 80 * Square draw() 81 * Triangle draw() 82 * Square draw() 83 * Triangle draw() 84 * Circle draw() 85 */