-
简单工厂模式:
/** * 产品基类 * @author Administrator */ public interface IProduct { /** * 产品描述 */ void description(); } public class ProductA implements IProduct { @Override public void description() { System.out.println("This is product A!"); } } public class ProductB implements IProduct { @Override public void description() { System.out.println("This is product B!"); } }工厂类:
/** * @author Java设计模式-创建型模式之工厂模式 * @description 简单工厂模式: * 缺点:新增产品时,需要新增产品类并修改工厂类。(扩展性差) * @date 2019-04-12 15:08 */ public class ProductFactory { private static final int PRODUCT_A = 1; private static final int PRODUCT_B = 2; public static IProduct getProductByType(int type){ switch (type){ case PRODUCT_A: return new ProductA(); case PRODUCT_B: return new ProductB(); default: return new ProductA(); } } public static void main(String[] args) { IProduct project = ProductFactory.getProductByType(ProductFactory.PRODUCT_A); project.description(); } } - 工厂方法模式:
提供一个用于创建对象的接口(工厂接口),让其实现类(工厂实现类)决定实例化哪一个类(产品类),并且由该实现类创建对应类的实例。
public interface IFactory { /** * 创建产品 * @param productType * @return */ IProduct createProduct(String productType); } public class ProductFactory implements IFactory { private static final String PRODUCT_A = "A"; private static final String PRODUCT_B = "B"; IProduct product; Map<String, Object> productParam = new HashMap<String, Object>(); @Override public IProduct createProduct(String productType) { switch (productType){ case PRODUCT_A: product = new ProductA(); productParam.put("param", "A"); break; case PRODUCT_B: product = new ProductB(); productParam.put("param", "B"); break; default: product = new ProductB(); productParam.put("param", "B"); } product.setProductParam(productParam); return product; } public static void main(String[] args) { ProductFactory productFactory = new ProductFactory(); IProduct product; try { product = productFactory.createProduct("A"); product.description(); product = productFactory.createProduct("B"); product.description(); } catch (Exception e) { e.printStackTrace(); } } }产品相关:
public interface IProduct { /** * 获取生产产品参数 * @return */ Map<String, Object> getProductParam(); /** * 设置生产产品参数 */ void setProductParam(Map<String, Object> productParam); /** * 产品描述 */ void description() throws Exception; } public class AbstractProduct implements IProduct { private Map<String, Object> productParam; @Override public Map<String, Object> getProductParam() { return productParam; } @Override public void setProductParam(Map<String, Object> productParam) { this.productParam = productParam; } @Override public void description() throws Exception { } } public class ProductA extends AbstractProduct{ @Override public void description() throws Exception{ Map<String, Object> productParam = getProductParam(); if(productParam==null){ throw new Exception("参数不能为空!"); }else { System.out.println("productParam="+productParam.get("param")); } System.out.println("This is product A!"); } } public class ProductB extends AbstractProduct{ @Override public void description() throws Exception{ Map<String, Object> productParam = getProductParam(); if(productParam==null){ throw new Exception("参数不能为空!"); }else { System.out.println("productParam="+productParam.get("param")); } System.out.println("This is product B!"); } }
相关推荐
25年PINN创新点大盘点!
在科研论文的激烈角逐中,想轻松产出高含金量成果?物理信息神经网络(PINN)堪称“宝藏研究方向”,是冲击高分区论文的绝佳赛道!对于渴望发表优质论文的科研伙伴,这三大研究路径值得重点关注。首先是自适应PINN,在PINN训练过程中,物理方程与实测数据间的矛盾常常导致权重调节困难,而恰好直击这一痛点,是极具价值的研究方向。其次,同样前景广阔,传统多物理场同时训练效果欠佳,如今分阶段训练方法脱颖而出,成效显著且竞争较小。最后,探索及多任务学习等技术的融合,能实现优势叠加,碰撞出创新火花。
【java设计模式】创建型模式介绍(工厂模式、抽象工厂模式、单例模式、建造者模式、原型模式)
本文介绍Java设计模式中创建型模式的五种
Solidworks钣金设计:折弯系数、K因子与折弯扣除的实战应用解析
本文深入解析Solidworks钣金设计中折弯系数、K因子与折弯扣除的核心概念与实战应用。通过厘清三者间的逻辑关系与换算方法,并结合材料特性、工艺参数及车间实测,指导工程师精准确定展开长度,确保设计图纸与加工实物的一致性,有效提升钣金件设计精度与生产效率。
Java创建型设计模式之简单工厂,工厂方法,抽象工厂
在Java的23中设计模式中,其中有创建型的一类创建模式,通常用来抽象封装创建对象来使用的,这一类创建模式是使得客户端调用者不用关心对象是如何创建,统统交给一个叫做“工厂”的对象来创建对应的对象。这样就能做到客户端不与想要创建的对象紧密地耦合,降低耦合度。 1.简单工厂 简单工厂具体来并不属于Java中的23种设计模式中之一,它只是一种对创建对象的简单的封装,只需要传入一个正确的参数,就...
2.4G跳频算法 2.4G切信道算法 (简例示意nRF52832)
2.4G跳频算法 nRF52832示例 2.4G自适应信道算法
Qt绘图库对决:QWT与QCustomPlot的实战选型指南
本文为Qt开发者提供了QWT与QCustomPlot两大绘图库的实战选型指南。文章从功能特性、性能资源、开发效率三大维度进行深度对比,并结合工业监控、科学计算、嵌入式设备等典型场景,给出了清晰的决策清单。对于需要专业科学图表(如极坐标、等高线图)的场景推荐QWT;对于追求高性能、实时性、快速开发及嵌入式部署的通用图表需求,QCustomPlot是更优选择。
Java设计模式——工厂模式(创建型)
Java设计模式——工厂模式(创建型)简介
摄像头模组选型避坑指南:如何根据Sensor和Lens参数匹配最佳组合
本文深入解析摄像头模组选型中Sensor与Lens的核心参数匹配要点。重点阐述了主光线角(CRA)匹配对边缘画质的关键影响,并提供了传感器尺寸、像素尺寸、焦距、光圈等参数的协同选型策略与实战避坑指南,帮助工程师在手机、安防、车载等不同应用场景下实现性能与成本的最佳平衡。
Java设计模式-内功修炼-创建型设计模式-工厂模式
创建型设计模式 知识补充 java反射机制 此处仅作最简单的应用型描述,因为下面会用到,所以在此补充,读者如果想深入了解java反射机制,请自行查资料。 举个简单例子: Class c = Class.forName("String"); Object obj = c.newInstance(); return obj; 上面的代码能够返回一个String 对象(与new String()是一样...
二、ROS2完成Docker容器和宿主机通信,使用ros2 topic list看到,但是无法echo
如果容器和主机权限不一致,则会导致无法通信(我的代码中,docker容器是root,宿主机是普通用户),虽然可以查询到相关话题,但是订阅后没有消息输出。比如,docker容器创建了一个共享内存,其所有者为docker容器用户root,当宿主机去访问的时候,由于用户不同,没有权限访问,也就无法进行信息传递。该方法只是临时修改共享内存的权限,每次运行完,需要重新运行上述命令,才可以进行通信。在运行的过程中,ROS2使用了共享内存机制,会共享内存。运行之后,宿主机和容器可能用户名不同,这是因为。
仓储控制系统(WCS)设计
WCS系统设计方法
初探Java设计模式1:创建型模式(工厂,单例等)
Java 设计模式 转自https://javadoop.com/post/design-pattern 系列文章将整理到我在GitHub上的《Java面试指南》仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下Star、fork哈 文章也将发表在我的个人博客,阅读体验更佳: www.how2playlife...
16、树莓派Pico W:蓝牙与Wi-Fi应用指南
本文介绍了树莓派Pico W在蓝牙与Wi-Fi通信方面的应用,涵盖蓝牙配对原理、使用HC-06模块实现手机远程控制LED和传输内部温度,以及通过Wi-Fi进行网络连接、扫描和UDP协议下的设备控制。文章包含详细电路连接、代码实现与测试步骤,并提供拓展建议、常见问题解决方法及开发注意事项,帮助读者快速掌握基于Pico W的无线通信项目开发,适用于物联网入门与实践场景。
【Java 设计模式】创建型之工厂方法模式
工厂方法模式是一种创建型设计模式,它定义了一个用于创建对象的接口,但是将对象的实例化延迟到子类中。这样,在实际使用时,客户端代码不需要关心具体实例是哪个类,只需通过工厂方法获取对象即可。工厂方法模式通过解耦了客户端和具体类的关系,使系统更加灵活。工厂方法模式是一种常见且实用的设计模式,它通过定义一个工厂方法接口,将对象的实例化延迟到子类中,从而实现了客户端和具体类的解耦。在实际开发中,工厂方法模式常用于创建复杂对象、管理对象的生命周期等场景。通过灵活使用工厂方法模式,可以使系统更容易扩展和维护。
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
Java 创建型设计模式 - 抽象工厂模式
抽象工厂模式是创建型设计模式,它提供了一个创建一系列相关或相互依赖对象的接口。通过抽象工厂,我们可以解耦客户端和具体的工厂、产品,使得系统更加灵活和可扩展。优点抽象工厂模式提供了一个用于创建一系列相关产品的接口,使得产品的创建和使用解耦。如果需要增加新的产品,只需要增加具体工厂和产品类,而无需修改现有的代码,符合开闭原则。客户端只依赖于抽象工厂接口,不需要了解具体的实现,从而提高了系统的可维护性。缺点随着产品系列和产品类型的增加,抽象工厂模式的类和接口会迅速增多,系统可能变得更加复杂。
VC中sqlite的使用
在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示
java 单例模式_初探Java设计模式1:创建型模式(工厂,单例等)
Java 设计模式转自https://javadoop.com/post/design-pattern系列文章将整理到我在GitHub上的《Java面试指南》仓库,更多精彩内容请到我的仓库里查看https://github.com/h2pl/Java-Tutorial喜欢的话麻烦点下Star、fork哈文章也将发表在我的个人博客,阅读体验更佳:www.how2playlife.com本文...
Java设计模式-(创建型)抽象工厂模式
抽象工厂模式(Abstract Factory Pattern):通过构造一个接口充当超级工厂(即抽象工厂),来创建其他工厂核心定义:定义一个对象接口,用于规范所有对象;定义一个工厂抽象类,用于规范工厂类获取对象的方式;再提供一个生成器类,提供静态方法对象统一提供获取工厂的方法。优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象缺点:由于存在两层封装,若扩展比较麻烦应用场景:针对不同操作系统进行适配的场景;针对不用语言/时区进行适配的场景。
23种设计模式-创建型模式之工厂方法模式(Java版本)
它让类的实例化推迟到子类进行,实现了。
【Java 设计模式】创建型之抽象工厂模式
抽象工厂模式是一种创建型设计模式,它提供了一个接口,用于创建一系列相关或相互依赖的对象,而无需指定它们具体的类。抽象工厂模式将一组相关的产品组合成一个工厂,客户端通过工厂接口创建一系列产品。这有助于实现产品族的概念,其中产品族是指一组相关的产品,例如不同品牌的汽车。抽象工厂模式是一种常见且实用的设计模式,它通过定义一个工厂接口,将一系列相关或相互依赖的对象的创建延迟到具体工厂的实现类中,从而实现了客户端和具体类的解耦。在实际开发中,抽象工厂模式常用于创建一系列相关的产品,如美食店中的可乐、汉堡包等。
设计模式快速入门——创建型模式之工厂模式(Java)
本文将快速入门创建型设计模式中的工厂模式。工厂模式是一种创建型设计模式,它提供了一种创建对象的接口,但允许子类决定实例化哪个类。这样,工厂模式在创建对象时将决策从客户端代码中分离出来。工厂模式的核心思想是定义一个工厂接口,由具体的工厂类来实现这个接口以创建对象。客户端代码通过工厂接口来请求对象,而不直接实例化具体对象。工厂模式旨在将对象的创建过程与其使用分离,以提高代码的可维护性和扩展性。有三种常见的工厂模式:简单工厂模式、工厂方法模式和抽象工厂模式。
介绍设计模式的创建型模式的抽象工厂,并用java代码实现
抽象工厂模式是创建型设计模式之一,它提供了一种创建对象的最佳方式。 在抽象工厂模式中,接口用于创建相关或依赖对象的工厂,而不是直接实例化对象。这样,系统可以具有更好的扩展性,因为在系统中加入新的产品时无需修改已有的代码。 例如,我们可以使用抽象工厂模式来创建不同类型的汽车,如油车和电动车。我们可以使用抽象工厂来创建不同品牌的汽车,如奥迪和宝马。 这是使用java实现抽象工厂模式的一个示例: ```...
java设计模式一创建型模式一简单工厂模式
java设计模式一简单工厂模式
【Java设计模式 - 创建型模式3】抽象工厂模式
java设计模式之工厂模式简介描述实现 简介 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。 描述 意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 主要解决:主要解决接口选择的问题。 何时使用:系统的产品有多于一
java设计模式day02--(创建型模式:工厂模式、原型模式、建造者模式)
在java中,万物皆对象,这些对象都需要创建,如果创建的时候直接new该对象,就会对该对象耦合严重,假如我们要更换对象,所有new对象的地方都需要修改一遍,这显然违背了软件设计的开闭原则。这些工厂只生产同种类产品,同种类产品称为同等级产品,也就是说:工厂方法模式只考虑生产同等级的产品,但是在现实生活中许多工厂是综合型的工厂,能生产多等级(种类) 的产品,如电器厂既生产电视机又生产洗衣机或空调,大学既有软件专业又有生物专业等。纵轴是产品族,也就是同一品牌的产品,同一品牌的产品产自同一个工厂。
Java23种设计模式——3.创建型模式之工厂模式
Java中除去有设计原则之外,还有23中设计模式。(设计模式我们主要来将思想和精髓的地方,除去Java还有其他的语言也可以适用) 这些模式都是前辈们一点一点积累下来,一直在改进,一直在优化的,这些设计模式可以解决一些特定的问题。 这些模式中,可以说是将语言的使用提现的淋漓尽致,我们今天要学习第一种设计模式,工厂模式! 工厂模式: 先来说说什么是工厂模式: 工厂模式,顾名思义,就是有一个工...
1972



被折叠的 条评论
为什么被折叠?



