peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikatukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Adapter Design Pattern trong C# - Cách triển khai và ví dụ
CĂN BẢN
NÂNG CAO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Adapter Design Pattern trong C# - Cách triển khai và ví dụ

Trong bài viết này mình sẽ giới thiệu đến các bạn Adapter Design Pattern trong C# - Cách triển khai và ví dụ. Nó thuộc nhóm Structural Design Pattern trong C#.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Chúng ta sẽ cùng nhau tìm hiểu về Adapter Design Pattern là gì? Cách triển khai nó trong C# và một vài ví dụ thực tế.

Adapter Design Pattern trong C# là gì?

Adapter Design Pattern hoạt động như một cầu nối giữa hai giao diện không tương thích. Pattern này liên quan đến một class duy nhất được gọi là adapter, chịu trách nhiệm giao tiếp giữa hai giao diện độc lập.

Vì vậy, nói một cách đơn giản, Adapter Design Pattern giúp hai giao diện không tương thích hoạt động cùng nhau.

Bài viết này được đăng tại [free tuts .net]

adapter 01 png

Adapter Design Pattern bao gồm 4 thành phần như sau:

  • Client: Làm việc trực tiếp qua Target interface.
  • Target: Đây là một interface và nó cần được thực thi bởi Adapter. Chỉ có Client có thể thấy interface này.
  • Adapter: Có chức năng kết nối hai hệ thống không tương thích hoạt động cùng nhau.
  • Adaptee: Class này chứa chức năng mà ứng dụng yêu cầu, nhưng nó không tương thích với code client hiện có. Vì vây, nó yêu cầu một số điều chỉnh trước khi code client có thể sử dụng nó.

Ưu điểm và nhược điểm của Adapter Design Pattern trong C#

Trong phần này, mình sẽ đưa ra một số ưu điểm và nhược điểm trong quá trình sử dụng Adapter Design Pattern trong C#.

Ưu điểm của Adapter Desing Pattern:

  • Làm việc với adapter class mà không thay đổi dữ liệu bên trong adapter, điều này giúp thuận tiện cho việc mở rộng.
  • Có thể sử dụng adapter với các class con của adaptee.
  • Cho phép nhiều interface khác nhau giao tiếp với nhau

Nhược điểm của Adapter Design Pattern:

  • Tất cả các yêu cầu phải được thông qua adapter, điều này làm tốn thêm chi phí.
  • Làm tăng thêm độ phức tạp của code, vì phải tạo thêm interface và các class.
  • Dễ gây ra các exception.

Cách triển khai và ví dụ về Adapter Design Pattern trong C#

Trong phần này mình sẽ thực hiện triển khai Adapter Design Pattern thông qua một ví dụ dưới đây.

Bước 1: Tạo class Employee.

Trong class khởi tạo các thuộc tính bao gồm ID, Name, Designation, Salary. Và khởi tạo thêm một constructor Employee.

Employee.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AdapterDesignPattern
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }
        public decimal Salary { get; set; }
        public Employee(int id, string name, string designation, decimal salary)
        {
            ID = id;
            Name = name;
            Designation = designation;
            Salary = salary;
        }
    }
}

Bước 2: Tạo Adaptee.

Class này có phương thức ProcessSalary() lấy danh sách nhân viên làm tham số đầu vào và xử lý tiền lương của từng nhân viên.

ThirdPartyBillingSystem.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AdapterDesignPattern
{
    public class ThirdPartyBillingSystem
    {
        public void ProcessSalary(List<Employee> listEmployee)
        {
            foreach (Employee employee in listEmployee)
            {
                Console.WriteLine("Rs." + employee.Salary + " muc luong duoc ghi cho tai khoan cua " + employee.Name);
            }
        }
    }
}

Bước 3: Tạo Target interface.

Class này định nghĩa phương thức ProcessCompanySalary() sẽ được thực thi bởi adaper.

ITarget.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AdapterDesignPattern
{
    public interface ITarget
    {
        void ProcessCompanySalary(string[,] employeesArray);
    }
}

Bước 4: Tạo Adapter.

Class này triển khai Target interface và cung cấp việc triển khai cho phương thức ProcessCompanySalary().

Phương thức ProcessCompanySalary() nhận thông tin nhân viên dưới dạng một mảng chuỗi. Sau đó chuyển đổi mảng chuỗi thành danh sách nhân viên và gọi phương thức ProcessSalary().

