java网页统计访客量

Visitor Design Pattern is one of the behavioral design pattern.

访客设计模式是行为设计​​模式之一。

访客设计模式 (Visitor Design Pattern)

Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.

当我们必须对一组相似类型的对象执行操作时,将使用访问者模式。 借助访问者模式,我们可以将操作逻辑从对象移动到另一个类。

For example, think of a Shopping cart where we can add different type of items (Elements). When we click on checkout button, it calculates the total amount to be paid. Now we can have the calculation logic in item classes or we can move out this logic to another class using visitor pattern. Let’s implement this in our example of visitor pattern.

例如,考虑一个购物车,我们可以在其中添加不同类型的项目(元素)。 当我们点击结帐按钮时,它将计算要支付的总金额。 现在我们可以将计算逻辑包含在项目类中,或者可以使用访问者模式将此逻辑移到另一个类中。 让我们在访问者模式示例中实现此功能。

访客设计模式Java示例 (Visitor Design Pattern Java Example)

To implement visitor pattern, first of all we will create different type of items (Elements) to be used in shopping cart.

为了实现访客模式,首先我们将创建要在购物车中使用的不同类型的项目(元素)。

ItemElement.java

ItemElement.java

package com.journaldev.design.visitor;public interface ItemElement {public int accept(ShoppingCartVisitor visitor);
}

Notice that accept method takes Visitor argument. We can have some other methods also specific for items but for simplicity I am not going into that much detail and focusing on visitor pattern only.

请注意,accept方法采用Visitor参数。 我们可以有一些其他特定于项目的方法,但为简单起见,我将不讨论太多细节,而仅关注访问者模式。

Let’s create some concrete classes for different types of items.

让我们为不同类型的项目创建一些具体的类。

Book.java

Book.java

package com.journaldev.design.visitor;public class Book implements ItemElement {private int price;private String isbnNumber;public Book(int cost, String isbn){this.price=cost;this.isbnNumber=isbn;}public int getPrice() {return price;}public String getIsbnNumber() {return isbnNumber;}@Overridepublic int accept(ShoppingCartVisitor visitor) {return visitor.visit(this);}}

Fruit.java

Fruit.java

package com.journaldev.design.visitor;public class Fruit implements ItemElement {private int pricePerKg;private int weight;private String name;public Fruit(int priceKg, int wt, String nm){this.pricePerKg=priceKg;this.weight=wt;this.name = nm;}public int getPricePerKg() {return pricePerKg;}public int getWeight() {return weight;}public String getName(){return this.name;}@Overridepublic int accept(ShoppingCartVisitor visitor) {return visitor.visit(this);}}

Notice the implementation of accept() method in concrete classes, its calling visit() method of Visitor and passing itself as argument.

注意具体类中accept()方法的实现,它调用Visitor的visit()方法并将其自身作为参数传递。

We have visit() method for different type of items in Visitor interface that will be implemented by concrete visitor class.

我们在Visitor界面中具有针对不同项目类型的visit()方法,将由具体的visitor类实现。

ShoppingCartVisitor.java

ShoppingCartVisitor.java

package com.journaldev.design.visitor;public interface ShoppingCartVisitor {int visit(Book book);int visit(Fruit fruit);
}

Now we will implement visitor interface and every item will have it’s own logic to calculate the cost.

现在,我们将实现访客界面,并且每个项目都有其自己的逻辑来计算费用。

ShoppingCartVisitorImpl.java

ShoppingCartVisitorImpl.java

