首页 > 其他分享 >[设计模式]工厂模式

[设计模式]工厂模式

时间:2024-08-09 19:51:28浏览次数:5  
标签:return ICar 模式 public 工厂 new Operation 设计模式 class

简单工厂

// 计算类的基类
@Setter
@Getter
public abstract class Operation {
    private double value1 = 0;
    private double value2 = 0;
    protected abstract double getResule();
}

//加法
public class OperationAdd extends Operation {
    @Override
    protected double getResule() {
        return getValue1() + getValue2();
    }
}
//减法
public class OperationSub extends Operation {
    @Override
    protected double getResule() {
        return getValue1() - getValue2();
    }
}
//乘法
public class OperationMul extends Operation {
    @Override
    protected double getResule() {
        return getValue1() * getValue2();
    }
}
//除法
public class OperationDiv extends Operation {
    @Override
    protected double getResule() {
        if (getValue2() != 0) {
            return getValue1() / getValue2();
        }
        throw new IllegalArgumentException("除数不能为零");
    }
}
//工厂类
public class OperationFactory {
    public static Operation createOperation(String operation) {
        Operation oper = null;
        switch (operation) {
            case "add":
                oper = new OperationAdd();
                break;
            case "sub":
                oper = new OperationSub();
                break;
            case "mul":
                oper = new OperationMul();
                break;

            case "div":
                oper = new OperationDiv();
                break;
            default:
                throw new UnsupportedOperationException("不支持该操作");
        }
        return oper;
    }
}
public static void main(String[] args) {
  Operation operationAdd = OperationFactory.createOperation("add");
  operationAdd.setValue1(1);
  operationAdd.setValue2(2)
  System.out.println(operationAdd.getResule());
}

工厂方法

//工厂接口
public interface IFactory {
    Operation CreateOption();
}

//加法类工厂
public class AddFactory implements IFactory {
    public Operation CreateOption() {
        return new OperationAdd();
    }
}

//减法类工厂
public class SubFactory implements IFactory {
    public Operation CreateOption() {
        return new OperationSub();
    }
}

//乘法类工厂
public class MulFactory implements IFactory {
    public Operation CreateOption() {
        return new OperationMul();
    }
}

//除法类工厂
public class DivFactory implements IFactory {
    public Operation CreateOption() {
        return new OperationDiv();
    }
}
public class Client {
    public static void main(String[] args) {
      //减法
      IFactory subFactory = new SubFactory();
      Operation operationSub =  subFactory.CreateOption();
      operationSub.setValue1(22);
      operationSub.setValue2(20);
      System.out.println("sub:"+operationSub.getResult());
      //除法
      IFactory Divfactory = new DivFactory();
      Operation operationDiv =  Divfactory.CreateOption();
      operationDiv.setValue1(99);
      operationDiv.setValue2(33);
      System.out.println("div:"+operationSub.getResult());
    }
}

抽象工厂

public class Engine {

    public void getStyle(){
        System.out.println("这是汽车的发动机");
    }
}

public class UnderPan {

    public void getStyle(){
        System.out.println("这是汽车的底盘");
    }
}

public class Wheel {

    public void getStyle(){
        System.out.println("这是汽车的轮胎");
    }
}


public interface ICar {
    void show();
}


@Getter
@Setter
@AllArgsConstructor
public class AudiCar implements ICar {
    private Engine engine;
    private Underpan underpan;
    private Wheel wheel;


    public void show() {
        engine.getStyle();
        underpan.getStyle();
        wheel.getStyle();
        System.out.println("造了一台奥迪汽车");
    }
}

@Getter
@Setter
@AllArgsConstructor
public class BenzCar implements ICar {


    private Engine engine;
    private Underpan underpan;
    private Wheel wheel;


    public void show() {
        engine.getStyle();
        underpan.getStyle();
        wheel.getStyle();
        System.out.println("造了一台奔驰汽车");
    }
}

public interface IFactory {

    ICar createBenzCar();
    ICar createAudiCar();
}


public class Factory implements IFactory {
    public ICar createBenzCar() {
        Engine engine = new Engine();
        Underpan underpan = new Underpan();
        Wheel wheel = new Wheel();
        ICar car = new BenzCar(engine, underpan, wheel);
        return car;
    }

