Advertisement



< Prev
Next >



Coupling and Cohesion in Java





Coupling


In order to create an effective application that is easy to develop, easy to maintain and easy to update, we need to know the concept of coupling and cohesion. Coupling refers to the extent to which a class knows about the other class.







A good application design is creating an application with high cohesive classes, which are targeted towards a specific specialized task and such classes are not only easy to create but also easy to maintain and update.

//Java - Example of high cohesion classes
 
class PlayerDatabase
{
	ConnectDatabase connectD= new connectDatabase();
	PrintAllPlayersInfo allPlayer= new PrintAllPlayersInfo();
	PrintRankings rankings = new PrintRankings();
	CloseDatabase closeD= new CloseDatabase();
	PrintSinglePlayerInfo singlePlayer = PrintSinglePlayerInfo();
}



class ConnectDatabase
{
	//connecting to database.
}


class CloseDatabase
{
	//closing the database connection.
}


class PrintRankings
{
	//printing the players current rankings.
}


class PrintAllPlayersInfo
{
	//printing all the players information.
}


class PrintSinglePlayerInfo
{
	//printing a single player information.
}


Program Analysis


Unlike the previous program, where we had a single class performing many different tasks, here we have created several different classes, where each class is performing a specific specialized task, which leads to an easy creation, maintenance and modification of these classes.

Classes created by following this programming design are said to performing a cohesive role and are termed as high cohesion classes, which is an appropriate programming design to follow while creating an application.



Please share this article -





< Prev
Next >
< Constructors in Java
Stack and Heap >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement