Object Oriented Programming - When to Add a New Class?

I am still very much learning the basics of Objective-C, but I'm still trying to deal with objects and classes.

I understand the usual analogy: “Car” is a class, and “Ford” is an object of this class, with certain properties, such as “color” and “year of manufacture”. I do not understand how this can be used in practice.

Let's say I create a basic web browser with all the usual functions (load the page, show the page, add the page as a bookmark, print the page). It seems that I do not think about such an application, except for simple variables and methods. You know, you press a button, and it calls a method that manipulates some variables. I also can't help but think that this will be one very long page of code, so I understand that this will help break it down.

I apologize if this is all very vague, it’s just hard for me to understand when I should create new classes, and when it’s normal, just add another method to the class that I already work in.

Sincerely.

+4
source share
5 answers

Lots of tips for a new class

  • When you can name something, if there is a concept in the code that you can name, this is a good candidate for a new class.

  • If you have several functions that control the same variables. If you pass the same variables to several functions, then most likely these variables and functions should be classes

  • Get good features from any complex code. This should be your first priority, good features that do well. Then see if all the functions are associated with one concept and group together in the class.

  • You have a lot of code that does the same thing, but handles different options. Then you have an abstraction / interface and a few subclasses.

Basically, don't be afraid to make a class ... just grab a punt and do it. If the class seems to just make things uncomfortable. You have learned something. So you need to rethink the design and find another way to break things / abstract things. All about how to try.

Start looking for design patterns and you will begin to see some common ways that other people handle uncomfortable situations a little more elegantly.

+6
source

The class is not limited to specific things, as you mentioned Car, Ford. It can be used to display abstract things. At least in your case, the browser itself, the page and the bookmark can be classes. Object layout is good OO programming practice.

0
source

First you need to analyze your domain, the data that your site will work with. Just write on paper, extract the nouns, and think about whether you can imagine them as separate objects. In your example, you can create a Car class if you have a car shop site. Soon you will be able to retrieve various types of equipment for this car and other related classes. It is simply a matter of thinking in terms of objects, but not methods in the first place. Look here , maybe this will give you more information. In the end, you probably work with a database, which itself involves dividing the data into entities. Probably many of these objects have methods. Also look at MVC for a basic knowledge of how you can present a Car on a web page. Hope this helps you a bit.

0
source

You think about procedural languages ​​like C. This is not bad, because there are all kinds of uses for procedural languages, but that’s not the way you want to think with an object-oriented language like Objective-C.

In procedural language, you have some data, and you have code. These two are separate. Although you may have functions that work with data, these functions can be anywhere in your source, as well as data. There is no clear division of responsibilities unless you take care of developing your program in a certain way. One way to differentiate is to write C code that looks very awful like object oriented code.

In OOP, you have data and code that works with this data. Code and data go together. Data is the member variables of your class, and code is the member functions of your class.

You can create large objects by combining objects of simpler classes. For example, a car will contain objects of the engine class, body, four objects of the tire class, etc.

Objects can also connect to each other if one object uses another. The driver’s object will use the car, or the car will use the road.

Object Oriented Programming is not the Silver Bullet we have been looking for since computers were invented. This is just a good way to organize your code. As you become more experienced, you will quickly find that the object-oriented code can be the same rat nest as the procedural code. If so, you are not thinking about your problem correctly.

0
source

You create a class when you want to represent an object. In your example, you mentioned that you create a web browser that performs tasks such as loading a page, displaying a page, adding a page as a bookmark, printing a page ... as you can see, here everything happens around the page ... You can also make the page a class and add various methods that work on it. If you observe popular browsers, for each new page you want to open a new tab or create a new window ... you can get the form the same way.

So....

  • Create a class when you want to represent the object that you would like to act on.
  • Create a function if you want to do the Task.

Happy coding!

0
source

Source: https://habr.com/ru/post/1369433/


All Articles