一道关于高朋网(Groupon)的设计模式题(翻译)

本文通过Groupon的商业模式,介绍了如何运用观察者模式来实现团购应用。具体包括类图设计、代码实现等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人原博地址:http://blog.saymagic.cn/blog.php?id=18

看到了伟帅老师在优快云博客里面分享的国外某所大学的一道设计模式题,于是决定将其翻译过来并分享一下自己的理解。

原博链接

Groupon has already been called the“fastest growing company in history” by the Forbes magazine. In it’s secondfull year of business, 2010, it pulleddown $713.4 million in revenue(收入). The revenue a year earlier was only $30.47 million. That’s agrowth rate of 2,241%. Yes you read it correctly. Now let us see what is howtheir business model is working:

高朋网被福布斯杂志称作“历史上发展最快速的公司”,2010年,也就是在它业务的第二个整年,它拿下了713400000美元收入这个收入较上年同期仅为30470000美元。这是2.241%的增长率。如果你已经正确的读懂了它,那现在就让我们来看一看他们的商业模式是怎样工作的。

When you check the Groupon website you willsee webpage like the one shown below:

当你进入高朋网站的时候你会看到下面出现的网页。

The webpage shows deeply discounted offersfrom different business located in the city where you are living. The figure shows a beauty salon offer thathas a 50% discount on women’s haircut. You do nothing if you are notinterested. If you like the Groupon deal, you select “buy” before theexpiration date and your credit card is charged. If enough people select thedeal, you will be sent a link for your rebate coupon at the end of the offer.Once you receive your coupon you can claim it at the specified store, usuallywithin a period of time before it expires as noted on the website. If enoughpeople do not join for that deal, the Groupon is cancelled and no one gets itand in this case you will get a refund for your money. You can see in the leftdown corner of the figure that the deal is on because more than 15 people hasalready joined the deal. In case that you have changed your mind you can alsocancel an already made order and you will get a full refund. Clever, isn’t it?

该网页根据您居住的城市来显示来自不同的业务丰厚的折扣优惠,该图显示了一个美发沙龙对妇女的发型提供了50%的折扣。如果你不感兴趣你讲什么都不需要做,如果你对高朋网的折扣感兴趣,如果在截至日期之前,并且你可以用信用卡来支付,你可以选择购买,如果足够多的人选择了这个折扣,您将在要约结束的时候收到关于优惠券的链接,一旦你收到了你的优惠券你可以在指定的商店得到它,通常是在到期之前的一段时间网站上公示的商店。如果没有足够的人加入到这个优惠,这个临时的组就会停止,没有人能够得到优惠券并且在这种情况下你讲得到你的退款,你在这个图的左下角可以看到可以进行这个交易因为超过15个人已经加入到这个折扣,万一你改变了主意你也可以停止一个已经成功预定的订单并且得到全额退款,聪明的你,看明白了吗?

Which design pattern you would use toimplement the Groupon application? Please give a short explanation why. In particular, show an appropriate classdiagram(s) and/or enough code fragments to illustrate your use of the patternto solve the problem.

你将使用哪种设计模式来实现高朋应用呢?请给出一个简短的解释说明,另外,画出合适的类图和给出足够多的代码片段来说明你用解决这个问题的设计模式。


好的,翻译完了,接下来我来讲一下我的解题思路,很明显,这是观察者模式的典型代表,每种团购作为观察者模式里的ConcretSubject,而每个用户则作为具体的Observer.

绘制类图如下:




代码如下:

抽象主题类:

num代表需要满足num人时才可以 每当增加或者删除顾客的时候团购,DealSubject会去判断当前list里面的人数是否大于num,若大于则向所有人发消息,说明可以团购了。

import java.util.*;


public class DealSubject {
   private    List<CustomerObserver> list = new ArrayList<CustomerObserver>();
   private boolean isOK = false;
   private int num =4;


	public List getList() {
		return list;
	}
	
	public void setList(List list) {
		this.list = list;
	}
	
	public void attach(CustomerObserver observer){
		list.add(observer);
		if(list.size()>=num)
			isOK=true;
	}
	
	 public void detach(CustomerObserver observer){


	        list.remove(observer);
			 if(list.size()<num)
					isOK=false;
	    }
	    /**
	     * 通知所有注册的观察者对象
	     */
	public void nodifyObservers(){
	        
	        for(CustomerObserver observer : list){
	        	if(isOK)
	        		observer.getMsg("团购成功");
	        	else
	        		observer.getMsg("人数不够,团购失败,退款给您");
	        }
	 }


}

具体主题类:

import java.util.*;

public class TuangouConcrete extends DealSubject {

   public TuangouConcrete(){
   }
	
  

}


抽象顾客类:

import java.util.*;

public interface CustomerObserver {
   void getMsg(String msg);


}

具体顾客类:

具体顾客类可以有很多,他们都继承自抽象顾客类,这里只列了一个。

import java.util.*;

public class Bing implements CustomerObserver {
	@Override
	public void getMsg(String num) {
		System.out.println(num);
		
	}

}

客户端代码:

public class Client {

	public static void main(String[] args) {
		DealSubject subject = new TuangouConcrete();
		CustomerObserver jia = new Jia();
		CustomerObserver ding = new Ding();
		CustomerObserver bing = new Bing();
		CustomerObserver yi = new Yi();
		subject.attach(jia);
		subject.attach(ding);
		subject.attach(bing);
		subject.attach(yi);
		subject.nodifyObservers();
		//两人退出以后
		subject.detach(yi);
		subject.detach(ding);
		subject.nodifyObservers();



	}

}

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值