设计模式

作者: 雯艺雪 | 来源:发表于2019-04-22 17:54 被阅读0次

1、单例模式

当需要使用某个全局共享访问点或共享数据时,或某个工具类时使用。

(1)静态内部类形式

   public class ProductRobot {
    private RobotFactory robotFactory;
    public static ProductRobot getInstance(){
        return ProductHolder.productRobot;
    }

    private static class ProductHolder{
        private static ProductRobot productRobot=new ProductRobot();
    }

   
    }

(2)DCL:双重检查模式

   private static volatile ProductRobot productRobot;
    public static ProductRobot getProductRobot(){
        if (productRobot==null){
            synchronized (ProductRobot.class){
                if (productRobot==null) productRobot=new ProductRobot();
            }
        }
        return productRobot;
    }

特点:返回某个类的引用

二、工厂模式

定义:定义一个用于创建对象的接口,让子类决定实例化哪个类。工厂方法使一个类的实例延迟到其子类。

(1)产品抽象类

    public interface Robot extends ProxyMode{
    String TAG="RobotPrint";
    void run();
    void walk();
    void talk();
    void createHead(String head);
    void createBody(String body);
    void createCPU(String cpu);
    void printMessage();
    void buy();
}

(2)具体产品类

    public class CatRobot implements Robot {
    @Override
    public void run() {
        LogUit.printLog("i",TAG,"机器猫跑步了");
    }

    @Override
    public void walk() {
        LogUit.printLog("i",TAG,"机器猫走路");
    }

    @Override
    public void talk() {
        LogUit.printLog("i",TAG,"我是机器猫");
    }

    private String head,body,cpu;
    @Override
    public void createHead(String head) {
        this.head=head;
    }

    @Override
    public void createBody(String body) {
        this.body=body;
    }

    @Override
    public void createCPU(String cpu) {
        this.cpu=cpu;
    }

    @Override
    public void printMessage() {
        LogUit.printLog("i",TAG,"head="+head+",body="+body+",cpu="+cpu);
    }

    @Override
    public void buy() {
        LogUit.printLog("i",TAG,"机器猫购物");
    }
}

(3)工厂抽象类

    public interface RobotFactoryMode {
        <T extends Robot> T createRobot(Class<T> tClass);
     }

(4)具体工厂类

    public class RobotFactory implements RobotFactoryMode {
    private Robot robot;

    @Override
    public <T extends Robot> T createRobot(Class<T> tClass) {
        try {
            robot= (Robot) Class.forName(tClass.getName()).newInstance();//发射
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return (T) robot;
    }
}

这样,当调用工厂的生产方法createRobot时将产品类传递进去即可产生具体产品。

特点:适用于生产不同的子类对象,这些子类具有相同的基类

三、建造者模式

定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
包含:Director、Builder、Product
(1)产品类(如上)
(2)Builder:相同的执行步骤

    public interface BuilderMode {
    void createHead(String head);
    void createBody(String body);
    void createCPU(String cpu);
    Robot create();
}

public class RobotBuilder implements BuilderMode{
    private Robot robot;
    public RobotBuilder(Robot robot){
        this.robot=robot;
    }

    @Override
    public void createHead(String head) {
        robot.createHead(head);
    }

    @Override
    public void createBody(String body) {
        robot.createBody(body);
    }

    @Override
    public void createCPU(String cpu) {
        robot.createCPU(cpu);
    }

    @Override
    public Robot create() {
        return robot;
    }
}

(3)Director:导演类,负责安排已有模块顺序,通过Builder开始建造

    
public class Director {
    private RobotBuilder robotBuilder;
    public Director(RobotBuilder robotBuilder){
        this.robotBuilder=robotBuilder;
    }

    public Robot createRobot(String head,String body,String cpu){
        robotBuilder.createHead(head);
        robotBuilder.createBody(body);
        robotBuilder.createCPU(cpu);
        return robotBuilder.create();
    }
}

特点:适用于不同的子类有相同的执行步骤,建造产品嘛

四、装饰器模式

定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器比生成子类更为灵活。
(1)对象(如上)
(2)装饰器类:

    
public interface DecoratorMode {
    String TAG="RobotPrint";
    void behavior();
}


/**
 * 具体装饰器
 */
public class RobotDecorator implements DecoratorMode{
    private Robot robot;
    public RobotDecorator(Robot robot){
        this.robot=robot;
    }

    private void eat(String fish) {
        LogUit.printLog("i",TAG,"机器人吃"+fish+"了");
    }

    private void drink(String water) {
        LogUit.printLog("i",TAG,"机器人喝"+water+"了");
    }

    public Robot getRobot() {
        return robot;
    }

    @Override
    public void behavior() {
        eat("鱼");drink("牛奶");
        eat("猫粮");drink("水");
    }
}

这样,只要继承DecoratorMode 实现装饰器类都可以对具体对象Robot的功能进行扩充,调用装饰器时既可以使用具体对象的方法又可以调用装饰器内新实现的方法。

优点:通过组合而非继承的方式,动态地扩展一个对象的功能,运行时选择不同的装饰器从而实现不同的功能
缺点:如果抽象对象发生改变,所有装饰器和具体对象(被装饰者)都受影响;由于比继承更灵活,装饰模式更加易于出错,也更加难以排错。

相关文章

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • 设计模式

    《C#设计模式》 《C#设计模式》-设计模式概述 《C#设计模式》-面向对象设计原则 《C#设计模式》-单例模式 ...

  • 浅谈JS的一些设计模式

    @(书籍阅读)[JavaScript, 设计模式] 常见设计模式 设计模式简介 设计模式概念解读 设计模式的发展与...

  • 前端设计模式

    JS设计模式一:工厂模式jS设计模式二:单例模式JS设计模式三:模块模式JS设计模式四:代理模式JS设计模式五:职...

  • 设计模式之工厂模式

    设计模式之工厂模式 标签(空格分隔): 设计模式 工厂模式 设计模式的感念 设计模式的应用 工厂设计模式的产生 工...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 第1章 设计模式概述

    一、设计模式的概念 二、设计模式的历史 三、设计模式的要素 四、设计模式的分类 ■ 创建型设计模式 ■ 结构型设计...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

网友评论

    本文标题:设计模式

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