Reputation: 106
One thing that I guess I did not learn correctly was when to use classes. For everything that I have had to do in school, another class wasnt entirely necessary unless I was working with objects in java. What are some good reasons and practices for using classes?
Upvotes: 3
Views: 332
Reputation: 12887
Think of your program as a car. A car, although it's one big machine, is made out of several components. Those components can interact, to make the car move. The motor needs the fuel system to get fuel. For the motor, it doesn't matter how the fuel gets there, as long as it gets there. The motor, with the help of some other components makes the wheels go round. For the wheels, it doesn't matter how the motor works, as long as they get pushed by the motor. This makes it very easy to create a car with a team of people. As long as everyone knows how all the components will interact, the components can be built individually.
Now think of your program as a car. You'll build all the components, which will then work together to form the program. One of the benefits is that you can develop parts independantly. A second advantage, is that using multiple classes gives your program a better structure. There is no risk that a file saving component could accedently override some of the back-up component's data, because classes can choose which data they expose and how.
A good way to divide your program into classes is by thinking of responsibilities. For example, in a car, the engine is responsible for getting the wheels to move, the tank is responsible for holding fuel,... There are two main types of responsibilities: doing something and knowing something. Classes can either be designed to execute certain tasks, or hold certain information. If you want to know more, google around a bit with the words Object oriented design and grasp. Grasp are a set of rules that make OO design a lot easier.
Upvotes: 1
Reputation: 33979
Invariants are owned by classes. Whenever you have two or more variables/objects and there is an invariant that must be maintained between them, then you should make a class that maintains the invariant.
Upvotes: 0
Reputation: 52247
Classes reflect responsibilities. Each class should have exactly one responsibility. So you add all behaviour and data that belongs to one responsibility to one class. A method that helps you to identify these responsibilities and define the necessary classes is called CRC cards.
Upvotes: 2
Reputation: 309008
Object-oriented programming models the world as software components that encapsulate data and behavior together. They communicate by sending messages to each other.
If that's a good fit for your problem, then OOP will work.
There's no magic recipe that says "Square peg, meet square hole."
If you've already accepted OOP, then a good start on objects would be Bob Martin's SOLID principles
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod http://blog.objectmentor.com/articles/2009/02/12/getting-a-solid-start
Upvotes: 4
Reputation: 4593
Classes reflect a type of object. What helps me to divide functionality into classes is to think of each concept in my program as a noun. Objects have behavior, they do things, so classes should define behaviors that their instances will perform.
Upvotes: 1
Reputation: 43108
In the real world you have a lot of objects, and all they have different properties and behave differently. In order to properly describe all this classes are very useful. So, when you have different object, which should communicate somehow with each other, object oriented programming is good.
Upvotes: 1