An Extract and Override Exercise
Extract And Override Solution
If you want to take a look at our solution, here you go!
We've changed m_accountId to be protected.And we've extracted two awkward parts of methods:
void Order::add(const LineItem& item)
{
m_items.push_back(item);
m_netTotal = m_netTotal.add(item.getExtendedAmount());
m_weight = m_weight.add(item.getExtendedWeight());
reserveProduct(item);
}std::string Order::getOrderId() const
{
if (m_orderId == "")
m_orderId = getOrderIdFromGenerator();
return m_orderId;
}void Order::reserveProduct(const LineItem& item) const
{
InventoryControl::get()->reserveProduct(getOrderId(), item.getSku(), item.getQuantity());
}std::string Order::getOrderIdFromGenerator() const
{
return IdGenerator::getOrderId(m_accountId);
}
We created
TestableOrder
right in the OrderTest
file, and overrode the two awkward parts.
class TestableOrder : public Order
{
public:
TestableOrder(const std::string& accountId) : Order(accountId)
{ }protected:
virtual std::string getOrderIdFromGenerator() const
{
return m_accountId + ":10001";
} virtual void reserveProduct(const LineItem& item) const
{
}
};
class OrderTest : public testing::Test…
protected:
virtual void SetUp()
{
order = OrderPtr(new TestableOrder("ACCOUNTID"));
}
You can download our solution here:
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |