Frage

Based on this question I think while the aim of OOP is not necessarily to copy real world objects, they could be source of inspiration to design a software or solve a problem or even learn the OOP itself.

For example for an application which deals with drawing, a Bitmap could be regarded like a Page, then as a page has back color, we can define a BackColor attribute for it. But we may realize a real page has not text color and it's the attribute of the pen, so we can define a class named Pen which is used for drawing and has a Color, LineWeight or ..

I always look for such analogy and inspirations of objects and their interactions when I develop a software, but I don't have a systematic or clear definition for what I am doing. I don't know which aspects of OOP I am copying.

Is it an acceptable technique in OOP? Do you use it? Is it possible that I violate an OOP guideline while I am using this technique?

I would like to know how this technique may help me to follow OO principles, so I can consciously use it.

War es hilfreich?

Lösung

You're conflating multiple things when you over analyse the problem.

For example, when designing a page object that reflects a real-world sheet of paper you start to consider pens. Don't!

Take just the sheet of paper and consider its properties. It has a paper colour, texture, size. Then do not try to map your preconceptions of computing terms to the object. A sheet of paper does not have a 'backcolour'. It has a colour, of the dye or material used in the manufacture of the paper. This is different to any concept of drawing using a pen that has 2 inks (assuming a very unusual pen that draws both a wide underlay and a ink nib).

Its important to try not to 'map' your concepts to the item. and simply look at the entity as an independant thing. consider what only it does, not what can be done to it or any other item. Then you'll find the properties fall into place, and you can move onto the next item, eg a pen and what properties it has.

When considering the object, also forget the methods you'll use to modify it. Start with the properties only. Methods are added as ways to interact and modify those properties so they are not really important in the design stage. eg. your paper can hold writing on it, so its property is that it can maintain text blocks in particular positions. You'll add a method to add new writing to it but when designing its the writing held by the paper that is much more important than how that writing gets written.

Hopefully this'll help clear your thoughts, design can be a bit vague and loose at the best of times!

Lizenziert unter: CC-BY-SA mit Zuschreibung
scroll top