Java8设计模式最佳实战-设计模式概述(第七天学习记录)

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

在这里插入图片描述

In the preceding diagram, we have FrontController, AbstractCommand, Command1, and

在前面的图表中,我们有FrontController、AbstractCommand、Command1和

Command2. FrontController receives all requests, treats some common points of the

命令2。FrontController接收所有请求,处理

request, and sends this request to the matching command. AbstractCommand is the

请求,并将此请求发送到匹配的命令。AbstractCommand是

abstract class of command. Command1 and Command2 are the subclasses of command,

命令的抽象类。Command1和Command2是command的子类,

which implement its correspondent logic.

实现了相应的逻辑。

In our case, we will have two pages—a homepage and a login page. If the user is logged in

在我们的例子中,我们将有两个页面-一个主页和一个登录页面。如果用户已登录

at the moment that a request is sent, then the application will launch the login page, and

在发送请求时,应用程序将启动登录页面,并且

then the homepage.

然后是主页。

Implementing FrontController

Here, we have an implementation of MyAppController, which is a FrontController to treat all the requests of an application:

package com.gary.book.chapter01;

import com.rhuan.action.Command.AbstractCommand;

import com.rhuan.action.Command.HomeCommand;

import com.rhuan.action.Command.LoginCommand;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = “MyAppController”, urlPatterns = “/myapp/*”)

public class MyAppController extends HttpServlet {

private static Logger logger =

LogManager.getLogger(MyAppController.class);

private final String PAGE_ERROR = “/pageError.jsp”;

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

protected void processRequest(HttpServletRequest

request, HttpServletResponse response)

throws ServletException, java.io.IOException {

String resultPage;

AbstractCommand command = null;

try {

//Create a correspondent Command.

if (request.getSession().getAttribute(“USER”) == null)

command = new LoginCommand();

else command = new HomeCommand();

//Execute the Command that return a page.

resultPage = command.execute();

} catch (Exception e) {

logger.error(e.getMessage());

resultPage = PAGE_ERROR;

}

//Dispatch to correspondent page.

getServletContext().getRequestDispatcher(resultPage)

.forward(request, response);

}

}

In the following code snippet, it is very important to note that urlPattern is used to

在下面的代码片段中,请注意urlPattern用于

define which requests a context will send to our controller. Here’s how we do this:

定义上下文将发送给控制器的请求。我们的方法如下:

//Defining the urlPattern to Front Controller

@WebServlet(name = “MyAppController”, urlPatterns = “/myapp/*”)

public class MyAppController extends HttpServlet {

}

On the urlPattern, the value is “/myapp/_". As previously shown in the preceding code

在urlPattern上,值为“/myapp/_”。如前面的代码所示

snippet, this URL pattern (”/myapp/_") establishes that all requests to the myapp URI are

这个URL模式(“/myapp/_”)确定对myapp URI的所有请求都是

sent to our controller. For example, http://ip:port/context/myapp/myfuncionality

发送给我们的控制器。例如,http://ip:端口/context/myapp/myfunctionality

is sent to our controller.

发送到我们的控制器。

When we implement this pattern, it is very important to pay attention to the use of

当我们实现这个模式时,注意使用

attributes on servlets, because all the class attributes on a servlet are shared with all threads

servlet上的属性,因为servlet上的所有类属性都与所有线程共享

or all requests.

或所有请求。

最后

针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。

下面的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题(都整理成文档,小部分截图)

在这里插入图片描述

最新整理电子书

在这里插入图片描述

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-wm6WSfPQ-1713610895315)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。在此模式中,我们使用一个工厂类来创建不同类型的对象,而不是在代码中直接实例化对象。 对于你提到的三角形和圆形的创建和擦除,我们可以使用简单工厂模式来实现。首先,我们需要定义一个形状接口,它包含画图和擦除方法。然后,我们可以创建两个实现这个接口的类,分别是三角形和圆形类。接下来,我们创建一个工厂类,它包含一个方法,该方法接受一个字符串参数,根据参数的不同返回三角形或圆形对象。 以下是一个简单的示例代码,用于演示如何使用简单工厂模式来创建和擦除三角形和圆形: ```java interface Shape { void draw(); void erase(); } class Triangle implements Shape { @Override public void draw() { System.out.println("Draw a triangle."); } @Override public void erase() { System.out.println("Erase a triangle."); } } class Circle implements Shape { @Override public void draw() { System.out.println("Draw a circle."); } @Override public void erase() { System.out.println("Erase a circle."); } } class ShapeFactory { public static Shape createShape(String type) { if (type.equals("triangle")) { return new Triangle(); } else if (type.equals("circle")) { return new Circle(); } else { throw new IllegalArgumentException("Invalid shape type."); } } } public class Main { public static void main(String[] args) { Shape triangle = ShapeFactory.createShape("triangle"); triangle.draw(); triangle.erase(); Shape circle = ShapeFactory.createShape("circle"); circle.draw(); circle.erase(); } } ``` 在上面的代码中,我们首先定义了一个Shape接口,并创建了Triangle和Circle类来实现它。接下来,我们创建了一个ShapeFactory工厂类,它包含一个createShape方法,该方法根据传入的参数返回不同的形状对象。最后,我们在主方法中使用ShapeFactory来创建和擦除三角形和圆形。 这是一个简单的示例,演示了如何使用简单工厂模式来创建和擦除不同类型的形状。在实际应用中,我们可以使用工厂模式来创建和管理更复杂的对象,可以提高代码的可维护性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值