java设计模式————装饰者模式,第三方登陆的第二种写法

QQ登陆第三方Demo(QQ互联) QQ登陆第三方web网站。 阅读详情

装饰者模式(Decorator Pattern)

指在不改变原有对象的基础之上,将功能附加到对象上,提供了比继承更有弹性的替代方案(扩展原有对象的功能)

属于结构型模式

适用场景:

1.用于扩展一个类的功能或者给一个类添加附加职责。

2.动态的给一个对象添加功能,这些功能可以动态的撤销。

现实场景:

大家经常吃炒粉不是,有原味,有加肉,有加蛋,也有我全都要的,我们马上想到了代码形式。

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 原味炒粉
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 15:55 2020-3-14
 */
public class Noodles {

    protected String getMsg(){
        return "炒粉";
    }

    protected int getPrice(){
        return 6;
    }

}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 15:59 2020-3-14
 */
public class NoddlesWithEggs extends Noodles {
    @Override
    protected String getMsg() {
        return super.getMsg()+",加一个鸡蛋";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+1;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:01 2020-3-14
 */
public class NoodlesWithEggsAndMeat extends NoddlesWithEggs{
    @Override
    protected String getMsg() {
        return super.getMsg()+",加点猪肉";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+2;
    }
}

测试下运行:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:02 2020-3-14
 */
public class client {
    public static void main(String[] args) {
        //来份原味炒粉
        Noodles noodles = new Noodles();
        System.out.println(noodles.getMsg() + "价格:"+noodles.getPrice());
        //来份鸡蛋炒粉
        NoddlesWithEggs noddlesWithEggs = new NoddlesWithEggs();
        System.out.println(noddlesWithEggs.getMsg() + "价格:"+noddlesWithEggs.getPrice());
        //我全都要,来份加肉加蛋的
        NoodlesWithEggsAndMeat noodlesWithEggsAndMeat = new NoodlesWithEggsAndMeat();
        System.out.println(noodlesWithEggsAndMeat.getMsg() + "价格:"+noodlesWithEggsAndMeat.getPrice());
    }
}

 

但是有的人又会说了,我要加二个鸡蛋怎么办呢,显然,不可能来个类noddlesWithDoubleEgg,对吧,所以我们继续进行优化。,这个时候装饰者模式就登场了。

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 此时炒粉就变成了一个抽象的炒粉了
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:13 2020-3-14
 */
public abstract class Noodles {

    //信息
    protected abstract String getMsg();

    //价格
    protected abstract int getPrice();
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  炒粉装饰器
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:16 2020-3-14
 */
public abstract class NoodlesDecorator extends Noodles{

    //又像静态代理
    private Noodles noodles;

    public NoodlesDecorator(Noodles noodles){
        this.noodles = noodles;
    }

    @Override
    protected String getMsg() {
        return this.noodles.getMsg();
    }

    @Override
    protected int getPrice() {
        return this.noodles.getPrice();
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 基础套餐,原味炒粉
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:15 2020-3-14
 */
public class BaseNoodles extends Noodles{
    @Override
    protected String getMsg() {
       return  "原味炒粉";
    }

    @Override
    protected int getPrice() {
        return 6;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 加鸡蛋
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:21 2020-3-14
 */
public class EggsDecorator extends NoodlesDecorator{

    public EggsDecorator(Noodles noodles) {
        super(noodles);
    }

    @Override
    protected String getMsg() {
        return super.getMsg()+ "+1个鸡蛋";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+1;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 加肉
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:23 2020-3-14
 */
public class MeatDecorator extends NoodlesDecorator{

    public MeatDecorator(Noodles noodles) {
        super(noodles);
    }

    @Override
    protected String getMsg() {
        return super.getMsg() + "+1份肉";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+2;
    }
}

测试运行一下:

 

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:25 2020-3-14
 */
public class DecoratorTest {
    public static void main(String[] args) {

        Noodles noodles;
        //路边买了个原味炒粉
        noodles = new BaseNoodles();
        //不爽啊,来个蛋
        noodles = new EggsDecorator(noodles);
        //还是不行啊,我全都要,再加份肉
        noodles = new MeatDecorator(noodles);
        //还是不行,真男人,我要双黄蛋,再来一个蛋
        noodles = new EggsDecorator(noodles);
        System.out.println(noodles.getMsg() + "价格:"+noodles.getPrice());
    }

 这就是装饰者模式的功能了,想加几个蛋就加几个,你还可以继续加香肠也没问题,不需要再去改变原有的代码。

好,上面那个是现实中的例子,现在我们来举个代码中用到的例子,第三方登陆形式二。

 初始登陆方式:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 登陆接口
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:44 2020-3-14
 */
public interface LoginService {

    public MsgResult login(String username, String password);

    public MsgResult register(String username,String password);
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  相当于系统已经写好的代码,不去动它
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 13:38 2020-3-14
 */
public class LoginServiceImpl implements LoginService{

    //用户名,密码登陆方法
    @Override
    public MsgResult login(String username, String password){
        System.out.println("用户pc登陆");
        return null;
    }

    //注册方法
    @Override
    public MsgResult register(String username,String password){
        return login(username,password);
    }
}

/**
 * @Author Darker
 * @Descrption
 * @Date : Created in 16:07 2020-3-12
 */
@Data
@AllArgsConstructor
public class MsgResult {

    private int code;
    private Object data;
    private String msg;

    public static MsgResult success(Object data){
        return new MsgResult(200,data,"成功");
    }

    public static MsgResult fail(Object data){
        return new MsgResult(404,data,"失败");
    }

}

/**
 * @Author Darker
 * @Descrption
 * @Date : Created in 10:57 2020-3-13
 */
@Data
public class User {
    private String id;

    private String usename;

    private String password;

    private int age;

    private String addr;
}

配上第三方登陆,使用装饰器加强来实现,不去动原有的代码:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 第三方登陆接口,直接继承登陆接口
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:48 2020-3-14
 */
public abstract class ThirdLoginServiceDecorator implements LoginService {

    //qq登陆留给子类实现
    protected MsgResult loginQQ(String id){
        return null;
    }

    //微信登陆留给子类实现
    protected MsgResult loginWechat(String id){
        return null;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 实现原有的基础套餐
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:50 2020-3-14
 */
public class ThirdLoginServiceImpl extends ThirdLoginServiceDecorator {

    private LoginService loginService;

    public ThirdLoginServiceImpl(LoginService loginService){
        this.loginService = loginService;
    }

    @Override
    public MsgResult login(String username, String password) {
        return loginService.login(username,password);
    }

    @Override
    public MsgResult register(String username, String password) {
        return loginService.register(username,password);
    }
}


/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  qq登陆装饰器
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 16:57 2020-3-14
 */
public class ThirdLoginQQDecorator extends ThirdLoginServiceDecorator {

    private ThirdLoginServiceDecorator thirdLoginServiceDecorator;

    public ThirdLoginQQDecorator(ThirdLoginServiceDecorator thirdLoginServiceDecorator){
        this.thirdLoginServiceDecorator = thirdLoginServiceDecorator;
    }

    @Override
    public MsgResult loginQQ(String id){
        System.out.println("qq登陆");
        return null;
    }

    @Override
    public MsgResult login(String username, String password) {
        return thirdLoginServiceDecorator.login(username,password);
    }

    @Override
    public MsgResult register(String username, String password) {
        return thirdLoginServiceDecorator.register(username,password);
    }
}

 测试一下:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : 1029149772@qq.com
 * @Date : Created in 17:00 2020-3-14
 */
public class updateLoginTest {
    public static void main(String[] args) {

        ThirdLoginServiceDecorator thirdLoginService = new ThirdLoginServiceImpl(new LoginServiceImpl());
        thirdLoginService = new ThirdLoginQQDecorator(thirdLoginService);
        thirdLoginService.loginQQ("4444");
        thirdLoginService.login("xiaoming","123456");

    }
}

感觉好像适配器模式是不是啊,其实代码上看起来像,但是重要的是两种模式的思想,我们这里来做一下区别。

我们来看看jdk中运用最经典的装饰者模式,io,发现了没有io流中就是new一级一级来传递的(io里面有个很经典的总结,大桶套小桶)。

 

 

把上面的抽象类注入子类,拿到一些父类的东西,完全符合大桶套小桶。

总结:

优点:

1.装饰者是继承的有利的补充,比继承灵活,不改变原有对象的情况下动态的给一个对象扩展功能,即插即用。

2.通过使用不同装饰类以及这些装饰类的排列组合可以实现不同的效果。

3.装饰者完全遵守开闭原则。

缺点:

1.增加更多的类,增加程序的复杂性。

2.动态装饰时,多层装饰时会更复杂。

 

MVC设计模式实现登陆程序 一: MVC简介: MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。 MVC模式的目的:实现一种动态的程序设计,使后续对程序的修改和扩展简化,并且使程序某一部分的重复利用成为可能。除此之外,此模式通过对复杂度的简化,使程序结构更加直观。软件系统通过对自身基本 阅读详情

相关推荐

订水商城H5实战教程-04用户注册

我们上一篇讲解了用户协议的功能,如果用户同意协议,就可以跳转到注册页面,要求用户录入个人基本信息,本篇我们介绍一下用户注册功能。

低代码布道师的博客 1073

设计模式--装饰模式示例代码

装饰模式: 装饰模式又叫包装模式,通过—种对客户端透明的方式来扩展对象功能,是继承关系的一种替代装饰模式就是把要附加的功能分别放在单独的类中,并让这个类包含它要装饰的对象,当需要执行时,客户端就可以有选择的、按顺序的使用装饰功能包装对象。 一般情况下用继承实现类的功能拓展 装饰模式可以动态给一个类增加功能 在装饰模式中的各个角色有: 抽象构件(Component)角色:给出一...

lzzw的学习之路 500

设计模式以及用户登陆注册页面

设计模式的简单介绍 有助于大家了解java设计模式 。

装饰者模式

装饰者模式,适用于扩展类或添加新方法。是适配器模式的一种特殊的例子。 装饰者模式是is-a  (1个usb接口扩展成4个usb接口,所实现的接口都是一样的) 适配器模式是has-a (usb接口转HTML接口,转VGM接口,所以适配器模式重点在于解决兼容问题) public interface ISigninService { public ResultMsg regist(Str...

Ving--造轮子小作坊 196

告别Visual Studio!用MSYS2+GCC+CMake在Windows上打造Linux式C++开发环境(保姆级避坑)

本文详细介绍了如何在Windows上使用MSYS2+GCC+CMake搭建Linux式C++开发环境,替代Visual Studio。从MSYS2的安装配置、MinGW-w64 GCC工具链的部署,到CMake项目的创建与构建,提供了保姆级教程和常见问题解决方案,帮助开发者高效过渡到命令行开发模式。

weixin_30298497的博客 334

如何免费访问和使用Gemini API?

Gemini Ultra具有最先进的性能,在几个指标上超过了GPT-4的性能。它是第一个在大规模多任务语言理解基准测试中超越人类专家的模型,该基准测试57个不同学科的世界知识和解决问题的能力。Gemini是谷歌迄今为止开发的最先进、最庞大的AI模型。它为多模态而建,理解并处理不同类型的数据,比如文本、代码、音频、图像和视频。下面我们将加载Masood Aslami的图像,并用它来测试Gemini Pro Vision的多模态性。有了Gemini可以为查询提供图像、音频和文本,获得几乎完美的答案。

java_cjkl的博客 1万+

设计模式---装饰器(Decorator)模式

1 定义 装饰器(Decorator)模式,也称为包装(Wrapper)模式,是继承关系的一种替代方案。指在不改变对象结构的情况下,动态的给对象添加一些功能。 2 优缺点 2.1 优点 装饰器是继承的有力补充,比继承灵活,在不改变原有对象的情况下,动态的给一个对象扩展功能,即插即用 通过使用不用装饰类及这些装饰类的排列组合,可以实现不同效果 装饰器模式完全遵守开闭原则 2.2 缺点 装饰器模式会增加许多子类,过度使用会增加程序得复杂性。 3 装饰器模式的结构与实现 装饰器模式的类图如下所示:

honor_zhang的博客 5285

携程景点评论数据爬取实战:破解AJAX动态加载与分页参数逆向分析

本文详细介绍了携程景点评论数据爬取的实战经验,重点破解了AJAX动态加载与分页参数逆向分析的技术难题。通过分析`getCommentCollapseList`接口,解析动态参数如`poiId`和`pageIndex`,并实现分页逻辑,帮助开发者高效获取评论数据。文章还提供了完整的Python爬虫代码和反爬策略,适用于数据分析与内容挖掘。

weixin_29098367的博客 679

Java设计模式之装饰者模式

Java设计模式之装饰者模式目录一、定义二、具体实现三、使用场景四、优缺点 目录 装饰者模式是结构性设计模式之一,其在不必改变类文件及不使用继承的情况下,动态地扩展一个对象的功能。它通过创建一个包装对象(即装饰)来包裹真实的对象。 一、定义 动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。 装饰着模式的结构图: 如上结构图所示,装饰者模式中有如下角色: Component:可以是接口或者是抽象类,被装饰的最原始的对象。 ConcreteComponent:组件具体实现类。

红日 6564

基于OpenCV的驾驶疲劳检测与预警系统设计

PERCLOS(Percentage of Eye Closure)是一种基于眼睑闭合程度的疲劳度量指标。在基于OpenCV的驾驶疲劳检测与预警系统设计中,PERCLOS可以用于检测驾驶员的眼睑闭合程度,以判断其是否疲劳。该函数用于检测视频流中人脸的疲劳程度。具体地,该函数通过检测人脸的眼睛和嘴巴的状态来评估人脸的疲劳程度。眼睛状态是通过计算眼睛的长宽比(eyear)来判断,如果长宽比小于某个阈值,则认为眼睛闭合,表示人处于疲劳状态。

2301_79810943的博客 2331

STM32 HAL SDADC DMA

由于项目需要使用STM32F373单片机的SDADC功能对电位计进行检测,网上资料比较少,踩了很多坑,下面进行总结。

weixin_44597885的博客 1204

设计模式——装饰器模式

指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。

CDConlyred的博客 410

i.MX6ULL驱动开发 | 31 - Linux内核网络设备驱动框架

net_device结构体在内核中指代一个网络设备,定义在,是一个非常大的结构体,包含网络设备的属性描述和操作接口,其中一些关键的成员如下。Linux在轮询处理机制和中断处理机制的基础上,提出了一种高效的网络处理技术NAPI。NAPI的核心思想是不全部采用中断来读取网络数据,而是采用中断来唤醒数据接收服务程序,在接收服务程序中采用poll的方法来轮询处理数据。提高短数据包的接收效率,减少中断处理的时间。...

Mculover666的博客(嵌入式) 1107

java中裝飾設計模式_java中设计模式实例分享

java中设计模式(装饰设计模式)的实例详解java 中设计模式(装饰设计模式)的实例详解应用场景:在不对原有对象类进行修改的基础上,给一个或多个已有的类对象提供增强额外的功能.我觉得可以从字面理解,装饰,装饰房子。房子可以看成原有的类。等于你把一个已经建好的房子按照自己的想法再装饰一遍。继承也可以实现这样的功能,但是继承有它的缺点,继承只是单一继承。装饰设计模式可以取多个不同的类的不同功能。具体...

weixin_30467625的博客 119

OpenOCD入门到精通:第 4 章 配置文件体系详解

本文深入解析OpenOCD配置文件的体系结构和工作机制。首先介绍了配置文件的搜索路径优先级和加载顺序,以及配置阶段与运行阶段的区别。然后详细分析了interface配置文件的适配器参数调优方法,包括ST-Link、CMSIS-DAP和FTDI等常见调试器的配置示例。最后重点讲解了target配置文件的核心结构,包括TAP声明、目标创建和工作区设置等关键步骤,并以stm32f1x为例展示了完整的配置流程。通过本文可以全面了解OpenOCD配置文件的分层设计理念和参数化配置方法。

从事嵌入式linux android驱动开发,拥有全栈能力 607

Python航空航天设计神器!从入门到实操,小白也能搞定飞机优化

AeroSandbox的核心价值的是:​降低航空工程优化的门槛,用Python实现高效、简洁的工程设计​。无论是科研、学习还是实际项目,它都能帮你节省大量时间,不用再手动推导导数、编写复杂的优化逻辑。对于新手来说,建议先从案例1(Rosenbrock函数)入手,熟悉优化流程,再尝试案例2(翼型优化),逐步掌握核心用法;对于有航空相关需求的小伙伴,可以深入研究内置的物理模型,结合自己的项目进行定制化开发。最后,互动时间到!👉 你是用AeroSandbox做科研、练手还是实际项目?

源代码杀手的博客 142

java设计模式————适配器模式,怎么加上第三方登陆接口

适配器模式(Adapter Pattern) 指将一个类的接口转换成客户期望的另一个接口,使原本的接口不兼容的类可以一起工作。 属于结构型设计模式。 适用场景: 1.已经存在的类, ...

qq_40111437的博客 612

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

java 设计模式 装饰模式 对目标类的功能进行增强

装饰模式 有名包装模式 * 1.装饰模式以对客户端的方式扩展对象的功能,是继承关系的一个替代方案 * 2..装饰模式已对客户端的方式动态的给一个对象附加上更多的责任 * 3.装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。 * 4.装饰模式把客户端的调用委派到被装饰类。装饰模式关键在于这种扩展是完全透明的 * 5.装饰模式是在不必改变原有文件和使用继承的情况下,动态

lm324114的专栏 584

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

常见的几种设计模式

文章目录单例模式为什么会有单例设计模式?应用spring中IOC解决的问题设计思想工厂模式简单工厂模式工厂方法模式观察者模式观察者模式的定义装饰模式模板方法定义:主要的作用:优点:缺点:应用场景:适配器模式(Adapter Pattern)定义类适配器对象适配器优点类适配器优缺点对象适配器优缺点适用环境职责链模式分析实例审批假条职责链模式的优点职责链模式的缺点纯与不纯的职责链模式代理模式动机优点缺...

清风博客 6666

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

微信授权登陆接入第三方App(步骤总结)Android。

这几天开发要用到微信授权的功能,所以就研究了一下。可是微信开放平台接入指南里有几个地方写的不清不楚。在此总结一下,以便需要的人。 很多微信公众平台的应用如果移植到app上的话就需要微信授权登陆了。       目前移动应用上微信登录只提供原生的登录方式,需要用户安装微信客户端才能配合使用。也就是如果第三方应用需要微信授权登陆的话就必须在本机上安装了微信。而后续授权登陆或调用接口之类的相

22万+

Java——Java基础之常用设计模式

文章目录1. 工厂模式2. 策略模式3. 状态模式4. 装饰器模式5. 单例模式6. 观察者模式7. 代理模式 1. 工厂模式 缺点:扩展性差。产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码 三大工厂模式的代码实现及总结 2. 策略模式 优点: 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。 缺点: 1、策略类会增多。 2、所有策略类都需要对外暴露 使用场景:1、如果在一个系统里面有许多类,它们之间的区别仅在

weixin_38938338的博客 518

常用的设计模式

从我们平常的开发中不难发现,如果一个类或者方法接口等等只做一件事,那么可读性很高,并且复用性也很高,并且一旦需求变化,也容易维护,假如你一个类糅杂多个职责,那么很难维护。普通的共享变量不能保证可见性,因为普通共享变量被修改之后,什么时候被写入主存是不确定的,当其他线程去读取时,此时内存中可能还是原来的旧值,因此无法保证可见性。5、接口隔离:使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口,类间的依赖关系应该建立在最小的接口上。也就是说:是用工厂类的方法来代替 new 操作的。

qq_56959223的博客 1036

重学 Java 设计模式:实战装饰器模式(SSO单点登录功能扩展,增加拦截用户访问方法范围场景)

作者:小傅哥 博客:https://bugstack.cn 沉淀、分享、成长,让自己和他人都能有所收获!???? 一、前言 对于代码你有编程感觉吗 很多人写代码往往是没有编程感觉的,也就是除了可以把功能按照固定的流程编写出流水式的代码外,很难去思考整套功能服务的扩展性和可维护性。尤其是在一些较大型的功能搭建上,比较缺失一些驾驭能力,从而导致最终的代码相对来说不能做到尽善尽美。 江洋大盗与江洋大偷 两个本想描述一样的意思的词,只因一字只差就让人觉得一个是好牛,一个好搞笑。往往我们去开发编程写代码时也经常将

bugstack虫洞栈 2259

微信小程序登陆(两种写法)

1.第一种写法, 不借助第三方工具,直接上代码 1.1Controller代码: @ApiOperation("微信登录授权") @PostMapping("/login") @ApiImplicitParams({ public void WxLogin(@RequestBody WeiXinLoginRequestVO weiXinLoginRequestVO) { userService.decodeUserInfo(weiXinLoginRequestVO); } 1.1.1

小林子的博客 3565

Java装饰者模式介绍+实际应用场景的案例 -- 柚子真好吃

Java装饰者模式介绍+实际应用场景的案例 -- 柚子真好吃一、装饰者模式简单介绍二、业务介绍三、代码实现 一、装饰者模式简单介绍 穿衣服,蛋糕上加草莓,电脑安音响耳机,小卡片上画条哥。 简单来讲就是为某一个东西,装饰一些漂亮的饰品,增强一些事务。 为一个现有的类,在不改变原代码的情况下,修饰或添加新功能。 也可以将装饰者模式理解为功能模块的分割,类似于分布式理解。 二、业务介绍 有一个用户名和密码登陆的功能,现在记录登陆的日志,同时添加一个微信登陆功能。 如果不用装饰者模式也是可以完成的,我们只需重

java 1343

Java设计模式大作业——访问者模式

在校园中,对于一个学院的师生,辅导员更为关心同学们的成绩以及基本个人信息,学业主观努力度、系主任则关注教师的满意度,学生的学分修读情况,学校则关注该学院的师资力量与学生整体学习情况,企业关注学生的修读课程水平以及教资力量。访问者分别实现对不同角色的访问方法。方法,对于访问学生,返回学生学号、姓名、性别、年龄、部门、所有课程成绩,还有平均成绩、任课教师的基本个人信息包括联系方式、姓名、所属部门。灵活性: 不同的访问者可以根据自己的需要定义自己的操作,而无需修改被访问的对象(学院、学生、教师等)。

记录些个人学习过程的代码实现以及解决方案 2155
上一篇: java设计模式————适配器模式,怎么加上第三方登陆接口
下一篇: java设计模式————观察者模式,手写一个监听器
小小攻城狮Jack
博客等级 码龄9年 62粉丝 42原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值