Learning Object Oriented Thinking

I am currently working on a small 2D engine in C ++, but now I come across a demon - I suck the development of a "class system" that really works. There is a blockade in my head that forbids me to see where I should use the class and where I should not. I read an article on engine design and intended to use the "State" class to control the state of different game records (I used int). He also suggested that all objects for the game (not io / video / sound, etc.) come from the Renderable or NonRenderable classes. This is smart. I already know that this was a reasonable way to do this - I mean that every object in Java has a basic Object on the right? Smart, I know that! Why didn’t I do that? What should I read in order to really enter into this thinking?

Another example. I take this summer course at Ruby (very basic) and we need to design a campsite. Simply! Thus, camping is a collection of “plots”, each of which has an electric pressure gauge to measure how much energy a guest consumes. My project consisted of three classes, one for camping, which, in turn, used arrays of the classes Guest and Plot. My teacher suggested using more classes. WTF (!) Was my first thought, where, what classes? In my opinion, everything was a class, until I realized, maybe the caliber should be a class? Integer in the Plot class is currently calibrated.

I want to learn how to come up with object-oriented solutions to my problems, and not just how to make the most obvious material in classes!

Tips / books / articles / blogs?

I’ve been doing a collage degree in CS for two years and have been programming as a hobby for many years! I am “stuck” - and this prevents me from creating more software!

+43
oop
Jul 21 '09 at 8:01
source share
16 answers

My personal experience has been to study object-oriented software using Object-Oriented Software Development, second edition of Bertrand Meyer.

At that time, the book was invaluable and still remains the only book from which I learned the most about OO programming and software development in general.

Here are some of his strengths:

  • Part A: Problems , a very good definition of software quality.
  • Part B: The road to object orientation , a logical step-by-step search for OO methods in such a way that the reader believes that the study is carried out live, that is, as if there were still no known results. You will probably get the thinking you are looking for from this part.
  • In Part C: Object-oriented methods , the technical core of the book, you will make your knowledge sound and learn very useful design methods for contracts, inheritance, universality, etc.
  • Part D: OO methodology: applying the method well is a more practical design approach that I also find very useful. See For example How to find classes (22) that you can find online .

After these parts, more complex topics appear, such as Concurrency (30) or Databases (31) .

Since the book uses the Eiffel language (designed by the author), it will put you in the right mindset and teach you how to think. These ideas will be easily applied to other, more or less OO, programming languages.

+20
Jul 21 '09 at 8:18
source share

Object oriented

Object-oriented programming is asking objects to do something: a deceptively complex concept to apply correctly.

Goban

Consider a two-dimensional game panel, for example, to play Go (called gobana).

First think about the behavior you need to complete your task. This means listing the behavior for an object, rather than defining the data that manipulates the behavior. For example, a baseboard may have the following types of behavior:

  • Place the stone of the Year.
  • Remove the Go stone.
  • Remove all stones.

For the computer version of Go, it’s convenient to draw attention to specific areas:

  • Mark the intersection (e.g. triangle, number, letter, circle, square).
  • Remove the mark from the marked intersection.
  • Remove all tags.

Please note that gobans should not provide a way to provide customers with links to the stone at a particular intersection. Instead, he can answer questions about his condition. For example, a gobana may answer the following questions:

  • Is there a black stone at this intersection?
  • Is there a white stone at this intersection?
  • Is there a mark at this intersection?

Goban is not responsible for the state of the game: this belongs to the instance of the game (which has the Rules). In real life, a gobana is just a scene for stones.

At this stage, we could write an interface for gobana, not knowing how the basic implementation will work.

public interface Goban { public void place( Stone stone, Point point ); public void removeStone( Point point ); public void removeStones(); public void place( Mark mark, Point point ); public void removeMark( Point point ); public void removeMarks(); public boolean hasWhiteStone( Point point ); public boolean hasBlackStone( Point point ); public boolean hasMark( Point point ); } 

Please note that the board is clearly separated from the rules and games. This makes gobans reusable for other games (including stones and intersections). Goban can inherit from a common interface (for example, the Board interface), but this is enough to explain one way of thinking in terms of objects.

