使用哪种模式

发布于 2024-10-18 21:11:16 字数 377 浏览 1 评论 0原文

我有一个课程——称之为“谢谢你”,它提供了这些内容。根据不同的实现(通用的或 Facebook 的),我需要提供定制的布局。现在,我正在 JS 中构建 HTML 并交付布局。

1) 注册电子邮件通讯(适用于通用和 Facebook 实施) 2)预告片内容(用于一般实施) 3) Facebook like(用于 FacebookThankYou 实现)

您认为使用哪种设计模式更好地实现 - Factory 还是 Mediator?我刚刚开始在我的代码中使用一些设计模式,并希望以正确的方式开始。

一些注意事项: a) 虽然功能可能相同,但通用和 Facebook 的布局可能不同

如果我不使用设计模式,我可以使用“if”语句轻松完成此操作,但我只是在寻找更优雅的解决方案。

I have a class - call it ThankYou that delivers these. Based on the different implementations, generic or Facebook, I need to deliver customized layout. Right now, I am building the HTML in JS and delivering the layout.

1) Sign up for email newsletters (for generic and Facebook implementation)
2) Teaser content (for generic implementation)
3) Facebook like(for Facebook ThankYou implementation)

Which design pattern do you think this is better implemented using - Factory or Mediator? I am just starting to utilize some design patterns in my code and would like to start off on the right foot.

Some notes:
a) Although the functionality might be the same, the layout might be different for generic and Facebook

If I don't use a design pattern, I could easily do this using an 'if' statement, but I am just looking for a more elegant solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-10-25 21:11:16

我认为 Factory 更适合这种情况。您有一个名为 IThankYou 的基类(接口),它实现了通用方法和两个扩展基本功能的类。工厂存储类型和类之间的映射。

小示例代码:

function IThankYou () {}
IThankYou.prototype = {
  templates: { // this is common field for all instances extending this class
    like: '<div class="b-like">Like</div>',
  },      

  like: function () { throw "Unimplemented" }, // this method is not implemented in base class

  commonMethod: function () { } // this is common method
};

function GenericThankYou (someParam) { this.someParam = someParam; };
GenericThankYou.prototype = new IThankYou;
GenericThankYou.prototype.like = function () {
  // there you could use base class fields and methods
  this.commonMethod();
};

function FacebookThankYou (someParam) { this.someParam = someParam; };
FacebookThankYou.prototype = new IThankYou;
FacebookThankYou.prototype.like = function () {
  // there you could use base class templates map
};

var ThankYouFactory = {
  typeMap: {
    'facebook' : FacebookThankYou,
    'generic'  : GenericThankYou
  },
  getConstructor: function (type) {
    return this.typeMap[type];
  }
};

ThankYouFactory.getConstructor('facebook')(ctorParam);
ThankYouFactory.getConstructor('generic')(ctorParam);

I think that Factory more suitable in this case. You have base class (interface) called IThankYou that implements common methods and two classes extending base functionality. Factory stores mapping between type and class.

Small example code:

function IThankYou () {}
IThankYou.prototype = {
  templates: { // this is common field for all instances extending this class
    like: '<div class="b-like">Like</div>',
  },      

  like: function () { throw "Unimplemented" }, // this method is not implemented in base class

  commonMethod: function () { } // this is common method
};

function GenericThankYou (someParam) { this.someParam = someParam; };
GenericThankYou.prototype = new IThankYou;
GenericThankYou.prototype.like = function () {
  // there you could use base class fields and methods
  this.commonMethod();
};

function FacebookThankYou (someParam) { this.someParam = someParam; };
FacebookThankYou.prototype = new IThankYou;
FacebookThankYou.prototype.like = function () {
  // there you could use base class templates map
};

var ThankYouFactory = {
  typeMap: {
    'facebook' : FacebookThankYou,
    'generic'  : GenericThankYou
  },
  getConstructor: function (type) {
    return this.typeMap[type];
  }
};

ThankYouFactory.getConstructor('facebook')(ctorParam);
ThankYouFactory.getConstructor('generic')(ctorParam);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文