Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1031753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:58:47+00:00 2026-05-16T13:58:47+00:00

Considering architecture…. I normally have a shopping cart class with addItem and removeItem methods

  • 0

Considering architecture….

I normally have a shopping cart class with addItem and removeItem methods (amongst a couple of others). However in the spirit of real world shananigans carts should be acted on by customers – therefore should the addtoCart / removefromCart not be a method of the customer?

OR should I have an itermediate CustomerActsOnCart object that takes the customer and cart objects as arguments in the constructor and perform the manipulation in there???

Any musings would be most welcome…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T13:58:48+00:00Added an answer on May 16, 2026 at 1:58 pm

    Even if you give a Customer the methods addToCart() and removeFromCart(), you would still have to have some logic in ShoppingCart to have these methods actually do the state change when an item is added or removed. IMO, the actor in this case is much better represented by a reference to the owning Customer and/or viceversa, e.g.

    $cart = new ShoppingCart;
    $cart->setOwner(new Customer);
    $cart->addItem(new Item('Apples'));
    $cart->checkout();
    

    You could approach this from the customer as well, e.g.

    $customer = new Customer;
    $customer->setShoppingCart(new ShoppingCart);
    $customer->addItemToShoppingCart(new Item('Apples'));
    $customer->checkout();
    

    But the method name addItemToShoppingCart already implies that the Customer acts on a ShoppingCart, so inside you are likely doing

    $this->getShoppingCart()->addItem($item);
    

    If ShoppingCart was a composite element of Customer for which we wanted to hide implementation details, we could use something like that, but since the ShoppingCart is IMO not an essential part of a Customer this method shouldnt be on Customer. It’s nothing but a convenience proxy.

    You could create a Decorator for the Customer to handle the Shopping Behavior. This would decouple the concern/responsibility of How-To-Shop from the Customer, e.g. something like

    class ShoppingDecorator
    {
        protected $actor;
        protected $cart;
        protected function __construct($actor) { ... }
        public function getCart() { ... };
        public function addToCart() { ... }
        ...
    }
    

    This would also allow you to apply the Shopping Behavior to other actors that might need this behavior.

    The CustomerActsOnCart approach sounds somewhat like the Mediator Pattern. Not sure if it’s a feasible approach though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
Background I am considering various architecture options. While I have used SQL over the
Considering the following architecture: a base object 'Entity' a derived object 'Entry:Base' and a
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering private is the default access modifier for class Members, why is the keyword
I am considering the general architecture of my next project. For the back-end, haskell
When considering social web app architecture, is it a better approach to document user
I'm considering a multi-threaded architecture for a processing pipeline. My main processing module has
Long version: I'm new to erlang, and considering using it for a scalable architecture.
We're considering moving to a new architecture with one master Redis database and 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.