Software design - Lecture 34. The main topics covered in this chapter include: structural design patterns; adapter or wrapper design pattern; laptop power supply to AC power supply Example; reasons for incompatibility; adapter pattern defined; issues in adapter design pattern;...
1 Software Design Lecture : 34 Structural Design Patterns They deal with how classes and objects deal with to form large structures Structural Design patterns use inheritance to compose interfaces or implementations Structural Design Patterns basically ease the design by identifying the relationships between entities Structural Design Patterns Deal with objects delegating responsibilities to other objects. This behavior results in a layered architecture of components with low degree of coupling Facilitate interobject communication when one object is not accessible to the other by normal means or when an Adapter or Wrapper Design Pattern Adapter or Wrapper Design Pattern Motivation Laptop power supply to AC power supply Example Adapter Design Pattern Clients of a class access the services offered by the class through its interface Sometimes, an existing class may provide the functionality required by a client, but its interface may not be what the client expects Reasons for Incompatibility This could happen due to various reasons such as the existing interface may be too detailed, or it may lack in detail, or the terminology used by the interface may be different from what the client is looking for Goal Keeping the client code intact we need to write a new class which will make use of services offered by the class Convert the services offered by class to the client in such a way that functionality will not be changed and the class will become reusable as shown in next slide 10 13 Adapter Pattern Defined “Adapter pattern convert the interface of the class into a form what client expects. Adapter let the classes work together which couldn’t otherwise due to incompatible interfaces.” 14 Issues in Adapter Design Pattern Due to adapter class the changes are encapsulated within it and client is decoupled from the changes in the class 15 Applicability – When to use We want to use the existing class and its interface does not match with the one you need In case of reusable classes due NonCompatible interfaces it is not possible to reuse them 16 Class Diagram of Adapter Design Pattern 17 18 Flow of Application Client call operations on Adaptor instance, which in return call adaptee opertions that carry out the request 19 Type of Adapters Object Adapters (Discussed till now) Class Adapters 20 Class Diagram of Class Adapter 21 Example Data Structures in Java ie Array, Vector Enumerators in Java Enumerations 22 Enumerators in Java public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } 23 public class EnumTest { Day day; // defining Enumerator varaible public EnumTest(Day day) { this.day = day; } public void tellItLikeItIs() { switch (day) // accessing Enumerator Values { case MONDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better."); break; case SUNDAY: System.out.println("Weekends are best."); break; default: System.out.println("Midweek days are so so."); break; } } 24 public static void main(String[] args) {// Access values of Enumerator EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs(); EnumTest thirdDay = new EnumTest(Day.WEDNESDAY); thirdDay.tellItLikeItIs(); EnumTest fifthDay = new EnumTest(Day.FRIDAY); fifthDay.tellItLikeItIs(); EnumTest sixthDay = new EnumTest(Day.SATURDAY); sixthDay.tellItLikeItIs(); EnumTest seventhDay = new EnumTest(Day.SUNDAY); seventhDay.tellItLikeItIs(); }} 25 26 27