软考——上午—设计模式

设计模式要素和分类

软考——上午—设计模式_第1张图片

这里英文和中文对应都要背。

创建型设计模式(5)

软考——上午—设计模式_第2张图片

简单工厂模式

软考——上午—设计模式_第3张图片

/**
 * 简单工厂模式
 */
public class SimpleFactory {
    public static void main(String[] args) {
        Product a = Factory.createProduct("A");
        a.info();
        Product b = Factory.createProduct("B");
        b.info();
        Product c = Factory.createProduct("C");

    }
}
class Factory{
    public static Product createProduct(String type){
        Product product=null;
        switch(type){
            case "A":
                product=new ProductA();
                break;
            case "B":
                product=new ProductB();
                break;
            default:
                System.out.println("没有该类产品");
                break;
        }
        return product;
    }
}
abstract class Product{
    public abstract void info();
}
class ProductA extends Product{

    @Override
    public void info() {
        System.out.println("产品A");
    }
}
class ProductB extends Product{

    @Override
    public void info() {
        System.out.println("产品B");
    }
}

这里有个问题就是,要增加一个新产品时会修改工厂类。

为了遵循开放封闭原则,对扩展开放,对修改封闭。会把简单工厂模式变成工厂方法模式. 

工厂方法模式及其概念

这里左边是简单工厂,右边是工厂方法。右边每一个之类都有对应的创建方法。

工厂方法里面工厂类变成了一个接口,不同的实现类都要实现接口里面的createProduct方法。然后每一个实现类都是一个用户需要的实例。

就连产品也变成了接口形式,不同的产品要实现接口的方法。

软考——上午—设计模式_第4张图片

 官方的结构如下所示软考——上午—设计模式_第5张图片

软考——上午—设计模式_第6张图片

抽象工厂模式

一个工厂可以创建一系列产品就是抽象工厂,只能创建一类产品就是普通的工厂模式。

可以看见,抽象工厂模式里面的抽象工厂不单单可以创建一类产品了。创建的产品都是同一族的。

软考——上午—设计模式_第7张图片

软考——上午—设计模式_第8张图片生成器模式

软考——上午—设计模式_第9张图片

软考——上午—设计模式_第10张图片

//首先,我们创建产品类,表示最终的汽车对象:


@Data
class Car {
    private String brand;
    private String model;
    private String color;
    private String engineType;

    public Car(String brand, String model, String color, String engineType) {
        this.brand = brand;
        this.model = model;
        this.color = color;
        this.engineType = engineType;
    }

    // Getter methods for car properties
    // ...
}
//然后,我们创建一个抽象的生成器接口,定义了构建汽车的方法:
interface CarBuilder {
    void buildBrand(String brand);
    void buildModel(String model);
    void buildColor(String color);
    void buildEngineType(String engineType);
    Car getResult();
}

//接下来,我们实现具体的生成器类,负责构建汽车对象:
class CarBuilderImpl implements CarBuilder {
    private Car car;

    public CarBuilderImpl() {
        this.car = new Car("", "", "", "");
    }

    @Override
    public void buildBrand(String brand) {
        car.setBrand(brand);
    }

    @Override
    public void buildModel(String model) {
        car.setModel(model);
    }

    @Override
    public void buildColor(String color) {
        car.setColor(color);
    }

    @Override
    public void buildEngineType(String engineType) {
        car.setEngineType(engineType);
    }

    @Override
    public Car getResult() {
        return car;
    }
}

//最后,我们可以创建一个指导者类,用于指导生成器构建汽车对象:


class CarDirector {
    public Car constructSUV() {
        CarBuilder builder = new CarBuilderImpl();
        builder.buildBrand("Toyota");
        builder.buildModel("Rav4");
        builder.buildColor("Silver");
        builder.buildEngineType("Gasoline");
        return builder.getResult();
    }

    public Car constructSportsCar() {
        CarBuilder builder = new CarBuilderImpl();
        builder.buildBrand("Porsche");
        builder.buildModel("911");
        builder.buildColor("Red");
        builder.buildEngineType("Electric");
        return builder.getResult();
    }
}

