I am trying to create a supermarket software that allows either a customer or the owner to log-in and use my system via a swing based GUI in Java. When the Customer has logged in they can view products. When the owner has logged in they can view products and add new products.
I want a method in the Customer
class: ViewProducts()
and methods in the Owner
class: ViewProducts()
, AddProducts()
.
Are these methods wrong because they're not specific to the customer/owner (they're related to the product).
My relationship would be the Customer
class having a 1 to 1 relationship with ProductList
and the Owner
having a 1 to 1 relationship with ProductList
, and the two classes can manipulate the data in their own way. Am I going about this wrong?
This way doesn't make sense because Customer
and Owner
can't have attributes that aren't related to them such as ProductList
.
You should always aim to capture what exists in reality. A Customer
instance does not have a 1 to 1 relationship with a ProductList
because a ProductList
can be viewed by more than one Customer
at a time, and the Customer
in no way owns that list.
What is probably closer to reality is:
Supermarket
individual manages one Inventory
individualInventory
individual:
Supermarket
individualInventory Item
individualsInventory Item
individual
Inventory
individualProduct
individualsProduct
individual
Inventory Item
individualPhysical Location
individualUser Account
individual
In real life, people play roles. These roles might be “customer”, “doctor”, or “police officer”. Every individual Role
has a set of capabilities it can perform. In an OO system, every individual Role
can use operations to implement its capabilities, such as purchaseProduct()
, prescribeMedication()
, or writeMovingViolation()
.
There are multiple ways to represent these roles and capabilities in an OO system. In one approach, a customer
instance of a Role
might be configured to allow access to queryInventory()
and purchaseProduct()
operations on the Supermarket
and InventoryItem
classes respectively. An owner
instance of a Role
1 might be configured to allow access to addInventoryItem()
and removeInventoryItem()
operations on the Inventory
class.
Here is an example of a UML model:
In another approach, you might create singleton subclasses of the Role
class, called CustomerRole
and OwnerRole
, and then have each of those subclasses invoke operations. You might put your viewProducts()
and addProducts()
operations into those singletons.
1 Consider calling this role “manager”, so the owner of the supermarket can hire other people to do the work.