简体   繁体   中英

How to decide methods in class

I am new to Object Oriented Programming. I am developing a software for a Grocery Store. The Grocery Store has Customers, Customers has Address and Subscriptions. All these are different classes in the application. I am little confused, that in which class should I create which method and how I should decide this. Like viewSubscription() should be a part of Subscription class or Customer class. modifyCustomer() should be a part of Customer class or Store class.

One of the ideas of object oriented programming is to group things together that make sense. In your example, since they are dealing with information related to the Customer, I would probably place both methods in the Customer class.

Methods are behavior, variables are state.

What can a subscription? from what is a suscription made?

A Suscription cant view itself, who will view a subscription? a customer?

A suscription should be a class indeed, but a POJO, its a collection of states. And customers can see those states.

OOP makes it easy to break down complex problems. You might want to sit down and do a schema of the relations between your classes and what data your classes will contain. It will become obvious where which method should go.

Why should view subscription should a member of Subscription?

Always ask your self this simple question: view subscriptions of what? On what do you want to do the action.

I bet you want to view the subscription of a Customer! Make viewSubscription a method of Customer! customer1.viewSubscription()

Check out UML and OCL. They will help you to model your ideas.

Just an overview: An object represents a real life entity lets say for example a car , A car has some properties like it has wheels,a steering,a gearbox and much more, the same way it has some behaviors like it moves forward, steers left, steers right and it stops. All the things mentioned above related to the car when brought down to an Object oriented programming approach will look like, We make a class Car and the properties(wheels,a steering,a gearbox etc) are defined as variables inside that class and the behaviors(moves forward, steers left, steers right) are defined as functions in that class.There's no hard and fast rule relating OOP you just have to make it feel logically as Real Life as possible for instance in your case the Subscription Class has all the information related to the subscription and the Customer has a subscription so the viewSubscription() method should come inside Customer Class as a Private field because it should fetch and display the subscription information related to a particular customer. modifyCustomer() as it involves modifying the data fields of the Customer class so this too will come inside the customer class as all the modification of the field values should probably be done inside the class containing the fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM