Tải bản đầy đủ (.pdf) (27 trang)

Software design: Lecture 34 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (490.26 KB, 27 trang )

1

Software Design
Lecture : 34


2

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.


3

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 



4

Adapter or Wrapper Design 
Pattern


5

Adapter or Wrapper  Design 
Pattern
Motivation 
Laptop power supply to AC power supply 
Example


6

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.


7

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.


8

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.


9


10


11


12



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  Non­Compatible 
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 i­e 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



×