第106章(1 / 1)

大话设计模式 吴强 631 字 2个月前

大B:“就我刚才跟你讲的汽车制造作为例子。现在我再用图形和代码具体和你讲讲。”

namespaceBuilder

……{

//车身

publicclassBody

……{

privatestringname;

publicstringName

……{

get……{returnname;}

set……{name=value;}

}

publicBody(stringname)

……{

this.name=name;

}

}

//引擎

publicclassEngine

……{

privatestringname;

publicstringName

……{

get……{returnname;}

set……{name=value;}

}

publicEngine(stringname)

……{

this.name=name;

}

}

//车轮

publicclassWheel

……{

privatestringname;

publicstringName

……{

get……{returnname;}

set……{name=value;}

}

publicWheel(stringname)

……{

this.name=name;

}

}

//Benz汽车

publicclassBenz

{

privateBodybody;

privateEngineengine;

privateIList《Wheel》wheels;

publicvoidAddBody(Bodybody)

{

this.body=body;

}

publicvoidAddEngine(Engineengine)

{

this.engine=engine;

}

publicvoidAddWheel(Wheelwheel)

{

if(wheels……null)

{

wheels=newList《Wheel》();

}

wheels.Add(wheel);

}

publicvoidShowMe()

{

if((this.body……null)||(this.engine……null)||(wheels……null))

{

Console.WriteLine(“ThiscarhasNOTbeenpletedyet!”);

}

Else

{

Console.WriteLine(“Thisisacarwitha”+body.Name+“anda”+engine.Name+);

Console.WriteLine(“Thiscarcontains”+wheels.Count+wheels:);

for(inti=0;i