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); }
// 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 VisitSphere(DMSphere sphere) { //count += sphere.edges;假设有1 count += 1; }
public override void VisitSphere(DMSphere sphere) { return; }
public override void VisitSphere(DMSphere sphere) { amount++; }
public abstract void VisitSphere(DMSphere sphere); //获取sphere对象的数据
public abstract void VisitSphere(DMSphere sphere);