SERVING THE QUANTITATIVE FINANCE COMMUNITY

  • 1
  • 2
  • 3
  • 4
  • 5
  • 15
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 27th, 2010, 7:30 pm

We have squares S and rectangle R in maths and CAD. How to model them correctly and hopefully be reusable: a. S ISA R (aka inheritance) b R ISA S c R HASA S (aka wrapper/composition) d S HASA R e develop S and R independently of each other; they may look the same but.. choices X and Y lead to the best solution; The others are an accident/nightmare waiting to happen. The other gem is Circle, Ellipse and Arc (a piece of a C or a E). A nice trick to do write code for Circle (easy, intersections etc.). Now, for an Ellipse, transform it to a Circle, get your answer and convert back. Very elegant and completely reusable. Question; What are X and Y?
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

Object Design Quiz

September 27th, 2010, 8:34 pm

i propose you create a class called Quadrilateral with 4 coordinates
knowledge comes, wisdom lingers
 
User avatar
Hansi
Posts: 3300
Joined: January 25th, 2010, 11:47 am

Object Design Quiz

September 27th, 2010, 8:47 pm

My though process went A then C and then E just because it's there wouldn't have bothered with it otherwise. I say X and Y are A and C.
 
User avatar
renorm
Posts: 886
Joined: February 11th, 2010, 10:20 pm

Object Design Quiz

September 27th, 2010, 10:44 pm

Use Occam's razor. Refactoring for the sake of refactoring isn't refactoring. Look at the whole picture. Sub-typing may not be the right approach to refactoring.
Last edited by renorm on September 27th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
KaiRu
Posts: 6
Joined: August 13th, 2010, 5:06 am

Object Design Quiz

September 28th, 2010, 2:05 am

QuoteOriginally posted by: Cuchulainn How to model them correctly and hopefully be reusable: Make them both immutable to prevent situation when change of height assumes the change of width (postcondition for square and not a postcondition for rectangle). Another option is to do "E" and hide them behind certain interface (with methods to calculateArea() or calculatePerimeter() etc.) if I want to operate with them in a same way but without methods that allow to modify internal state.
Last edited by KaiRu on September 27th, 2010, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 28th, 2010, 5:23 am

Just to scope the problem for the moment:the sides of a rectangle and a square are parallel to the x and y axes.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 28th, 2010, 5:25 am

QuoteMy though process went A then C and then E just because it's there wouldn't have bothered with it otherwise. I say X and Y are A and C.a-->Rect has members to set width and length; these functions are inherited by Square.. oopsSquare is not a specialisation of Rectangle when viewed by its behaviour but on a structural level it 'seems' to be similar. Choice a is scary. Choice c => Rectangle delegates to Square's interface. What has Square that Rectangle needs, unless a change of coordinates and call Square's mehods?QuoteMake them both immutable to prevent situation when change of height assumes the change of width (postcondition for square and not a postcondition for rectangle). Of course, it is possible to talk to a Square that only knows 'size'; height and width would just N/A unless Square is related to Rectangle in some way. QuoteUse Occam's razor. Refactoring for the sake of refactoring isn't refactoring. Look at the whole picture. Sub-typing may not be the right approach to refactoring. Refactoring is a programming trick. What we are really talking about is (semantic) modelling.
Last edited by Cuchulainn on September 27th, 2010, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

Object Design Quiz

September 28th, 2010, 2:12 pm

QuoteOriginally posted by: CuchulainnSquare is not a specialisation of Rectangle when viewed by its behaviour but on a structural level it 'seems' to be similar. Choice a is scary. this is nonsense.
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 28th, 2010, 2:34 pm

QuoteOriginally posted by: quantmehQuoteOriginally posted by: CuchulainnSquare is not a specialisation of Rectangle when viewed by its behaviour but on a structural level it 'seems' to be similar. Choice a is scary. this is nonsense.Not very scientific answer OK here is an interface; all implementation details are relegated to classes and not the superordinate interface. Hai capito?All Java people should appreciate this.
Last edited by Cuchulainn on September 27th, 2010, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
Jim
Posts: 340
Joined: February 1st, 2002, 5:20 pm

Object Design Quiz

September 28th, 2010, 3:00 pm

The issue is that you cannot have a child class restrict the general properties of a parent class. It can extend the properties, but not restrict them. See Liskov Substitution Principle.
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 28th, 2010, 6:41 pm

QuoteOriginally posted by: JimThe issue is that you cannot have a child class restrict the general properties of a parent class. It can extend the properties, but not restrict them. See Liskov Substitution Principle.Example ?class Rectangle{// set stuff; W, H};class Square : public Rectangle{};Inheritance not good here?
Last edited by Cuchulainn on September 27th, 2010, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

Object Design Quiz

September 28th, 2010, 7:00 pm

QuoteOriginally posted by: CuchulainnAll Java people should appreciate this.you mean 'I' prefixes in front interfaces?there is not a single way to classify/categorize entities. for instance, if I'm psudo theor physicist (aka cosmologist), then I could look at the symmetry group of a rectangle and square. they're D2 and D4 respectively. you can't flip the rectangle over diagonals or turn 90 degrees, but you can flip horizontally or turn 180 degrees. they belong to isometric symmetry group 'hierarchy'you could also have a regular polynomial class to model all of them at once, then rectangular won't be there at all. RegPol : Pol list<double> side;RegPol can be a subtype of Polynomial, to which rectangle belongs to. this way rectangle is an instance of Pol, but it's not a base type of square, which is also not a type but an isntance of RegPol
 
User avatar
Jim
Posts: 340
Joined: February 1st, 2002, 5:20 pm

Object Design Quiz

September 28th, 2010, 9:23 pm

> Inheritance not good here?Not if there are Rectangle class methods which are incompatible with the methods/assumptions in Square. You can often reorganize the inheritance hierarchy to put the common methods into a different common base class and separate the incompatiblities, like so:class RectangularShape{// get height, width, area}class Rectangle : public RectangularShape{// set height, width}class Square : public RectangularShape{// set size}
 
User avatar
quantmeh
Posts: 5974
Joined: April 6th, 2007, 1:39 pm

Object Design Quiz

September 28th, 2010, 9:58 pm

QuoteOriginally posted by: Jimclass Square : public RectangularShape{// set size}you're wasting a lot of space here. a square doesn't need width and height
 
User avatar
Cuchulainn
Topic Author
Posts: 61149
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Object Design Quiz

September 29th, 2010, 7:46 am

QuoteOriginally posted by: quantmehQuoteOriginally posted by: CuchulainnAll Java people should appreciate this.you mean 'I' prefixes in front interfaces?there is not a single way to classify/categorize entities. for instance, if I'm psudo theor physicist (aka cosmologist), then I could look at the symmetry group of a rectangle and square. they're D2 and D4 respectively. you can't flip the rectangle over diagonals or turn 90 degrees, but you can flip horizontally or turn 180 degrees. they belong to isometric symmetry group 'hierarchy'you could also have a regular polynomial class to model all of them at once, then rectangular won't be there at all. RegPol : Pol list<double> side;RegPol can be a subtype of Polynomial, to which rectangle belongs to. this way rectangle is an instance of Pol, but it's not a base type of square, which is also not a type but an isntance of RegPolWhat is being said here is that classes are context-sensitive and their behaviour is determined by their interaction. Even in graphical apps, a CAD designer and GUI widget maker view a Rectangle differently.What this is called is the Explanation based view in modelling; the other views are attribute-based (data-oriented).
Last edited by Cuchulainn on September 28th, 2010, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Every Time We Teach a Child Something, We Keep Him from Inventing It Himself
Jean Piaget
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On