package com.journaldev.design.visitor;public class ShoppingCartVisitorImpl implements ShoppingCartVisitor {@Overridepublic int visit(Book book) {int cost=0;//apply 5$ discount if book price is greater than 50if(book.getPrice() > 50){cost = book.getPrice()-5;}else cost = book.getPrice();System.out.println("Book ISBN::"+book.getIsbnNumber() + " cost ="+cost);return cost;}@Overridepublic int visit(Fruit fruit) {int cost = fruit.getPricePerKg()*fruit.getWeight();System.out.println(fruit.getName() + " cost = "+cost);return cost;}}

Lets see how we can use visitor pattern example in client applications.

让我们看看如何在客户端应用程序中使用访问者模式示例。

ShoppingCartClient.java

ShoppingCartClient.java

package com.journaldev.design.visitor;public class ShoppingCartClient {public static void main(String[] args) {ItemElement[] items = new ItemElement[]{new Book(20, "1234"),new Book(100, "5678"),new Fruit(10, 2, "Banana"), new Fruit(5, 5, "Apple")};int total = calculatePrice(items);System.out.println("Total Cost = "+total);}private static int calculatePrice(ItemElement[] items) {ShoppingCartVisitor visitor = new ShoppingCartVisitorImpl();int sum=0;for(ItemElement item : items){sum = sum + item.accept(visitor);}return sum;}}

When we run above visitor pattern client program, we get following output.

当我们在访客模式客户端程序之上运行时,我们得到以下输出。

Book ISBN::1234 cost =20
Book ISBN::5678 cost =95
Banana cost = 20
Apple cost = 25
Total Cost = 160

Notice that implementation if accept() method in all the items are same but it can be different, for example there can be logic to check if item is free then don’t call the visit() method at all.

请注意,如果所有项目中的accept()方法的实现都是相同的,但是可以不同,例如,可以使用逻辑检查项目是否为空,那么根本不要调用visit()方法。

访客设计模式类图 (Visitor Design Pattern Class Diagram)

Class diagram for our visitor design pattern implementation is:

我们的访客设计模式实现的类图是:

访客模式的好处 (Visitor Pattern Benefits)

The benefit of this pattern is that if the logic of operation changes, then we need to make change only in the visitor implementation rather than doing it in all the item classes.

这种模式的好处是,如果操作逻辑发生变化,那么我们仅需要在访问者实现中进行更改,而无需在所有项目类中进行更改。

Another benefit is that adding a new item to the system is easy, it will require change only in visitor interface and implementation and existing item classes will not be affected.

另一个好处是,将新项目添加到系统很容易,只需要在访问者界面和实现中进行更改,现有项目类就不会受到影响。

访客模式限制 (Visitor Pattern Limitations)

The drawback of visitor pattern is that we should know the return type of visit() methods at the time of designing otherwise we will have to change the interface and all of its implementations. Another drawback is that if there are too many implementations of visitor interface, it makes it hard to extend.

访客模式的缺点在于,在设计时我们应该知道visit()方法的返回类型,否则我们将不得不更改接口及其所有实现。 另一个缺点是,如果访问者接口的实现过多,则很难扩展。

Thats all for visitor design pattern, let me know if I have missed anything. Please share it with others also if you liked it.

这就是访客设计模式的全部内容,如果我错过了任何内容,请告诉我。 如果喜欢,也请与他人分享。

翻译自: https://www.journaldev.com/1769/visitor-design-pattern-java

java网页统计访客量

java网页统计访客量_Java中的访客设计模式相关推荐

  1. java工厂模式和抽象工厂_Java中的抽象工厂设计模式

    java工厂模式和抽象工厂 Welcome to Abstract Factory Design Pattern in java example. Abstract Factory design pa ...

  2. java如何统计文章阅读量_博客中的阅读量是如何设计的?

    在博客园中,一篇博客的底部,通常有该篇博客的阅读量的统计.当浏览器端没发起一个请求的时候,它通过相应的逻辑判断,如果符合要求,则给阅读量加一.所以,有了如下代码: package test; impo ...

  3. java开发个人博客过程_java web个人博客开发(四详细设计)

    1.序言 详细设计主要内容在这里是接口设计,即html页面请求到,java后台返回数据的接口.预期实用restful风格,所以接口形式是url+请求参数,数据格式为json.由于文章统计阅读量和博主的 ...

  4. java 什么时候进行垃圾回收_Java中垃圾回收有什么目的?什么时候进行垃圾回收?...

    仅提供一个大致的思路: 垃圾回收(gc)的目的是释放堆中不需要保存的对象,达到内存的充分利用. 1.回收哪些对象的判定 垃圾回收最简单的思路是采用引用计数的方式,即记录对象被引用的次数,直到一段时间内 ...

  5. java 个人博客开发_Java实现个人博客系统

    导读:进入二十一世纪,以Internet为核心的现代网络积水和通信技术已经得到了飞速的发展和广泛的应用,各种网络交流互动工具也应运而生.其中以论坛.博客.社区.空间最为受广大网民朋友的欢迎,也是目前为 ...

  6. java什么是栈和堆_JAVA中的栈和堆

    JAVA在程序运行时,在内存中划分5片空间进行数据的存储.分别是:1:寄存器.2:本地方法区.3:方法区.4:栈.5:堆. 基本,栈stack和堆heap这两个概念很重要,不了解清楚,后面就不用学了. ...

  7. java被3整除的数_java中计算1-200以内可以被3整除的数,输出这些数并统计一共有多少个....

    public class c {public static void main(String[] args) {for(int i=10;i<100;i++){if(i%7==0&&am ...

  8. java byte 字面量_Java中的字面量

    在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(natation).几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串:而有很多也对布尔类型和 ...

  9. java sdi接口是什么意思_JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分

    首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...

最新文章

  1. 【springboot】配置
  2. Vue2.0中引入element-ui
  3. oracle中查看用户权限
  4. 限制mysql服务为本地访问
  5. P3531 [POI2012]LIT-Letters(求逆序对)
  6. boost::function_types::components用法的测试程序
  7. sublime自定义主题-修改行号的颜色
  8. 基于链表的两个集合的交集(C++)
  9. [C++] 构造函数 which is of non-class type
  10. Hao Yin Jian 寒假第一周
  11. android自定义布局中的平滑移动
  12. (传播智客)刘意Java基础班精华版
  13. 日常笔记-snownlp情感分析计算情感得分
  14. spring boot生成Excel表格 导出/导入
  15. js常用插件(八)之移动端滑动插件swiper,BScroll
  16. 中芯国际进军PRAM存储,蚕食三星40nm产能
  17. 如何在计算机管理设置开机密码,电脑怎么设置开机密码 开机密码设置步骤
  18. NVIDIA GPU Compute Capability
  19. 英文名称:DSPE-PEG10-Mal的试剂分子式是C68H127N2O21P
  20. 矩阵相乘(Python)

热门文章

  1. C++ Pitfalls 之 reference to an object in a dynamically allocated containter
  2. 转:LoadRunner检查点使用小结
  3. [转载] 【Python】range()、np.arange()、np.linspace()、np.logspace()的使用和区别
  4. Broadwell I7-5775c/5675c BSOD 蓝屏问题
  5. JSPs only permit GET POST or HEAD的解决方案(REST风格)
  6. Python中sort和sorted函数代码解析
  7. IDEA: 遇到问题Error during artifact deployment. See server log for details解决方法
  8. 【NOI2014】魔法森林
  9. lua循环,减少不必要的循环
  10. springmvc跳转到自定义404页面的三种方法