【问题标题】:OOP Advice to get rid of a switch statement using prototype使用原型摆脱 switch 语句的 OOP 建议
【发布时间】:2016-11-23 04:12:01
【问题描述】:

Javascript OOP 中事务的多态性:

我正在尝试在 javascript 中的 OOP 方面做得更好,目前正在研究用于处理不同类型的商业交易、购买、投标等的 OOP 解决方案。

我之前的做法是,我在 if 语句的一侧拥有所有卖方操作,并带有一个开关来处理每种交易类型。

然后我为买家做了同样的事情。

通过此操作,我改进了代码结构,使其更易于阅读,但我发现自己认为 processSellerTransactions 和 processBuyerTransactions 中的“SWITCH”是错误的。

如果我设置了所有原型(购买、出价)等。我想直接调用原型函数而无需使用 switch 语句。

谁能指出我正确的方向(教程等)或指出我做错了什么。

我得到它(有点),但它仍然很新,所以任何帮助将不胜感激。

提前致谢。

var Transaction = function(transactionType) {
    this.transactionType = transactionType;
};

Transaction.prototype.buy = function(){
    console.log("Buy");
};

Transaction.prototype.bid = function(){
    console.log("Bid");
};

function Type(transactionType, transactionId) {
    Transaction.call(this, transactionType); 
    this.transactionId = transactionId;
}

Type.prototype = Object.create(Transaction.prototype);

Type.prototype.constructor = Type;

Type.prototype.processSellerTransactions = function(){ 

    switch(this.transactionType) {
        case 'buy':
                  console.log('do the buy logic for seller');
                break;
            case 'bid':
                  console.log('do the bid logic for seller')
                break;
    }
};

Type.prototype.processBuyerTransactions = function(){ 
    switch(this.transactionType) {
        case 'buy':
                 console.log('do the buy logic for buyer');
                break;
            case 'bid':
                 console.log('do the bid logic for buyer')
                break;
  }
};

var user = 'seller';
var transaction_type = 'buy';
var transaction_id = '12345';

var transaction = new Type(transaction_type, transactionId);

if(user == seller)
    transaction.processSellerTransactions();    

if(user == buyer)
    transaction.processBuyerTransactions(); 

感谢 John 提供有关策略模式的建议。这是一个简单的 Javascript 示例

Strategy Pattern In Javascript Example

【问题讨论】:

  • 听起来你真的想做一个BuyTypeBidTypeTransaction 子类?
  • 是的,我想可能就是这样。我想做我需要做的任何事情,以便能够相当快地进行扩展。老板是个疯狂的制造者,所以说他们明天会进来说。 “我们也希望允许人们搁置一些东西”。如果只有这两个就很简单了。我正在尝试考虑未来添加的事务类型。购买、出价、搁置、退货、延期交货等

标签: javascript oop prototype


【解决方案1】:

您需要制作TransactionType 和知道如何处理事务的抽象,然后您可以使用Strategy Pattern 将处理传递给TransactionType

您可以将其转换为原型样式

function BidTransactionType(){
  this.processSellerTransaction = function(type){
    console.log('process seller transaction')
  }
  this.processBuyerTransaction = function(type){
    console.log('process buyer transaction')
  }
}
function Type(transactionType,
Type.prototype.processSellerTransactions = function(){ 

    switch(this.transactionType) {
        case 'buy':
                  console.log('do the buy logic for seller');
                break;
            case 'bid':
                  console.log('do the bid logic for seller')
                break;
    }
};
function Type(transactionType, transactionId) {
    this.transactionType = transactionType; 
    this.transactionId = transactionId;
}

Type.prototype.processBuyerTransactions = function(){ 
    transactionType.processBuyerTransaction(this);
};

Type.prototype.processSellerTransactions = function(){ 
    transactionType.processSellerTransaction(this);
};

var user = 'seller';
var transaction_type = new BuyerType();
var transaction_id = '12345';

var transaction = new Type(transaction_type, transactionId);

这样做的好处是您可以创建更多交易类型,而无需更改您的 Type 对象,与 Open/Closed Principle 保持一致

【讨论】:

  • 是的,策略模式正是我想要的。
【解决方案2】:

您可以通过拥有 BuyerTransaction 和 SellerTransactionClass 来摆脱那个 switch 语句。然后将该逻辑移到另一个实例化正确类型的函数中。

例如

function createTransaction(person){
if(person === 'buyer'){
  return new BuyerTransaction();
}
return new SellerTransaction();
}

var BuyerTransaction = function(){}
BuyerTransaction.prototype = Object.create(Transaction.prototype);
BuyerTransaction.prototype.buy = function(){}
BuyerTransaction.prototype.bid = function(){}

var SellerTransaction = function(){}
SellerTransaction.prototype = Object.create(Transaction.prototype);
SellerTransaction.prototype.buy = function(){}

//at runtime
var transaction = createTransaction('buyer');
transaction.buy();

【讨论】:

  • 也这样。
猜你喜欢
  • 2021-12-02
  • 2018-09-08
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
相关资源
最近更新 更多