设计模式
codesuperman1314
For the better days
展开
-
模板方法
模板方法泡咖啡为例public abstract class RefreshBeverage { /** * 使用final关键字让子类无法更改固定流程 */ public final void repareBeverageTemplate() { boilWater(); brew(); /**原创 2017-04-12 20:11:50 · 314 阅读 · 0 评论 -
单例模式
/** * 懒汉式 */ public class Singleton { private static Singleton instance; /** * 1.构造方法私有化 */ private Singleton(){ System.out.println("创建对象"); } /** * 根据对象是否为原创 2017-04-12 20:31:18 · 292 阅读 · 0 评论 -
代理模式
代理模式静态代理继承方式实现public interface Moveable { void move(); }public class Car implements Moveable { @Override public void move() { // 实现开车 try { System.out.println("汽原创 2017-04-13 11:05:21 · 328 阅读 · 0 评论 -
适配器模式
适配器模式组合方式实现/** * 三相插座接口 */ public interface IThreePlug { //使用三相电流供电 void powerWithThree(); }public class GBTwoPlug { //使用二相电流供电 public void powerWithTwo(){ System.out.println(原创 2017-04-13 12:15:22 · 329 阅读 · 0 评论 -
责任链模式
责任链模式/** * 价格处理人,负责处理客户折扣申请 */ public abstract class PriceHandler { // 直接后继,用于传递请求 protected PriceHandler successor; public void setSuccessor(PriceHandler successor) { this.succes原创 2017-04-13 18:05:07 · 316 阅读 · 0 评论 -
观察者模式
观察者模式import java.util.Observable; /** * 天气目标的具体实现类 */ public class ConcreteWeatherSubject extends Observable { // 天气情况的内容 private String content; public String getContent() { retur原创 2017-04-14 19:31:40 · 236 阅读 · 0 评论 -
策略模式
策略模式/** * 策略接口,定义飞行行为 */ public interface FlyingStragety { void fly(); }public abstract class Duck { private FlyingStragety fs; public void setFs(FlyingStragety fs) { this.fs = fs;原创 2017-04-14 21:59:55 · 359 阅读 · 0 评论