Learner Learning Software Engineering
Posts with the tag Design Patterns:

Adapter Design Pattern

This design patterns guides to create a wrap around existing class to reuse existing class for a new requirement. Assume, I have two logging classes named WriteLogsToLocalFile and WriteLogsToServer. class WriteLogsToLocalFile { public void Log(string fileName, string message) { //write logs to file } } class WriteLogsToServer { public void Log(string urL, string message, string token) { //call rest API with oAuth // Note: here token is used for oAuth } } static void Main(string[] args) { WriteLogsToLocalFile fileLogs = new WriteLogsToLocalFile(); fileLogs.Log("hello.xml", "login success"); WriteLogsToServer serverLogs = new WriteLogsToServer(); serverLogs.Log("https://server.logs/upload", "login success", "oAuthToken"); } Here user is manually creating objects to write logs separately to local file and server.

Composite Design Pattern

The Composite Design pattern is structural design pattern. This design pattern is used when we want to treat a group of objects in same way. The composite design pattern has following elements. Component This is a class that contain all the members that needs to be implemented by all the objects. Composite This class is used to add, remove and traverse components. Leaf The leaf object in tree structure is defined as leaf element. Code Sample I want to print salaries of all employees in my organization with names and designation. The root of organization tree is CEO. The CEO has directors as child composite.