private void Start() { DMShpere shpere1 = new DMShpere(); DMShpere shpere2 = new DMShpere(); DMCube cube1 = new DMCube(); DMCube cube2 = new DMCube(); DMCube cube3 = new DMCube(); DMCylinder cylinder1 = new DMCylinder(); DMShapeContainer container = new DMShapeContainer(); container.AddShape(shpere1); container.AddShape(shpere2); container.AddShape(cube1); container.AddShape(cube2); container.AddShape(cube3); container.AddShape(cylinder1); AccountOfShapeVisitor shapeVisitor = new AccountOfShapeVisitor(); container.RunVisitor(shapeVisitor); Debug.Log("图形数量:" + shapeVisitor.account); AccountOfCubeVisitor cubeVisitor = new AccountOfCubeVisitor(); container.RunVisitor(cubeVisitor); Debug.Log("Cube数量:" + cubeVisitor.account); }
private void Start() { DMSphere sphere1 = new DMSphere(); DMCylinder cylinder1 = new DMCylinder(); DMCube cube1 = new DMCube(); DMCube cub2 = new DMCube(); DMShapeContainer container = new DMShapeContainer(); container.AddShape(sphere1); container.AddShape(cylinder1); container.AddShape(cube1); container.AddShape(cub2); //int count = container.GetShapeCount(); //需求扩展,不符合开闭原则 //int cubeCount=container.GetCubeCount() //需求扩展,不符合开闭原则 //符合开闭原则 AmountVisitor amountVisitor = new AmountVisitor(); container.RunVisitor(amountVisitor); print("图形数量:" + amountVisitor.amount); CubeAmountVisitor cubeAmount = new CubeAmountVisitor(); container.RunVisitor(cubeAmount); print("cube数量:" + cubeAmount.amount); EdgeVisitor edgeVisitor = new EdgeVisitor(); container.RunVisitor(edgeVisitor); print("edge数量:" + edgeVisitor.count); }
private void Start() { DMCube cube = new DMCube(); DMShpere shpere = new DMShpere(); DMShapeContainer container = new DMShapeContainer(); container.AddShape(cube); container.AddShape(shpere); DMAmountVisitor visitor = new DMAmountVisitor(); container.RunVisitor(visitor); print(visitor.Amount); }
void Start() { DMShpere shpere1 = new DMShpere(); DMCylinder cylinder1 = new DMCylinder(); DMCube cube1 = new DMCube(); DMCube cube2 = new DMCube(); DMShapeContainer shapeContainer = new DMShapeContainer(); shapeContainer.AddShape(shpere1); shapeContainer.AddShape(cylinder1); shapeContainer.AddShape(cube1); shapeContainer.AddShape(cube2); AmountVisitor amountVisitor = new AmountVisitor(); shapeContainer.RunVisitor(amountVisitor); Debug.Log(amountVisitor.amount); }
void Start() { DMShpere shpere1 = new DMShpere(); DMCylinder cylinder1 = new DMCylinder(); DMCube cube1 = new DMCube(); DMCube cube2 = new DMCube(); DMShapeContainer container = new DMShapeContainer(); container.AddShape(shpere1); container.AddShape(cylinder1); container.AddShape(cube1); container.AddShape(cube2); //int count = container.GetShapeCount(); //int cubeCount = container.GetCubeCount(); AmountVisitor amountVisitor = new AmountVisitor(); container.RunVisitor(amountVisitor); int amount = amountVisitor.amount; Debug.Log("图形总数:" + amount); CubeAmountVisitor cubeAmountVisitor = new CubeAmountVisitor(); container.RunVisitor(cubeAmountVisitor); int cubeAmount = cubeAmountVisitor.amount; Debug.Log("Cube总数:" + cubeAmount); EdgeVisitor edgeVisitor = new EdgeVisitor(); container.RunVisitor(edgeVisitor); int edgeAmount = edgeVisitor.amount; Debug.Log("边总数:" + edgeAmount); }
// Use this for initialization void Start() { DMSphere sphere1 = new DMSphere(); DMCylinder cylinder1 = new DMCylinder(); DMCube cube1 = new DMCube(); DMCube cube2 = new DMCube(); DMShapeContainer container = new DMShapeContainer(); container.AddShape(sphere1); container.AddShape(cylinder1); container.AddShape(cube1); container.AddShape(cube2); // int count = container.GetShapeCount(); // int cubeCOunt = container.GetCubeCount(); AmountVisitor amountVisitor = new AmountVisitor(); container.RunVisitor(amountVisitor); int amount = amountVisitor.amount; CubeAmountVisitor cubeAmountVisitor = new CubeAmountVisitor(); container.RunVisitor(cubeAmountVisitor); int cubeAmount = cubeAmountVisitor.amount; }
public override void VisitCube(DMCube cube) { amount += 12; }
public abstract void VisitCube(DMCube cube);
public override void VisitCube(DMCube cube) { //count += cube.edges; 有12 count += 12; }
public abstract void VisitCylinder(DMCylinder cylinder); //获取cylinder对象的数据 public abstract void VisitCube(DMCube cube); //获取cube对象的数据
public override void VisitCube(DMCube sphere) { amount += 30; }
public abstract void VisitCube(DMCube sphere);
public override void VisitCube(DMCube cube) { account++; }