美文网首页
设计模式相关

设计模式相关

作者: 细雨蒙情 | 来源:发表于2018-06-13 22:27 被阅读13次

1、观察者模式

//被观察者
    public interface observer{
        public void update(String s);
    }
    
    //观察著
    public interface observable{
        
        public void registerObserver(observer o);
        public void notifyUpdate();
    }
    //微信公众号
    public static class Wechat implements observable{
        
        public observer observer;

        @Override
        public void registerObserver(observer o) {
            this.observer = o;
        }
              
        @Override
        public void notifyUpdate() {
            observer.update("有新的内容推送啦!");       
        }
        
    }
    //用户
    public static class User implements observer{

        @Override
        public void update(String s) {
            System.out.println(s);
            
        }
    }

      //测试代码
      Wechat wechat = new Wechat();
      User user = new User();
      wechat.registerObserver(user);
      wechat.notifyUpdate();

https://www.cnblogs.com/luohanguo/p/7825656.html

2、适配器模式、

//目标接口
public interface Target {
 
    //这是源类Adapteee没有的方法
    public void Request(); 
}
//源接口
public class Adaptee {
    
    public void SpecificRequest(){
    }
}
//适配器
class Adapter implements Target{  
    // 直接关联被适配类  
    private Adaptee adaptee;  
    
    // 可以通过构造函数传入具体需要适配的被适配类对象  
    public Adapter (Adaptee adaptee) {  
        this.adaptee = adaptee;  
    }  
    
    @Override
    public void Request() {  
        // 这里是使用委托的方式完成特殊功能  
        this.adaptee.SpecificRequest();  
    }  
}
//测试类
public class AdapterPattern {
    public static void main(String[] args){
        //需要先创建一个被适配类的对象作为参数  
        Target mAdapter = new Adapter(new Adaptee());
        mAdapter.Request();
     
    }
}

https://www.jianshu.com/p/9d0575311214

3、代理模式

interface Subject{
    void request();
}

class RealSubject implements Subject{
    public void request(){
        System.out.println("request");
    }
}

class Proxy implements Subject{
    private Subject subject;
    public Proxy(){
    //编译时确定
        this.subject = new RealSubject() ;
    }
    public void request(){
    //访问控制
       if(){
          subject.request();
        }else{
          ....
        }       
    }
}
//测试类
public class ProxyDemo {
    public static void main(String args[]){    
        Proxy p = new Proxy();
        p.request();
    }
}

4、装饰者模式

//抽象构件角色

public interface Component {
    
    public void sampleOperation();
    
}
//具体构件角色

public class ConcreteComponent implements Component {

    @Override
    public void sampleOperation() {
        // 写相关的业务代码
    }
}
//装饰角色

public class Decorator implements Component{
    private Component component;
    
    public Decorator(Component component){
        this.component = component;
    }

    @Override
    public void sampleOperation() {
        // 委派给构件
        component.sampleOperation();
    }   
}
//具体装饰角色
public class ConcreteDecoratorA extends Decorator {

    public ConcreteDecoratorA(Component component) {
        super(component);
    }
    
    @Override
    public void sampleOperation() {
      // 写相关的业务代码
   super.sampleOperation();
     // 写相关的业务代码
    }
}

https://www.jianshu.com/p/d7f20ae63186

5、适配器模式、代理模式、装饰者模式的区别

6、建造者模式(builder)

  • 用于构建复杂对象

public class MyDialog {
    private String title;
    private String content;
    private String icon;

     static class MyBuilder {

        MyDialog dialog;

        public MyBuilder() {
            dialog = new MyDialog();
        }

        public MyBuilder setTile(String title) {
            dialog.title = title;
            return this;
        }

        public MyBuilder setContent(String content) {
            dialog.content = content;
            return this;
        }

        public MyBuilder setIcon(String icon) {
            dialog.icon = icon;
            return this;
        }

        public MyDialog create() {
            return dialog;
        }
    }

    @Override
    public String toString() {
        return "Dialog [title=" + title + ", content=" + content + ", icon=" + icon + "]";
    }

}

MyDialog.MyBuilder builder = new MyDialog.MyBuilder();
        MyDialog dialog = builder.setContent("内容").setTile("标题").setIcon("图标").create();
        System.out.println(dialog.toString());

https://www.jianshu.com/p/4dcc723b676e
https://blog.csdn.net/carson_ho/article/details/54910597

7、责任链模式

//抽象处理者
public abstract class Handler{
        //下一节点的处理者
        protected  Handler nextHandler;
        //满足条件则处理,不满足条件交给下一节点进行处理
        public abstract void handRequest(int i) ;
    }
    
    public class Handler1 extends Handler{
        @Override
        public void handRequest(int i) {
            if(i < 1) {
                System.out.println(i);
            }else {
                nextHandler.handRequest(i);
            }
            
        }
    }
    
    public class Handler2 extends Handler{
        @Override
        public void handRequest(int i) {
            if(i < 2) {
                System.out.println(i);
            }else {
                nextHandler.handRequest(i);
            }
            
        }
    }

         //测试代码
       Handler1 handler1 = new Handler1();
       Handler1 handler2 = new Handler2();
       handler1.nextHandler = handler2;
       handler1.handRequest(3);

相关文章

  • 【设计模式Android】中介者模式

    设计模式Android 其他相关文章:【设计模式Android】设计模式六大原则【设计模式Android】代理模式...

  • 设计模式相关

    1、观察者模式 https://www.cnblogs.com/luohanguo/p/7825656.html ...

  • 设计模式相关

    1、设计模式 设计模式是一套被反复使用,多数人知晓的,经过分类编目的、代码设计经验的总结。使用设计模式的目的是为了...

  • 设计模式相关

    uml、时序图和常用设计模式都介绍了。讲得比较详细。可以好好读读 http://design-patterns.r...

  • 设计模式(十四)中介者模式

    相关文章 设计模式(一)设计六大原则设计模式(二)单例模式的七种写法设计模式(三)建造者模式设计模式(四)简单工厂...

  • 2020-04-16

    iOS设计模式-策略模式 面向对象的设计模式中,我们可以把相关的算法分离为不同的类,成为策略。与这种做法相关的一种...

  • 23.策略模式(行为型)

    策略模式(行为型) 原书链接设计模式(刘伟) 适应算法灵活性而产生的设计模式——策略模式。 一、相关概述 1). ...

  • 【设计模式Android】代理模式

    设计模式Android 其他相关文章:【设计模式Android】设计模式六大原则 定义:为其他对象提供一种代理以控...

  • 构建基于功能稳定性和扩展性的设计分析模型

    最近看了一些的设计原则与设计模式相关的知识,基本每种设计模式都是基于六大设计原则去进行设计的,也了解了一些设计模式...

  • 设计模式(十五)状态模式

    相关文章设计模式系列 前言 建议在阅读本文前先阅读设计模式(十一)策略模式这篇文章,虽说状态模式和策略模式的结构几...

网友评论

      本文标题:设计模式相关

      本文链接:https://www.haomeiwen.com/subject/pkaqeftx.html