强制某人仅使用工厂创建对象

时间:2019-01-09 00:29:02

标签: java design-patterns factory

我有一个class ConventionalNameTest < ActiveSupport::TestCase class ContextTest < self # so much stuff... end class AnotherContextTest < self # and some more... end end 接口,并具有实现Shape接口的CircleTriangle类。可以说方法是Shape,它只是打印形状的类型。

现在有一个工厂类,即基于工厂设计模式的printShape(),它提供ShapeFactoryCircle对象。

现在,我想强制所有人使用Triangle创建ShapeFactoryCircle的对象。

如果某人不了解Triangle,那么他/她可以使用ShapeFactory创建对象而无需使用new。我希望没有人可以创建像Shapefacoty这样的对象。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

好吧,如果您不希望任何人能够使用您自己的软件包中的类以外的类,那么只需将这些类设置为 package-private >。

IF ,您甚至要禁止与ShapeFactory相同的包中的类使用实现Shape接口的类,然后跳过第一个示例并转到< em> UPDATE 报价。

请看下面的示例,其中展示了软件包的用法。

package Shapes;

class Circle implements Shape{
//implementation
}

package Shapes; 

class Triangle implements Shape{
//implementation
}

package Shapes;

public class ShapeFactory{

  public static Triangle createTriangle(){
    return new Triangle();
  }

  public static Circle createCircle(){
    return new Circle();
  }
}

package SomeOtherPackage;
import Shapes.ShapeFactory;

public class OtherClass{
  Shape myCircle = ShapeFactory.createCircle();
  Shape myTriangle = ShapeFactory.createTriangle();
}

最后,关于如果用户不知道ShapeFactory存在怎么办?问题。 这就是存在文档的原因。为了让其他程序员使用您的API,知道如何使用它!

  

更新

     

即使以上方法是正确的并且将提供所要求的功能,但以下范例将演示如何防止其他类(即使来自同一程序包)以能够使用这些类实现Shape接口的代码。他们将只能通过ShapeFactory()创建其他形状。

//ShapeFactory can be public, or package-private. Depends on programmer's needs
public class ShapeFactory {

    //If factory methods are static, then inner classes need to be static too!
    public static Circle createCircle(){
        return new Circle();
    }

    public static Triangle createTriangle(){
        return new Triangle();
    }


    /*Inner classes must  be set to private, to mask their existance
     from all other classes, except ShapeFactory. Static is only needed
     if the factory method that produces an instance of said class, is
     going to be static itself.*/
    static private class Circle implements Shape{
        private Circle(){
            //Circle implementation
        }
    }

    static private class Triangle implements Shape{
        private Triangle(){
            //triangle implementation
        }
    }

    //More inner classes, etc..

}
  

如上所述创建ShapeFactory后,可以从任何其他包的任何类(甚至与ShapeFactory相同的包)中实例化Shapes仅通过ShapeFactory

//An arbitrary class that can belong to any package
public class ArbitraryClass {

    public static void main(String[] arguments){
        //creation of Shapes using ShapeFactory
        Shape myCircle = ShapeFactory.createCircle();
        Shape myTriangle = ShapeFactory.createTriangle();
    }
}

答案 1 :(得分:-1)

enter image description here

    // this class is in package scope, nobody can access it outside this package
class ShapeImpl1 implements Shape {

        // default is package scope
        ShapeImpl1() {
        }

        @Override
        public void print() {

        }
    }

    public class ShapeFactory {


        public Shape buildShape() {
            return new ShapeImpl1();
        }
    }