1. 程式人生 > 其它 >c#設計模式-建造者

c#設計模式-建造者

1.建造者模式

將一個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示。建造者模式使得建造程式碼與表示程式碼的分離,可以使客戶端不必知道產品內部組成的細節,從而降低了客戶端與具體產品之間的耦合度。

2 案例

 --自行車類 
   public class Bike
    {
        public FrameBolt frameBolt { get; set; }  
        public Seat seat { get; set; }  
        public Tire tire { get; set; }
    }
--細節
    public
class FrameBolt { } --建造者 public class BikeBuilder { private Bike bike = new Bike(); public void BuilderFrameBolt() { this.bike.frameBolt = new FrameBolt(); } public void BuilderSeat() { this.bike.seat = new Seat(); }
public void BuilderTire() { this.bike.tire = new Tire(); } public Bike GetBike() { return this.bike; } }