EmployeeAdapter.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace AdapterDesignPattern
{
    public class EmployeeAdapter : ITarget
    {
        ThirdPartyBillingSystem thirdPartyBillingSystem = new ThirdPartyBillingSystem();

        public void ProcessCompanySalary(string[,] employeesArray)
        {
            string Id = null;
            string Name = null;
            string Designation = null;
            string Salary = null;
            List<Employee> listEmployee = new List<Employee>();
            for (int i = 0; i < employeesArray.GetLength(0); i++)
            {
                for (int j = 0; j < employeesArray.GetLength(1); j++)
                {
                    if (j == 0)
                    {
                        Id = employeesArray[i, j];
                    }
                    else if (j == 1)
                    {
                        Name = employeesArray[i, j];
                    }
                    else if (j == 2)
                    {
                        Designation = employeesArray[i, j];
                    }
                    else
                    {
                        Salary = employeesArray[i, j];
                    }
                }
                listEmployee.Add(new Employee(Convert.ToInt32(Id), Name, Designation, Convert.ToDecimal(Salary)));
            }
            Console.WriteLine("Adapter chuyen doi mang Employee thanh danh sach Employee");
            Console.WriteLine("Sau do uy quyen cho he thong ThirdPartyBillingSystem de xu ly tien luong cua nhan vien\n");
            thirdPartyBillingSystem.ProcessSalary(listEmployee);
        }
    }
}

Bước 5: Xử lý trên hàm Main()

Tạo một instance của class EmployeeAdapter và gọi phương thức ProcessCompanySalary() bằng cách chuyển mảng chuỗi làm đối số.

Main
using System;

namespace AdapterDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] employeesArray = new string[5, 4]
            {
                {"101","John","SE","10000"},
                {"102","Smith","SE","20000"},
                {"103","Dev","SSE","30000"},
                {"104","Pam","SE","40000"},
                {"105","Sara","SSE","50000"}
            };

            ITarget target = new EmployeeAdapter();
            Console.WriteLine("He thong nhan su chuyen chuoi employee sang adapter\n");
            target.ProcessCompanySalary(employeesArray);

            Console.Write("\n------------------------------\n");
            Console.WriteLine("Chuong trinh nay duoc dang tai Freetuts.net");
            Console.Read();
        }
    }
}

Kết quả:

adapter 02 png

Như vậy là chúng ta đã cùng nhau tìm hiểu về Adapter Dessign Pattern trong C# - Cách triển khai và ví dụ. Ở bài viết tiếp theo mình sẽ giới thiệu đến các bạn Facade Design Pattern trong C#, các bạn chú ý theo dõi nhé !!!

Cùng chuyên mục:

Memento Design Pattern trong C# - Cách triển khai và ví dụ

Memento Design Pattern trong C# - Cách triển khai và ví dụ

Interpreter Design Pattern trong C# - Cách triển khai và ví dụ

Interpreter Design Pattern trong C# - Cách triển khai và ví dụ

Strategy Design Pattern trong C# - Cách triển khai và ví dụ

Strategy Design Pattern trong C# - Cách triển khai và ví dụ

Visitor Design Pattern trong C# - Cách triển khai và ví dụ

Visitor Design Pattern trong C# - Cách triển khai và ví dụ

Command Design Pattern trong C# - Cách triển khai và ví dụ

Command Design Pattern trong C# - Cách triển khai và ví dụ

Observer Design Pattern trong C# - Cách triển khai và ví dụ

Observer Design Pattern trong C# - Cách triển khai và ví dụ

Iterator Design Pattern trong C# - Cách triển khai và ví dụ

Iterator Design Pattern trong C# - Cách triển khai và ví dụ

Proxy Design Pattern trong C# - Cách triển khai và ví dụ

Proxy Design Pattern trong C# - Cách triển khai và ví dụ

Composite Design Pattern trong C# - Cách triển khai và ví dụ

Composite Design Pattern trong C# - Cách triển khai và ví dụ

Bridge Design Pattern trong C# - Cách triển khai và ví dụ

Bridge Design Pattern trong C# - Cách triển khai và ví dụ

Decorator Design Pattern trong C# - Cách triển khai và ví dụ

Decorator Design Pattern trong C# - Cách triển khai và ví dụ

Facade Design Pattern trong C# - Cách triển khai và ví dụ

Facade Design Pattern trong C# - Cách triển khai và ví dụ

Singleton Design Pattern trong C# - Cách triển khai và ví dụ

Singleton Design Pattern trong C# - Cách triển khai và ví dụ

Prototype Design Pattern trong C# - Cách triển khai và ví dụ

Prototype Design Pattern trong C# - Cách triển khai và ví dụ

Builder Design Pattern trong C# - Cách triển khai và ví dụ

Builder Design Pattern trong C# - Cách triển khai và ví dụ

Tự viết ứng dụng quản lý xe ra vào C# Winforms và Firebase

Tự viết ứng dụng quản lý xe ra vào C# Winforms và Firebase

Trong bài viết này Freetuts chia sẽ cho các bạn một ứng dụng khá thú…

Factory Design Pattern trong C# - Cách triển khai và ví dụ

Factory Design Pattern trong C# - Cách triển khai và ví dụ

Chúng ta sẽ tìm hiểu về Factory Design Pattern trong C# là gì? Cách triển…

Hướng dẫn giải phương trình bậc hai trong C#

Hướng dẫn giải phương trình bậc hai trong C#

Giải phương trình bậc nhất ax + b = 0 trong C#

Giải phương trình bậc nhất ax + b = 0 trong C#

Cách nối hai mảng trong C# bằng hai cách khác nhau

Cách nối hai mảng trong C# bằng hai cách khác nhau

Top