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