Encapsulation

The implementation of the Goban interface does not provide its internal data. At this point, I could ask you to implement this interface, write unit tests, and send me a compiled class when you are done.

I do not need to know which data structures you used. I can use your implementation to play (and portray) Goban. This is an important point when many projects are mistaken. Many, many projects encode the following:

 public class Person { private HairColour hairColour = new HairColour( Colour.BROWN ); public Person() { } public HairColour getHairColour() { return hairColour; } public void setHairColour( HairColour hairColour ) { this.hairColour = hairColour; } } 

This is ineffective encapsulation. Consider a case where Bob does not like his hair to be pink. We can do the following:

 public class HairTrickster { public static void main( String args[] ) { Person bob = new Person(); HairColour hc = bob.getHairColour(); hc.dye( Colour.PINK ); } } 

Bob's hair was now pink, and nothing could stop it. There are ways to avoid this situation, but people do not. Instead, encapsulation is broken, resulting in hard, inflexible, distorted errors and unsupported systems.

One possible way to ensure encapsulation is to return the HairColour clone. The revised Person class now makes it difficult to change hair color to Pink.

 public class Person { private HairColour hairColour = new HairColour( Colour.BROWN ); public Person() { } public HairColour getHairColour() { return hairColour.clone(); } public void setHairColour( HairColour hairColour ) { if( !hairColour.equals( Colour.PINK ) { this.hairColour = hairColour; } } } 

Bob can sleep peacefully, knowing that he will not wake up to the pink paint job.

+13
Jul 21 '09 at 9:00 a.m.
source share

Keep in mind: OO is not an end in itself. The point of OO is to make development, and especially code maintenance, easier throughout the life of the product. Beware of thinking "OO for OO".

+9
Jul 21 '09 at 8:52
source share

Initial Object Oriented Analysis and Design

I like The First Book because they are interested in reading. They have exercises and puzzles to scratch your head. I read this book and found it very good.

The book covers:

  • Use the principles of OO (encapsulation and delegation)
  • Open Closing Principle (OCP)
  • Single Responsibility Principle (SRP)
  • design patterns, UML, use cases, etc.
+6
Jul 21 '09 at 8:06
source share

There is a blockade in my head that forbids me to see where I should use the class and where I should not.

When it comes to this, classes are a way to divide complex systems into simple parts that interact with each other. Try creating classes, otherwise you would repeat.

The calibrator is currently an integer in the Plot class.

Should a class be a class? What would be the advantage of turning it into a class? These are the things you always need to ask yourself.

  • Game engines are difficult to design. Separating such vaguely defined requirements is a complex process to read here: an article on game engines
  • The design is iterative, and you will refactor several times, do not be surprised at this.
+4
Jul 21 '09 at 8:37
source share

There's an essay in Jeff Bay's “ ThoughtWorks Anthology ”: “Object Calisthenics,” in which he gives a set of rules for designing OOP software:

  • Use only one indent level for each method.
  • Do not use the else keyword
  • Wrap all primitives and strings
  • Use only one dot per line
  • Do not cut
  • Keep All Entities Small
  • Do not use classes with more than two instance variables
  • Use first class collections
  • Do not use getters / setters / properties

At first glance, he may look too strict to follow all these rules. Keep in mind that even trying to write some code that calls them will make you the best OOP developer.

+3
Jul 21 '09 at 8:19
source share

Just remember that there has never been a solution to a problem. Turning everything into a class is also not a solution. Especially tiny things (for example, a manometer) may well be members of an int or float inside the plot class, just like you do.

My suggestion is that practice is a good teacher. Just keep trying and keep reading. Over time, you will be more and more fluent in speaking.

+2
Jul 21 '09 at 8:11
source share

I probably learned the most about developing object-oriented software from Craig Larman's. Using UML and Templates: An Introduction to Object-Oriented Analysis and Design and Iterative Development .

In their approach, classes are produced systematically from use cases:

  • Nouns in use cases are mapped to classes,
  • verbs to methods and
  • adjectives to member variables.

This, of course, works better for concepts in the problem domain than, say, GUI widgets. However, starting with the description / use case of the program to be written, I managed to find better abstractions than when I skipped this step.

+2
Jul 21 '09 at 8:48
source share

For me, OO is not a 'click' until I read a book about design templates. If you already like concepts such as abstract classes, interfaces, etc., then only half the battle.

The next step is to find out why you should prefer composition over inheritance, how to code the interface, and how to write your classes so that they are decoupled and well encapsulated. Design patterns show you solutions to common OO problems and help structure your code by following the guidelines above.

I cannot recommend any specific books about C ++, but GOF book is the standard in Design Patterns (Java). I prefer books that talk about design patterns in a particular language so that you can get specific code examples. Design patterns in Ruby are pretty good, and PHP: objects, Templates, and practice .

It seems to me that your instructor does not know what he is talking about. "More classes" is useless advice.

+2
Jul 21 '09 at 8:51
source share

You may find the Thinking in the Bruce Eckel templates useful. You can download this book for free from your website (I can publish only one link as a new member, so just click on the links there and you can find it). Although, the book since 2003, perhaps the ideas presented in this book will help you grow as a programmer as a whole.

+2
Jul 21 '09 at 13:08
source share

Write a really huge part of the software, and throughout the process, the more you get it, the more extensiveness you will need and you will need a better class, so the next time you think ahead and make your design class good at the beginning ...

+1
Jul 21 '09 at 8:10
source share

A simple way to come up with a reasonable set of things that should probably be objects (and therefore classes): write down a description of the problem of your task, for example:

There are guests at the campsite, and each guest has access to several shops. the software must be able to control the energy consumed by each guest, so it must know the outputs used by the guest and the power consumed through each outlet.

Now create a list of all nouns for a good idea about which classes (= kind of objects) are involved in your problem:

  • Camping spots
  • the guest
  • Exit
  • Food

This is not necessarily the final list, but it is a good start.

+1
Jul 21 '09 at 9:25
source share

One of the things that helped to get into the thinking of OO, along with the practice that was previously reported, is to rewrite / improve the existing code that you wrote using the principles of OO.

For example:

but. In situations where there are many if / else constructs, perhaps you can think of a class hierarchy for distributing branch codes, respectively, and use polymorphism.

b. Any use of operators such as (instanceof in Java) points to specific types, and you might consider how to get rid of instance validation.

from. Use the "Law of Demeter" as a guide and see if there is a connection between the classes are highly

To some extent, the practice of “Test Driven Development” also helped me, as it makes you think in terms of interfaces / behaviors that should be exposed and not just focus on how best to solve the problem can be encoded.

+1
Jul 21 '09 at 10:21
source share

Haha I remember this moment. The whole "how the hell does this thing work?" Just hold on to it, at some point he just presses. It really looks like a light bulb. At one point this makes no sense, and then after a second you encode everything in the classes.

Try downloading some of the open source tools that are likely to end up using and reading the code. This will give you something to reference your code style.

0
Jul 21 '09 at 8:09
source share

Peter Coad and Ed Theidon wrote a book about this a couple of years ago. Although this new book is not filled with new super-high methodologies, this book provides a good basis for thinking in the style of an object.

0
Jul 21 '09 at 8:24
source share

In my opinion, one of the best books I've read to learn about object-oriented concepts is:

Object Oriented Thinking

For me, this book really makes you think in an object-oriented way (well, the key is in the title!) This is a pretty linguistic agnostic that contains some small code examples throughout the text in VB.NET, C # and Java and often refers to many of the “greats” in the world of OO analysis and design, such as Grady Booch , Martin Fowler and others.

Because a book helps you think in an object-oriented way, it often takes a concrete example and demonstrates the differences between the OO method of approaching a problem and the procedural method. This can be a big help if you come from a more or procedural background. It also touches on things like UML to help explain and understand the design of complete class libraries (such as frameworks) and the interaction between classes, as well as the design of rich classes using concepts such as aggregation and composition.

0
Jul 21 '09 at 8:35
source share



All Articles