java - Open/Closed Principle OO class design -


i'm trying figure out class design library operates on weighted graph. various algorithms may performed on graph, example, finding shortest distance between 2 nodes, longest distance between 2 nodes, number of paths of between 2 nodes distance less 10 (say), etc.

my concern not how implement algorithm or data structures graphs know how this, rather on overall high-level class design. point being in future may want add other algorithms, solution should extensible. 1 option implementing write single class has methods implementing each of these algorithms. in future additional methods can added class new algorithms.

public class graphcalculator {     graph _graph;     public int getlongestdistance(string startplacename, string endplacename)     {      }     public int getshortestdistance(string startplacename, string endplacename)     {      }     public int getnumberofpaths(int minimumdistance)     {      }     //any new algorithms implemented new methods added class } 

my concern violates solid open/closed principle. should each algorithm instead implemented in own class? if so, recommended class structure achieve this, loosely coupled , testable, , how called public api layer? there recommended design patterns this?

the answer question should each algorithm instead implemented in own class yes! stating, want easily extensible solution. single class has methods implementing each of these algorithms. in future additional methods can added class new algorithms. not extensible @ all! changing code , need modify current base implementation! opposite of oop principle - closed modification, open extension!

every single algorithm have implement (at present or in future) behaviour , should defined using interface. implementations should implement common interface. way able test every single algorithm implementation on own. allows define 1 list of algorithms, maintained dynamically (via code or configuration). considering this, need kind of plug-in architecture.

one design pattern matches need visitor pattern, since adds new operations (= algorithms shortest path, longest path, etc.) existing data structures (graph object).

another option plugin design pattern, although imo pattern more challenging implement visitor. if alright use 3th party software , existing frameworks, have @ sprint plugin project, uses spring framework , defines pluggable architecture helper. (somewhat) similar solution .net managed extensibility framework and/or enterprise library - unity application block.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -