单例、工厂、适配器、装饰器
单例
单例分为懒汉式和饿汉式
1 public class Singleton { 2 3 4 //饿汉加载 5 //应用时才加载,节省内存 6 private static Singleton instance; 7 private Singleton() { 8 } 9 public static Singleton getInstance(){ 10 if (instance == null) { 11 synchronized (Singleton.class) { 12 if (instance == null) { 13 instance = new Singleton(); 14 } 15 } 16 } 17 return instance; 18 } 19 20 21 //饿汉加载 22 //浪费内存 23 // private static Singleton singleton=new Singleton(); 24 // public static Singleton getInstance(){ 25 // return singleton; 26 // } 27 28 public void run(){ 29 System.out.println("hellowolrd"); 30 } 31 }
工厂模式:
工厂方法、抽象工厂
1 package Factory; 2 3 4 //抽象工厂 5 public class AbstractMethod { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 10 //简单工厂 11 // ICar car=Driver.createCar("Bike"); 12 // car.goWork(); 13 14 15 //抽象工厂 16 // LowPersonFactory lowPersonFactory=new LowPersonFactory(); 17 // ICar car=lowPersonFactory.getCar(); 18 // IBreakfast breakfast=lowPersonFactory.getBreakfast(); 19 // car.goWork(); 20 // breakfast.getEat(); 21 } 22 } 23 24 25 26 //简单工厂 27 class Driver{ 28 public static ICar createCar(String car){ 29 ICar c=null; 30 if("Bike".equalsIgnoreCase(car)){ 31 c=new Bike(); 32 }else{ 33 c=new Car(); 34 } 35 return c; 36 } 37 } 38 39 40 //抽象工厂 41 interface ICar{ 42 public void goWork(); 43 } 44 45 class Bike implements ICar{ 46 47 @Override 48 public void goWork() { 49 // TODO Auto-generated method stub 50 System.out.println("自行车上班"); 51 } 52 } 53 54 55 class Car implements ICar{ 56 57 @Override 58 public void goWork() { 59 // TODO Auto-generated method stub 60 System.out.println("汽车上班"); 61 } 62 } 63 64 65 interface IBreakfast{ 66 public void getEat(); 67 } 68 69 class Bread implements IBreakfast{ 70 71 @Override 72 public void getEat() { 73 // TODO Auto-generated method stub 74 System.out.println("吃面包"); 75 } 76 } 77 78 79 class Noodle implements IBreakfast{ 80 81 @Override 82 public void getEat() { 83 // TODO Auto-generated method stub 84 System.out.println("吃面"); 85 } 86 } 87 88 89 interface IFactory{ 90 public ICar getCar(); 91 public IBreakfast getBreakfast(); 92 } 93 94 class LowPersonFactory implements IFactory{ 95 96 @Override 97 public ICar getCar() { 98 // TODO Auto-generated method stub 99 return new Bike(); 100 } 101 102 @Override 103 public IBreakfast getBreakfast() { 104 // TODO Auto-generated method stub 105 return new Noodle(); 106 } 107 }
适配器:
适配器分为类适配器、对象适配器
类适配
用继承实现添加新功能
1 package Factory; 2 3 4 //类适配器 5 public class Adapter extends Adaptee implements Target{ 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Adapter adapter=new Adapter(); 10 adapter.sampleOperation2(); 11 } 12 13 @Override 14 public void sampleOperation2() { 15 // TODO Auto-generated method stub 16 sampleOperation1(); 17 System.out.println("额外的代码操作"); 18 } 19 20 } 21 22 23 class Adaptee { 24 25 public void sampleOperation1(){ 26 System.out.println("Adaptee"); 27 } 28 29 } 30 31 32 interface Target { 33 34 public void sampleOperation2(); 35 36 }
对象适配器:
利用组合来添加新功能:
1 package Factory; 2 3 //对象适配器 4 public class Adapter extends Adaptee implements Target{ 5 6 private Adaptee adaptee; 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Adaptee adaptee=new Adaptee(); 11 Adapter adapter=new Adapter(adaptee); 12 adapter.sampleOperation2(); 13 } 14 15 16 17 public Adapter(Adaptee adaptee) { 18 this.adaptee=adaptee; 19 // TODO Auto-generated constructor stub 20 } 21 22 23 24 @Override 25 public void sampleOperation2() { 26 // TODO Auto-generated method stub 27 adaptee.sampleOperation1(); 28 System.out.println("额外的代码"); 29 } 30 31 } 32 33 34 class Adaptee { 35 36 public void sampleOperation1(){ 37 System.out.println("Adaptee"); 38 } 39 40 } 41 42 43 interface Target { 44 45 public void sampleOperation2(); 46 47 }
装饰器:
1 package Factory; 2 3 public class Wrapper { 4 5 public static void main(String[] args){ 6 TheGreatestSage sage = new Monkey(); 7 TheGreatestSage bird = new Bird(sage); 8 bird.move(); 9 } 10 11 } 12 13 interface TheGreatestSage { 14 15 public void move(); 16 } 17 18 19 class Monkey implements TheGreatestSage { 20 21 @Override 22 public void move() { 23 //代码 24 System.out.println("Monkey Move"); 25 } 26 } 27 28 29 class Change implements TheGreatestSage { 30 31 private TheGreatestSage sage; 32 33 public Change(TheGreatestSage sage){ 34 this.sage = sage; 35 } 36 @Override 37 public void move() { 38 // 代码 39 sage.move(); 40 } 41 } 42 43 44 class Fish extends Change { 45 46 public Fish(TheGreatestSage sage) { 47 super(sage); 48 } 49 50 @Override 51 public void move() { 52 // 代码 53 System.out.println("Fish Move"); 54 } 55 } 56 57 58 class Bird extends Change { 59 60 public Bird(TheGreatestSage sage) { 61 super(sage); 62 } 63 64 @Override 65 public void move() { 66 // 代码 67 System.out.println("Bird Move"); 68 } 69 }