//现在,我们可以使用生成器模式创建不同类型的汽车对象,而无需了解其构建细节:
public class Test {
    public static void main(String[] args) {
    CarDirector director = new CarDirector();

    Car suv = director.constructSUV();
    System.out.println("SUV: " + suv);

    Car sportsCar = director.constructSportsCar();
    System.out.println("Sports Car: " + sportsCar);
}
}

 生成器模式的关键在于,汽车生成器独立于汽车对象的内部表示和构建方式。生成器知道如何构建不同类型的汽车元素,但不需要了解汽车对象的具体实现细节。这种分离使得我们可以轻松地创建不同类型的汽车,同时保持了代码的可维护性和可扩展性。如果需要添加新类型的汽车元素,我们只需创建一个新的生成器实现即可,而不必修改现有的代码。这就是生成器模式中生成算法与对象组成的独立性的体现。

原型模式

软考——上午—设计模式_第11张图片

软考——上午—设计模式_第12张图片

这里克隆返回的是一个新的对象,但是属性值相同。 

单例模式

软考——上午—设计模式_第13张图片

软考——上午—设计模式_第14张图片 如上所示,这个类的构造方法被私有化了,没办法通过new来创建它。只能通过提供的方法获得唯一的一个实例。

结构型设计模式(7)

适配器模式

软考——上午—设计模式_第15张图片

 java中没有多重继承,所以类适配器不用看。

软考——上午—设计模式_第16张图片软考——上午—设计模式_第17张图片

  代码如下,其实就是增加一个中间的实现类包含了要使用的类。

软考——上午—设计模式_第18张图片

桥接模式

软考——上午—设计模式_第19张图片

软考——上午—设计模式_第20张图片 在普通情况下,有n种产品,m种颜色时可能会有1+n+n*m种类存在。软考——上午—设计模式_第21张图片

 如果是在桥接模式下就只有n+m+2.软考——上午—设计模式_第22张图片

 软考——上午—设计模式_第23张图片

典型例题, 

软考——上午—设计模式_第24张图片

组合模式

软考——上午—设计模式_第25张图片

软考——上午—设计模式_第26张图片

这里写的好像有点问题,应该是在Component接口中定义行为,在Composite中实现有关操作。 

 上面的类的层次图大概就是这个模样。软考——上午—设计模式_第27张图片

软考——上午—设计模式_第28张图片

装饰器模式

软考——上午—设计模式_第29张图片

软考——上午—设计模式_第30张图片

代码实现上就是套娃.

外观模式

软考——上午—设计模式_第31张图片

相当于提供一个导航栏,新增什么功能就往导航栏里面加元素 。

软考——上午—设计模式_第32张图片

享元模式

软考——上午—设计模式_第33张图片

软考——上午—设计模式_第34张图片

软考——上午—设计模式_第35张图片 这里感觉好麻烦,不看了。

代理模式

软考——上午—设计模式_第36张图片

 软考——上午—设计模式_第37张图片

软考——上午—设计模式_第38张图片

软考——上午—设计模式_第39张图片

行为设计模式(11)

责任链模式

软考——上午—设计模式_第40张图片

软考——上午—设计模式_第41张图片

命令模式

软考——上午—设计模式_第42张图片

软考——上午—设计模式_第43张图片软考——上午—设计模式_第44张图片

解释器模式(沒考過)

软考——上午—设计模式_第45张图片

软考——上午—设计模式_第46张图片

迭代器模式

软考——上午—设计模式_第47张图片

软考——上午—设计模式_第48张图片

中介者模式

软考——上午—设计模式_第49张图片

软考——上午—设计模式_第50张图片

备忘录模式

软考——上午—设计模式_第51张图片

软考——上午—设计模式_第52张图片

观察者模式(重要考点)

软考——上午—设计模式_第53张图片

软考——上午—设计模式_第54张图片 软考——上午—设计模式_第55张图片

状态模式

软考——上午—设计模式_第56张图片

软考——上午—设计模式_第57张图片

策略模式

软考——上午—设计模式_第58张图片

软考——上午—设计模式_第59张图片 

模板方法模式

软考——上午—设计模式_第60张图片

软考——上午—设计模式_第61张图片 

访问者模式

软考——上午—设计模式_第62张图片

软考——上午—设计模式_第63张图片 软考——上午—设计模式_第64张图片

软考——上午—设计模式_第65张图片

你可能感兴趣的:(软考专区,设计模式,java)