    public ICar createAudiCar() {
        Engine engine = new Engine();
        Underpan underpan = new Underpan();
        Wheel wheel = new Wheel();
        ICar car = new AudiCar(engine, underpan, wheel);
        return car;
    }
}


public class AbstractFactoryMain {
    public static void main(String[] args) {
        IFactory factory = new Factory();
        ICar benzCar = factory.createBenzCar();
        benzCar.show();

        ICar audi = factory.createAudiCar();
        audi.show();
    }
}

标签:return,ICar,模式,public,工厂,new,Operation,设计模式,class
From: https://www.cnblogs.com/DCFV/p/18351369

相关文章

  • 工厂模式【简单工厂工厂方法抽象工厂】
    简单工厂:将对象的创建封装在子类当中,客户不需要去手动创建对象,只需要调用一些接口传递不同的类型参数来创建对象。但是在一个工厂里存在多种产品,会导致工厂的频繁修改工厂方法:实现了不同的产品存在于不同的工厂,使其解耦抽象工厂:实现了不同的工厂里面不止一种产品 简单......
  • [设计模式]单例模式
    单例模式懒汉式,线程不安全publicclassSingleton{privatestaticSingletoninstance;privateSingleton(){}publicstaticSingletongetInstance(){if(instance==null){instance=newSingleton();}......
  • Rust实现构建器模式和使用Bon库中的构建器
    实现构建器模式的一种方式这里参考资料2的文章,修改部分代码后如下。这段代码的目的是使用构建器模式创建和初始化Person对象。以下是各部分的解释:结构体定义Person:定义了一个结构体,包含name、age、address和sex四个字段。address和sex是可选的PersonBuilder:用于逐步构......
  • [设计模式]装饰者模式
    抽象构件publicabstractclassFastFood{publicStringdesc;publicintprice;publicabstractStringgetDesc();publicabstractintgetPrice();}具体构件米饭publicclassRiceextendsFastFood{publicRice(){this.desc......
  • “斯诺克”不等于“台球”-《分析模式》漫谈17
    DDD领域驱动设计批评文集做强化自测题获得“软件方法建模师”称号《软件方法》各章合集“AnalysisPatterns”的第一章有这么一句:Considersomeonewhowantstowritesoftwaretosimulatea game of snooker. 2004(机械工业出版社)中译本的译文为: game翻译成“游......
  • 工厂英文 | 之[装配线]
    1.产品相关中文英文缩写产品Product、Production 半成品SemiFinishedGoods、Semi-ManufacturedGoods 成品FinishedGoods、FinishedProduct、FinishedArticles 最终产品FinalGoodsFG零件、部件AssemblyPart 副产品By-Product 副......
  • 一二三物联网智慧工厂运维管理平台
    智慧工厂运维管理平台是一种基于信息技术和物联网技术的管理平台,主要用于智能制造领域中的设备管理、生产管理和质量管理。该平台能够实现对工厂生产过程中的各种设备、生产线、生产计划、生产数据、质量数据等信息的实时监测、预测、维护和管理,以提高生产效率和降低生产成本。......
  • K8S中,flannel有几种模式?
    在Kubernetes(K8S)中,Flannel作为一个流行的容器网络接口(CNI)插件,用于为集群中的容器提供网络互通能力。Flannel支持多种模式来实现其网络功能,主要包括以下几种常见模式:1.VXLAN模式描述:VXLAN(VirtualExtensibleLAN)是Flannel的默认后端驱动,它使用VXLAN封装技术来创建跨节点的虚拟......
  • 基于java+springboot+vue基于MVC模式的考研论坛交流管理系统的设计与实现万字文档和PP
    前言......
  • 策略模式揭秘:如何让飞书、企业微信、钉钉的入职与生日祝福更智能?
    继上一篇飞书、企业微信、钉钉如何精准推送入职与生日祝福背后的数据魔法之后,今天在此基础上分享下策略模式。策略模式是一种行为型设计模式,在工作中使用的频次非常高。生日祝福,入职周年祝福等,每一种都是一种不同的策略。不了解背景的人可以先去看看入职周年祝福与生日......