1. Trang chủ
  2. » Giáo án - Bài giảng

Adapter pattern - Dr Neal

12 293 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 12
Dung lượng 107 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

However you have decided to use a third party class that will give you more flexibility.. the third party XYZ class have different names and parameters.. class with as little changes a

Trang 1

Adapter Pattern

Dr Neal CIS 480

Trang 2

internal class However you have decided

to use a third party class that will give you more flexibility

the third party XYZ class have different

names and parameters.

class with as little changes as possible.

Trang 3

Solution is to Use an Adapter

existing method calls to the present

calculation class.

interface so that no method calls will be broken

accept the old calls and make the new

calls to XYZ.

Trang 4

Design

Classes

BaseApp

Main() doPayroll()

(from Console Interface)

CalculatePay

hourly : double fringe : double[]

CalculatePay() calculateHourly() calculateSalary()

(from Business Services)

ICalculate

calculateHourly() calculateSalary()

(from Business Services)

<<Interface>>

CalculateAdapter

calculateHourly() calculateSalary()

(from Business Services)

XYZPayCalculator hourly : double

XYZPayCalculator() performHourlyPayCalculation() performSalaryiedPayCalculation()

(from Business Services)

instantiates

instantiates

instantiates

Current

System

XYZ System

Trang 5

Sequence for Existing System

1: CalculatePay(double) 2: calculateHourly(double) 3: calculateSalary(int, double)

Trang 6

Sequence for Use of Adapter

CalculateA

: CalculateA

: XYZPayCal

: XYZPayCal

1: CalculateAdapter( ) 2: calculateHourly(double)

3: XYZPayCalculator( ) 4: performHourlyPayCalculation(double) 5: calculateSalary(int, double)

6: XYZPayCalculator( ) 7: performSalaryiedPayCalculation(double)

8: doPayroll( )

Trang 7

Output to Console

Trang 8

namespace AdapterPattern

{

/// <summary>

/// Dr Neal /// CIS 480 /// Example of Adapter pattern use.

/// </summary>

class BaseApp {

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args) {

BaseApp ba = new BaseApp();

ba.doPayroll();

} private void doPayroll() {

// the only change in the application is to use the CalculateAdapter // rather than the CalculatePay class in use currently

//

// CalculatePay cp = new CalculatePay(10.75);

CalculateAdapter cp = new CalculateAdapter();

Console.WriteLine("Hourly Payroll");

Console.WriteLine("George Applegate " + cp.calculateHourly(40.0).ToString()); Console.WriteLine("Jim Bonner " + cp.calculateHourly(42.0).ToString()); Console.WriteLine(" ");

Console.WriteLine("Salaried Payroll");

Console.WriteLine("Don Ho " + cp.calculateSalary(1, 1278.0).ToString()); Console.WriteLine("Steve Smith " + cp.calculateSalary(2, 1634.23).ToString()); Console.WriteLine(" ");

} }

}

BaseApp Class

Only

change one

line

to

implement

XYZ

Trang 9

namespace AdapterPattern

{

/// <summary>

/// Dr Neal /// CIS 480 /// Example of Adapter pattern use.

/// </summary>

public class CalculatePay : ICalculate {

private double hourly;

private double[] fringe;

public CalculatePay(double h) {

hourly = h;

fringe = new double[3];

fringe[0] = 1;

fringe[1] = 15;

fringe[2] = 2;

}

public double calculateHourly(double hours) {

return (hours * hourly);

}

public double calculateSalary(int category, double salary) {

return (salary * (1 + fringe[category]));

CalculatePay Class

Trang 10

namespace AdapterPattern

{

/// <summary>

/// Dr Neal /// CIS 480 /// Example of Adapter pattern use.

/// </summary>

public interface ICalculate {

double calculateHourly(double hours);

double calculateSalary(int category, double salary);

} }

ICalculate Interface

Trang 11

namespace AdapterPattern

{

/// <summary>

/// Dr Neal /// CIS 480 /// Example of Adapter pattern use.

/// </summary>

public class CalculateAdapter : ICalculate {

public CalculateAdapter() {

}

public double calculateHourly(double hours) {

XYZPayCalculator xyz = new XYZPayCalculator(); return xyz.performHourlyPayCalculation(hours); }

public double calculateSalary(int category, double salary) {

XYZPayCalculator xyz = new XYZPayCalculator(); return xyz.performSalaryiedPayCalculation(salary); }

} }

CalculateAdapter Class

Trang 12

namespace AdapterPattern

{

/// <summary>

/// Dr Neal /// CIS 480 /// Example of Adapter pattern use.

/// </summary>

public class XYZPayCalculator {

private double hourly;

public XYZPayCalculator() {

hourly = 10.75;

}

public double performHourlyPayCalculation(double hours) {

return (hours * hourly);

}

public double performSalaryiedPayCalculation(double salary) {

double fringe;

if (salary < 1500.00)

fringe = 15;

else

fringe = 20;

return (salary * (1 + fringe));

}

XYZPayCalculator Class

Ngày đăng: 19/03/2014, 22:09

w