1 单例模式
简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为构造器是被private修饰的,一般通过getInstance()的方法来获取它们的实例。
getInstance()的返回值是一个对象的引用,并不是一个新的实例,所以不要错误的理解成多个对象。
1 public class Singleton { 2 3 private static Singleton singleton; 4 5 private Singleton() { 6 } 7 8 public static Singleton getInstance() { 9 if (singleton == null) { 10 singleton = new Singleton(); 11 } 12 return singleton; 13 } 14 }
懒汉式写法(线程安全)
1 public class Singleton { 2 private static Singleton instance; 3 private Singleton (){} 4 public static synchronized Singleton getInstance() { 5 if (instance == null) { 6 instance = new Singleton(); 7 } 8 return instance; 9 } 10 }
饿汉式写法
1 public class Singleton { 2 private static Singleton instance = new Singleton(); 3 private Singleton (){} 4 public static Singleton getInstance() { 5 return instance; 6 } 7 }
静态内部类
1 public class Singleton { 2 private static class SingletonHolder { 3 private static final Singleton INSTANCE = new Singleton(); 4 } 5 private Singleton (){} 6 public static final Singleton getInstance() { 7 return SingletonHolder.INSTANCE; 8 } 9 }
枚举
1 public enum Singleton { 2 INSTANCE; 3 public void whateverMethod() { 4 } 5 }
双重校验锁
1 public class Singleton { 2 private volatile static Singleton singleton; 3 private Singleton (){} 4 public static Singleton getSingleton() { 5 if (singleton == null) { 6 synchronized (Singleton.class) { 7 if (singleton == null) { 8 singleton = new Singleton(); 9 } 10 } 11 } 12 return singleton; 13 } 14 }
2 观察者模式
对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
1 public interface Person { 2 //小王和小李通过这个接口可以接收到小美发过来的消息 3 void getMessage(String s); 4 }
1 public class LaoWang implements Person { 2 3 private String name = "小王"; 4 5 public LaoWang() { 6 } 7 8 @Override 9 public void getMessage(String s) { 10 System.out.println(name + "接到了小美打过来的电话,电话内容是:" + s); 11 } 12 13 } 14 15 public class LaoLi implements Person { 16 17 private String name = "小李"; 18 19 public LaoLi() { 20 } 21 22 @Override 23 public void getMessage(String s) { 24 System.out.println(name + "接到了小美打过来的电话,电话内容是:->" + s); 25 } 26 27 }
public class XiaoMei { List<Person> list = new ArrayList<Person>(); public XiaoMei(){ } public void addPerson(Person person){ list.add(person); } //遍历list,把自己的通知发送给所有暗恋自己的人 public void notifyPerson() { for(Person person:list){ person.getMessage("你们过来吧,谁先过来谁就能陪我一起玩儿游戏!"); } } }
1 //测试类 2 public class Test { 3 public static void main(String[] args) { 4 5 XiaoMei xiao_mei = new XiaoMei(); 6 LaoWang lao_wang = new LaoWang(); 7 LaoLi lao_li = new LaoLi(); 8 9 //小王和小李在小美那里都注册了一下 10 xiao_mei.addPerson(lao_wang); 11 xiao_mei.addPerson(lao_li); 12 13 //小美向小王和小李发送通知 14 xiao_mei.notifyPerson(); 15 } 16 }
3 装饰者模式
对已有的业务逻辑进一步的封装,使其增加额外的功能,如Java中的IO流就使用了装饰者模式,用户在使用的时候,可以任意组装,达到自己想要的效果。
1 public class Food { 2 3 private String food_name; 4 5 public Food() { 6 } 7 8 public Food(String food_name) { 9 this.food_name = food_name; 10 } 11 12 public String make() { 13 return food_name; 14 }; 15 }
1 //面包类 2 public class Bread extends Food { 3 4 private Food basic_food; 5 6 public Bread(Food basic_food) { 7 this.basic_food = basic_food; 8 } 9 10 public String make() { 11 return basic_food.make()+"+面包"; 12 } 13 } 14 15 //奶油类 16 public class Cream extends Food { 17 18 private Food basic_food; 19 20 public Cream(Food basic_food) { 21 this.basic_food = basic_food; 22 } 23 24 public String make() { 25 return basic_food.make()+"+奶油"; 26 } 27 } 28 29 //蔬菜类 30 public class Vegetable extends Food { 31 32 private Food basic_food; 33 34 public Vegetable(Food basic_food) { 35 this.basic_food = basic_food; 36 } 37 38 public String make() { 39 return basic_food.make()+"+蔬菜"; 40 } 41 42 }
1 public class Test { 2 public static void main(String[] args) { 3 Food food = new Bread(new Vegetable(new Cream(new Food("香肠")))); 4 System.out.println(food.make()); 5 } 6 }
4 适配器模式
将两种完全不同的事物联系到一起,就像现实生活中的变压器。假设一个手机充电器需要的电压是20V,但是正常的电压是220V,这时候就需要一个变压器,将220V的电压转换成20V的电压,这样,变压器就将20V的电压和手机联系起来了。
1 public class Test { 2 public static void main(String[] args) { 3 Phone phone = new Phone(); 4 VoltageAdapter adapter = new VoltageAdapter(); 5 phone.setAdapter(adapter); 6 phone.charge(); 7 } 8 } 9 10 // 手机类 11 class Phone { 12 13 public static final int V = 220;// 正常电压220v,是一个常量 14 15 private VoltageAdapter adapter; 16 17 // 充电 18 public void charge() { 19 adapter.changeVoltage(); 20 } 21 22 public void setAdapter(VoltageAdapter adapter) { 23 this.adapter = adapter; 24 } 25 } 26 27 // 变压器 28 class VoltageAdapter { 29 // 改变电压的功能 30 public void changeVoltage() { 31 System.out.println("正在充电..."); 32 System.out.println("原始电压:" + Phone.V + "V"); 33 System.out.println("经过变压器转换之后的电压:" + (Phone.V - 200) + "V"); 34 } 35 }
5 工厂模式
简单工厂模式:一个抽象的接口,多个抽象接口的实现类,一个工厂类,用来实例化抽象的接口
1 // 抽象产品类 2 abstract class Car { 3 public void run(); 4 5 public void stop(); 6 } 7 8 // 具体实现类 9 class Benz implements Car { 10 public void run() { 11 System.out.println("Benz开始启动了。。。。。"); 12 } 13 14 public void stop() { 15 System.out.println("Benz停车了。。。。。"); 16 } 17 } 18 19 class Ford implements Car { 20 public void run() { 21 System.out.println("Ford开始启动了。。。"); 22 } 23 24 public void stop() { 25 System.out.println("Ford停车了。。。。"); 26 } 27 } 28 29 // 工厂类 30 class Factory { 31 public static Car getCarInstance(String type) { 32 Car c = null; 33 if ("Benz".equals(type)) { 34 c = new Benz(); 35 } 36 if ("Ford".equals(type)) { 37 c = new Ford(); 38 } 39 return c; 40 } 41 } 42 43 public class Test { 44 45 public static void main(String[] args) { 46 Car c = Factory.getCarInstance("Benz"); 47 if (c != null) { 48 c.run(); 49 c.stop(); 50 } else { 51 System.out.println("造不了这种汽车。。。"); 52 } 53 54 } 55 56 }
工厂方法模式:有四个角色,抽象工厂模式,具体工厂模式,抽象产品模式,具体产品模式。不再是由一个工厂类去实例化具体的产品,而是由抽象工厂的子类去实例化产品
1 // 抽象产品角色 2 public interface Moveable { 3 void run(); 4 } 5 6 // 具体产品角色 7 public class Plane implements Moveable { 8 @Override 9 public void run() { 10 System.out.println("plane...."); 11 } 12 } 13 14 public class Broom implements Moveable { 15 @Override 16 public void run() { 17 System.out.println("broom....."); 18 } 19 } 20 21 // 抽象工厂 22 public abstract class VehicleFactory { 23 abstract Moveable create(); 24 } 25 26 // 具体工厂 27 public class PlaneFactory extends VehicleFactory { 28 public Moveable create() { 29 return new Plane(); 30 } 31 } 32 33 public class BroomFactory extends VehicleFactory { 34 public Moveable create() { 35 return new Broom(); 36 } 37 } 38 39 // 测试类 40 public class Test { 41 public static void main(String[] args) { 42 VehicleFactory factory = new BroomFactory(); 43 Moveable m = factory.create(); 44 m.run(); 45 } 46 }
抽象工厂模式:与工厂方法模式不同的是,工厂方法模式中的工厂只生产单一的产品,而抽象工厂模式中的工厂生产多个产品
1 //抽象工厂类 2 public abstract class AbstractFactory { 3 public abstract Vehicle createVehicle(); 4 public abstract Weapon createWeapon(); 5 public abstract Food createFood(); 6 } 7 //具体工厂类,其中Food,Vehicle,Weapon是抽象类, 8 public class DefaultFactory extends AbstractFactory{ 9 @Override 10 public Food createFood() { 11 return new Apple(); 12 } 13 @Override 14 public Vehicle createVehicle() { 15 return new Car(); 16 } 17 @Override 18 public Weapon createWeapon() { 19 return new AK47(); 20 } 21 } 22 //测试类 23 public class Test { 24 public static void main(String[] args) { 25 AbstractFactory f = new DefaultFactory(); 26 Vehicle v = f.createVehicle(); 27 v.run(); 28 Weapon w = f.createWeapon(); 29 w.shoot(); 30 Food a = f.createFood(); 31 a.printName(); 32 } 33 }
6 代理模式之静态代理
整个流程大概是这样的:家里人催婚->男女双方家庭商定结婚的黄道即日->找一家靠谱的婚庆公司->在约定的时间举行结婚仪式->结婚完毕
1 //代理接口 2 public interface ProxyInterface { 3 //需要代理的是结婚这件事,如果还有其他事情需要代理,比如吃饭睡觉上厕所,也可以写 4 void marry(); 5 //代理吃饭(自己的饭,让别人吃去吧) 6 //void eat(); 7 //代理拉屎,自己的屎,让别人拉去吧 8 //void shit(); 9 }
1 public class WeddingCompany implements ProxyInterface { 2 3 private ProxyInterface proxyInterface; 4 5 public WeddingCompany(ProxyInterface proxyInterface) { 6 this.proxyInterface = proxyInterface; 7 } 8 9 @Override 10 public void marry() { 11 System.out.println("我们是婚庆公司的"); 12 System.out.println("我们在做结婚前的准备工作"); 13 System.out.println("节目彩排..."); 14 System.out.println("礼物购买..."); 15 System.out.println("工作人员分工..."); 16 System.out.println("可以开始结婚了"); 17 proxyInterface.marry(); 18 System.out.println("结婚完毕,我们需要做后续处理,你们可以回家了,其余的事情我们公司来做"); 19 } 20 21 }
1 public class NormalHome implements ProxyInterface{ 2 3 @Override 4 public void marry() { 5 System.out.println("我们结婚啦~"); 6 } 7 8 }
1 public class Test { 2 public static void main(String[] args) { 3 ProxyInterface proxyInterface = new WeddingCompany(new NormalHome()); 4 proxyInterface.marry(); 5 } 6 }