-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Here is a description about implementing this program.
There are different implementation for determining the type of a triangle by passing the sides lengths. The simplest way is a static method that just check the rules for determining the type of triangle and return that, it is fast and efficient, but not scalable. For implementing a good and scalable code we should consider some assumption, which changing them will change the base of implementation.
If the sides can change after creating a triangle, the triangle have state instead of type and it should be change after it created. In this case we can use the State Design Pattern and each triangle type should be a state.
A multi-type triangle means that an equilateral triangle is an isosceles and scalene, and an isosceles triangle is a scalene. In this case we can use Inheritance. Equilateral extends Isosceles extends Scalene implements Triangle By inheritance every equilateral triangle also is an isosceles and scalene, and every isosceles triangle is a scalene.
In this implementation, I assumed that the triangle is immutable and a triangle have a single type that is the most specific type, and I use the Factory Design Pattern which a triangle only should be created with the factory, and the factory determined the type and create the related triangle. For TriangleFactory I use the static method for creating triangles, the other choice is using Singletone Pattern for factory class.