Object Design

Rebecca Wirfs-Brock, Alan McKean

Mentioned 6

Object technology pioneer Wirfs-Brock teams with expert McKean to present a thoroughly updated, modern, and proven method for the design of software. The book is packed with practical design techniques that enable the practitioner to get the job done.

More on Amazon.com

Mentioned in questions and answers.

I'm trying to wrap my head around Object Oriented programming. But I'm having some trouble. I (think) I understand the general concepts and the arguments for why OOP is a 'good' design. My problem comes when I sit down and try to write code that is OOP.

I tend to end up with programs that are either very procedural but have the occasional object tossed in for good measure....or programs that seem ridiculously long and complex for what they are doing....everything is an object; but there are many, many objects and the inheritance trees get long and ugly.

What I've been trying to find is some non-trivial examples (I've seen plenty of throw-away/pseudo code involving cats, dogs, and animals....but they don't seem to help when I actually try to code up something that needs to do something) of really well designed OOP source code. Ideally, I'm looking for something that would step me through the thought process. Like 'Okay - here is some procedural code that does XYZ. Now, here is some really great OOP code that does the same thing!'.

Thanks

The problem procedural programmers often have when starting object-oriented programming is they keep designing procedures and try to organize them as objects. That doesn't work.

Object-oriented programming is a different design methodology; a different way of thinking about the roles and responsibilities you distribute throughout your code, not just a different coding methodology.

When you get past the "dog is-a mammal" metaphors (which never translate to real applications), I would recommend this book: Object Design: Roles, Responsibilities, and Collaborations. It was the first book I read where I finally got why I had to stop viewing (in my case) C++ as "C with classes thrown in".

a point of architectural style that I'd like your opinion on please:

My ORM has given me a User object which corresponds to a user of my system. I wanted to develop a bunch of methods for handling Users - GetByUsername(), Authenticate(), VerifyLoginPassword() etc. However it feels to me that some of these methods don't really belong to the User class - e.g. GetByUsername() feels like a static method of User at least, but wouldn't it be more "clean" to have another class, say "UserManager" which provides us with these User-management type of tasks? It seems a little strange for a User instance to contain the Authenticate() method, for example, if it's the security system that does the authenticating?

The thing I worry about is that I end up following this model to the point where the User class is no more than a struct, and my User Manager and Security Manager classes actually do all the method work. It doesn't feel very "OO" to have all these manager classes manipulating lightweight objects.

Any thoughts or links to prior art on this philosophical matter would be appreciated!

It sounds like you at that point where you are moving beyond defining objects as "a dog is-a animal" and moving into object definitions in terms of roles, responsibilities, and behaviors.

I would recommend this book to help you make that transition and really "get it":

Object Design: Roles, Responsibilities, and Collaboration

I don't have an answer to your specific question because it is such a fundamental design decision that you really should learn about the "bigger picture." This book will give you a good foundation in the principles and techniques of Responsibility-Driven Design.

Enjoy,

Robert C. Cartaino

I was reading this paper from Apple:

http://developer.apple.com/library/mac/documentation/cocoa/conceptual/OOP_ObjC/OOP_ObjC.pdf

where it talks about OOP which I never heard before. I graduated in computer science around 1991, before OOP becoming popular, so the use of OOP was merely defining some classes, and then calling the methods, that's it. Objects didn't interact with each other -- everything was done in a main function that calls the various objects' methods.

Until I read the paper above, which talks about Interface, dynamic typing, dynamic binding, that an object can send another object a message, even before the second object is invented -- only the "interface", or the message, needs to be well defined. The second object can have unknown data type as of right now, to be invented in the future, but all it needs to do is to understand the "message".

