Nhà cung cấp trăn nhà máy

Trong lập trình dựa trên lớp, mẫu phương thức xuất xưởng là một mẫu tạo sử dụng các phương thức xuất xưởng để giải quyết vấn đề tạo đối tượng mà không cần phải chỉ định lớp chính xác của đối tượng sẽ được tạo. Điều này được thực hiện bằng cách tạo các đối tượng bằng cách gọi một phương thức xuất xưởng—hoặc được chỉ định trong một giao diện và được triển khai bởi các lớp con hoặc được triển khai trong một lớp cơ sở và được ghi đè tùy chọn bởi các lớp dẫn xuất—chứ không phải bằng cách gọi một hàm tạo

Tổng quan[sửa]

Mẫu thiết kế Factory Method [1] là một trong 23 mẫu thiết kế nổi tiếng mô tả cách giải quyết các vấn đề thiết kế định kỳ để thiết kế phần mềm hướng đối tượng linh hoạt và có thể tái sử dụng, tức là các đối tượng dễ triển khai, thay đổi,

Mẫu thiết kế Factory Method giải quyết các vấn đề như. [2]

  • Làm cách nào một đối tượng có thể được tạo để các lớp con có thể xác định lại lớp nào sẽ khởi tạo?
  • Làm cách nào một lớp có thể trì hoãn việc khởi tạo thành các lớp con?

Mẫu thiết kế Factory Method mô tả cách giải quyết các vấn đề đó

  • Xác định một thao tác riêng [phương thức xuất xưởng] để tạo một đối tượng
  • Tạo một đối tượng bằng cách gọi một phương thức xuất xưởng

Điều này cho phép viết các lớp con để thay đổi cách tạo đối tượng [để xác định lại lớp nào sẽ khởi tạo].
Xem thêm sơ đồ lớp UML bên dưới.

Định nghĩa[sửa]

"Xác định một giao diện để tạo một đối tượng, nhưng hãy để các lớp con quyết định lớp nào sẽ khởi tạo. Phương thức Factory cho phép một lớp trì hoãn việc khởi tạo mà nó sử dụng cho các lớp con. " [Băng nhóm bốn người]

Việc tạo một đối tượng thường yêu cầu các quy trình phức tạp không phù hợp để đưa vào một đối tượng soạn thảo. Việc tạo đối tượng có thể dẫn đến sự trùng lặp mã đáng kể, có thể yêu cầu thông tin mà đối tượng soạn thảo không thể truy cập được, có thể không cung cấp đủ mức độ trừu tượng hoặc có thể không phải là một phần trong mối quan tâm của đối tượng soạn thảo. Mẫu thiết kế phương thức xuất xưởng xử lý những vấn đề này bằng cách xác định một phương thức riêng biệt để tạo các đối tượng, sau đó các lớp con có thể ghi đè lên để chỉ định loại sản phẩm dẫn xuất sẽ được tạo

Mẫu phương thức xuất xưởng dựa trên tính kế thừa, vì việc tạo đối tượng được ủy quyền cho các lớp con triển khai phương thức xuất xưởng để tạo đối tượng. [3] Như được hiển thị trong ví dụ C# bên dưới, mẫu phương thức xuất xưởng cũng có thể dựa vào Giao diện - trong trường hợp này là IPerson - để được triển khai

Cấu trúc[sửa]

Sơ đồ lớp UML[sửa mã nguồn]

Sơ đồ lớp UML mẫu cho mẫu thiết kế Factory Method. [4]

Trong sơ đồ lớp UML ở trên, lớp

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 yêu cầu đối tượng
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
0 không khởi tạo trực tiếp lớp
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
1. Thay vào đó,
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 đề cập đến một
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
0 riêng biệt để tạo một đối tượng sản phẩm, điều này làm cho
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 không phụ thuộc vào lớp cụ thể nào được khởi tạo. Các lớp con của
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 có thể xác định lại lớp nào sẽ khởi tạo. Trong ví dụ này, lớp con
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
3 triển khai lớp trừu tượng
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
0 bằng cách khởi tạo lớp
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
1

Ví dụ[sửa]

Trò chơi mê cung có thể được chơi ở hai chế độ, một chế độ với các phòng thông thường chỉ được kết nối với các phòng liền kề và một chế độ với các phòng ma thuật cho phép người chơi được vận chuyển ngẫu nhiên.

Cấu trúc[sửa]

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
6 là lớp cơ sở cho sản phẩm cuối cùng [
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
7 hoặc
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
8].
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 tuyên bố phương pháp nhà máy trừu tượng để sản xuất một sản phẩm cơ sở như vậy.
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
7 và
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
8 là các lớp con của sản phẩm cơ sở triển khai sản phẩm cuối cùng.
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
2 và
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
3 là các lớp con của
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 thực hiện phương pháp nhà máy sản xuất các sản phẩm cuối cùng. Do đó, các phương thức xuất xưởng tách người gọi [
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9] khỏi việc triển khai các lớp cụ thể. Điều này làm cho Toán tử "mới" trở nên dư thừa, cho phép tuân thủ nguyên tắc Mở/đóng và làm cho sản phẩm cuối cùng linh hoạt hơn trong trường hợp thay đổi

