java网页统计访客量_Java中的访客设计模式

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;
	}

	@Override
	public 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;
	}
	
	@Override
	public 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 {

	@Override
	public int visit(Book book) {
		int cost=0;
		//apply 5$ discount if book price is greater than 50
		if(book.getPrice() > 50){
			cost = book.getPrice()-5;
		}else cost = book.getPrice();
		System.out.println("Book ISBN::"+book.getIsbnNumber() + " cost ="+cost);
		return cost;
	}

	@Override
	public 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网页统计访客量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值