So this way, each object interacts with one another, and each object may have a list of "outlets" which are the relationship it has with the outside world, and the object will interact with the outlets by sending them messages, and those objects, when getting a message, can in turn send back a messages to the sender. (send a message to an object = call the object's method).

I think this sort of opened my eye for OOP, much more than even the Design Pattern book by the Gang of Four. The Apple paper didn't cite any source, but I wonder it might follow some methodology from a book? Does any OOP book give a good, solid foundation in OOP which is what the Apple paper is talking about?

Nice introduction to OOP is "Coffee maker" (and quite short).

I personally really enjoy reading "Object thinking".

Another interesting book is "Domain-Driven Design: Tackling Complexity in the Heart of Software".

Next in my to-read list is "Object Design: Roles, Responsibilities, and Collaborations".

I am from .Net background and I am planning to read the following book to address this question.

Foundations of Object-Oriented Programming Using .NET 2.0 Patterns - Christian Gross

What I am finding interesting about this book is

  1. Use of generics
  2. Explaining patterns as a solution to a problem

my question is in relation to operations on a class diagram.

I'm very new to UML and I am creating a class diagram for an online quiz system where a teacher will assign login details to a student, the teacher will create a multiple choice quiz and then from a specified time, students can log in to complete the quiz.

My question is in relation to the operations included. For example, the teacher will add a 'QuizQuestion'. So can I include the operation 'addQuizQuestion()' in the 'Teacher' class as the 'Teacher' will perform the task or shall I add it to the 'QuizQuestion' class as it is what is being created?

Similary, once results have been collated, reports are generated and sent out to parents. So shall I have a 'receiveReport()' operation in the 'Parent' class or should there be a 'sendReport()' operation in the 'Report' class?

I'm so confused!!

Thanks

Your confusion has nothing to do with diagramming and everything to do with basic object oriented concepts such as in your particular case, roles and responsibilities. I think you would do yourself (and your teacher) a favor by reading a book on this topic such as Object Design: Roles, Responsibilities, and Collaborations or a more general OO book such as The Object-Oriented Thought Process, before embarking on this project.

I am a newbie in software engineering and I want to specialize in C++. Now, I know C++ syntax very well and use Qt. But I don't have any knowledge about UML, design patterns or the Boost Library. Which library or topics do you recommend I learn first?

Learning syntax is like learning a grammar that is a stepping stone to become a poet. To become a poet you need to know how to form sentences that are easy and catchy.

Similarly for any language you should know how to design a system that is simple and maintainable. I suggest you to learn OOP Concepts, Design Patterns, and Refactoring

I am developing a game in java.For my game i designed a level.Now i want to design another level for my game.It will be really helpful for my new level if i can extend my previous level.
In game i am doing stage change in various level.For that i have to extend Application class.As java do not support multiple inheritance, is there any other way to going different level without doing stage change?

Stage Change:

    //LevelOne selection
    levelOneIm = new ImageView();
    levelOne = new Image("minion/image/levelOne.jpeg");
    levelOneIm.setImage(levelOne);
    levelOneIm.setTranslateX(300);
    levelOneIm.setTranslateY(200);
    levelOneIm.setOnMouseClicked(e -> { 
        popUp.show();
        popUpOk.setOnAction(ab -> {
            try {
                userName = popUpUserName.getText();
            } catch(Exception a){
                System.out.println("Exception handled");
            }     
            //Going to Main Game and popup stage closing
            Minion minion = new Minion();
            popUp.close();
            minion.gameRun = true;
            minion.start(stage);//Level1 stage change
    });
    });

    //LevelTwo selection
    levelTwoIm = new ImageView();
    levelTwo = new Image("minion/image/levelTwo.jpg");
    levelTwoIm.setImage(levelTwo);
    levelTwoIm.setTranslateX(500);
    levelTwoIm.setTranslateY(200);
    levelTwoIm.setOnMouseClicked(e -> { 
        popUp.show();
        popUpOk.setOnAction(ab -> {
            try {
                userName = popUpUserName.getText();
            } catch(Exception a){
                System.out.println("Exception handled");
            }     
            //Going to LevelTwo and popup stage closing
            LevelTwo levelTwo= new LevelTwo();
            popUp.close();
            levelTwo.gameRun = true;
            levelTwo.start(stage);
    });
    });     

Level1:

public class Minion extends Application implements Runnable{
    private Group root;
    private StackPane stackpane;
    private Scene mainbgscene;
    private ImageView mainbgv,heartIm1,heartIm2,heartIm3,characterIm,obsIm,obsIm2,obsIm3,obsIm4,obsIm5,obsIm6,obsIm7;
    private Image mainbg,heart1,character,obs,obs2,obs3,obs4,obs5,obs6,obs7;
    private VBox mainvbox,scoreVbox,characterVbox,obsVbox;
    private HBox heartHbox,scoreHbox;
    private Stage stageContainer;
    private Label scoreLabel;
    private Rectangle scoreBox;

Level2:

public class LevelTwo extends Application implements Runnable{
    private Group root;
    private StackPane stackpane;
    private Scene mainbgscene;
    private ImageView mainbgv,heartIm1,heartIm2,heartIm3,characterIm,obsIm,obsIm2,obsIm3,obsIm4,obsIm5,obsIm6,obsIm7;
    private Image mainbg,heart1,character,obs,obs2,obs3,obs4,obs5,obs6,obs7;
    private VBox mainvbox,scoreVbox,characterVbox,obsVbox;
    private HBox heartHbox,scoreHbox;
    private Stage stageContainer;
    private Label scoreLabel;
    private Rectangle scoreBox;
    private Runnable r1;
    private int characterBoundary,life;
    private boolean flag1 =true;
    private boolean obf1,obf2,obf3,obf4,obf5,obf6,obf7;
    private double characterX,characterY;
    public static int score,lastScore;

Change your design. Refactor your application to utilize the Single Responsibility Principle. The application should not be responsible for implementing logic for a level, nor should it be implementing Runnable. Your application class should be responsible only for implementing the JavaFX application lifecycle, as defined in the JavaFX application javadoc. In fact, your application should be a really small class.

Instead, tasks such as implementing a game level should be delegated to other classes that handle those things. Favoring composition over inheritance can make it easier for you to associate different functional attribute with items rather than sticking to a strict and sometimes inflexible hierarchy, which can suffer from issues and restrictions such as the diamond problem.

For solution to problems which might seem like they are best suited for solving via multiple inheritance, you could try a twin pattern, which is really just a form of delegation as previously recommended in this post.

To understand how to go about refactoring your program, study up on object oriented design principles and techniques, such as responsibility driven design and crc modeling, then validate that your design fits together on a run-time basis using some simple sequence diagrams. As far a book recommendation for learning this stuff, perhaps pick up a copy of Object Design: Roles, Responsibilities, and Collaborations