常用设计模式
一.单例模式:
public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
二.观察着模式:
public interface Person { //老王和老李通过这个接口可以接收到小美发过来的消息 void getMessage(String s); }
public class LaoWang implements Person { private String name = "老王"; public LaoWang() {} @Override public void getMessage(String s) { System.out.println(name + "接到了小美打过来的电话,电话内容是:" + s); } }
public class LaoLi implements Person { private String name = "老李"; public LaoLi() {} @Override public void getMessage(String s) { System.out.println(name + "接到了小美打过来的电话,电话内容是:->" + s); } }
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("我是广播,我发消息了哈"); } } }
public class Test { public static void main(String[] args) { XiaoMei xiao_mei = new XiaoMei(); LaoWang lao_wang = new LaoWang(); LaoLi lao_li = new LaoLi(); //老王和老李在小美那里都注册了一下 xiao_mei.addPerson(lao_wang); xiao_mei.addPerson(lao_li); //小美向老王和老李发送通知 xiao_mei.notifyPerson(); } }
三.装饰器模式
public class Food { private String food_name; public Food() {} public Food(String food_name) { this.food_name = food_name; } public String make() { return food_name; } }
//面包类 public class Bread extends Food { private Food basic_food; public Bread(Food basic_food) { this.basic_food = basic_food; } public String make() { return basic_food.make()+"+面包"; } }
//奶油类 public class Cream extends Food { private Food basic_food; public Cream(Food basic_food) { this.basic_food = basic_food; } public String make() { return basic_food.make()+"+奶油"; } }
//蔬菜类 public class Vegetable extends Food { private Food basic_food; public Vegetable(Food basic_food) { this.basic_food = basic_food; } public String make() { return basic_food.make()+"+蔬菜"; } }
//测试: public class Test { public static void main(String[] args) { Food food = new Bread(new Vegetable(new Cream(new Food("香肠")))); System.out.println(food.make()); } }
四.适配器模式
public class Test { public static void main(String[] args) { Phone phone = new Phone(); VoltageAdapter adapter = new VoltageAdapter(); phone.setAdapter(adapter); phone.charge(); } }
// 手机类 class Phone { public static final int V = 220;// 正常电压220v,是一个常量 private VoltageAdapter adapter; // 充电 public void charge() { adapter.changeVoltage(); } public void setAdapter(VoltageAdapter adapter) { this.adapter = adapter; } }
// 变压器 class VoltageAdapter { // 改变电压的功能 public void changeVoltage() { System.out.println("正在充电..."); System.out.println("原始电压:" + Phone.V + "V"); System.out.println("经过变压器转换之后的电压:" + (Phone.V - 200) + "V"); } }
五.代理模式
//代理接口 public interface ProxyInterface { //需要代理的是结婚这件事 void marry(); }
婚庆公司的代码: public class WeddingCompany implements ProxyInterface { private ProxyInterface proxyInterface; public WeddingCompany(ProxyInterface proxyInterface) { this.proxyInterface = proxyInterface; } @Override public void marry() { System.out.println("我们是婚庆公司的"); System.out.println("我们在做结婚前的准备工作"); System.out.println("节目彩排..."); System.out.println("礼物购买..."); System.out.println("工作人员分工..."); System.out.println("可以开始结婚了"); proxyInterface.marry(); System.out.println("结婚完毕,我们需要做后续处理,你们可以回家了,其余的事情我们公司来做"); } }
结婚家庭 public class NormalHome implements ProxyInterface{ @Override public void marry() { System.out.println("我们结婚啦~"); } }