Ví dụ triển khai[sửa]

C # [ chỉnh sửa ]

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
3

Trong đoạn mã trên, bạn có thể thấy việc tạo một giao diện có tên là

public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
6 và hai triển khai có tên là
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
7 và
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
8. Dựa trên loại được truyền vào đối tượng
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
9, chúng tôi sẽ trả lại đối tượng cụ thể ban đầu dưới dạng giao diện
public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
6

Phương thức xuất xưởng chỉ là phần bổ sung cho lớp

public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}
9. Nó tạo đối tượng của lớp thông qua các giao diện nhưng mặt khác, nó cũng cho phép lớp con quyết định lớp nào được khởi tạo

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}

Bạn có thể thấy chúng tôi đã sử dụng

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
12 trong ConcreteFactory. Kết quả là, bạn có thể dễ dàng gọi
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
13 từ nó để lấy
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
14. Bạn cũng có thể viết logic tùy chỉnh của mình sau khi lấy đối tượng trong Phương thức nhà máy cụ thể. GetObject được tạo trừu tượng trong giao diện Factory

Java[sửa]

Ví dụ về Java này tương tự như ví dụ trong cuốn sách Design Patterns

MazeGame sử dụng Phòng nhưng nó đặt trách nhiệm tạo Phòng cho các lớp con của nó để tạo ra các lớp cụ thể. Chế độ trò chơi thông thường có thể sử dụng phương pháp mẫu này

public abstract class Room {
    abstract void connect[Room room];
}

public class MagicRoom extends Room {
    public void connect[Room room] {}
}

public class OrdinaryRoom extends Room {
    public void connect[Room room] {}
}

public abstract class MazeGame {
     private final List rooms = new ArrayList[];

     public MazeGame[] {
          Room room1 = makeRoom[];
          Room room2 = makeRoom[];
          room1.connect[room2];
          rooms.add[room1];
          rooms.add[room2];
     }

     abstract protected Room makeRoom[];
}

Trong đoạn mã trên, hàm tạo

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
9 là một phương thức mẫu tạo ra một số logic chung. Nó đề cập đến phương pháp nhà máy
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
16 đóng gói việc tạo các phòng sao cho các phòng khác có thể được sử dụng trong một lớp con. Để triển khai chế độ trò chơi khác có phòng ma thuật, chỉ cần ghi đè phương thức
public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
16 là đủ

public interface IProduct
{
    string GetName[];
    bool SetPrice[double price];
}

public class Phone : IProduct
{
    private double _price;

    public string GetName[]
    {
        return "Apple TouchPad";
    }

    public bool SetPrice[double price]
    {
        _price = price;
        return true;
    }
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
    protected abstract IProduct MakeProduct[];

    public IProduct GetObject[] // Implementation of Factory Method.
    {
        return this.MakeProduct[];
    }
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
    protected override IProduct MakeProduct[]
    {
        IProduct product = new Phone[];
        // Do something with the object after you get the object.
        product.SetPrice[20.30];
        return product;
    }
}
1

PHP [ chỉnh sửa ]

Một ví dụ khác trong PHP sau đây, lần này sử dụng triển khai giao diện trái ngược với phân lớp [tuy nhiên, điều tương tự có thể đạt được thông qua phân lớp]. Điều quan trọng cần lưu ý là phương thức xuất xưởng cũng có thể được định nghĩa là công khai và được gọi trực tiếp bởi mã máy khách [ngược lại với ví dụ Java ở trên]

Nhà cung cấp trong Python là gì?

Nhà cung cấp giúp tập hợp các đối tượng . Họ tạo các đối tượng và đưa vào các phụ thuộc. Mỗi nhà cung cấp là một callable. Bạn gọi trình cung cấp giống như một chức năng khi bạn cần tạo một đối tượng. Nhà cung cấp truy xuất các phụ thuộc cơ bản và đưa chúng vào đối tượng đã tạo.

Nhà cung cấp nhà máy là gì?

Đối số đầu tiên của trình cung cấp Factory là một lớp, hàm gốc hoặc phương thức tạo đối tượng . Phần còn lại của các đối số vị trí và từ khóa của Nhà máy là các phần phụ thuộc. Nhà máy tiêm các phụ thuộc mỗi khi tạo một đối tượng mới.

Làm cách nào để triển khai phép nội xạ phụ thuộc trong Python?

Dependency Injector giúp lắp ráp và thêm các phần phụ thuộc. Nó cung cấp một thùng chứa và các trình cung cấp giúp bạn lắp ráp các đối tượng. Khi bạn cần một đối tượng, bạn đặt dấu Cung cấp làm giá trị mặc định của đối số hàm. Khi bạn gọi hàm này, khung sẽ lắp ráp và thêm phần phụ thuộc .

Khung tiêm phụ thuộc là gì?

Tiêm phụ thuộc vào. NET là một phần tích hợp sẵn của khung, cùng với cấu hình, ghi nhật ký và mẫu tùy chọn . Một phụ thuộc là một đối tượng mà một đối tượng khác phụ thuộc vào. Kiểm tra lớp MessageWriter sau bằng phương thức Viết mà các lớp khác phụ thuộc vào. Sao chép C#.

Chủ Đề