简体   繁体   English

策略模式无法获得吸气剂

[英]strategy pattern no access to getters

Structure 结构体

-ClassA
|---|
|---ClassAImplA
|---ClassAImplB
-Main

Class A: A类:

public interface ClassA {

    public void execute();

}

Implementaion A: 实施A:

public class ClassAImplA implements ClassA 
{
    private int a = 5;

    public ClassAImplA (int a){setA(a);}
    @Override
    public void execute() {
        System.out.println(a);

    }
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }

Implementaion B: 实施B:

public class ClassAImplB implements ClassA 
{
    private boolean b = false;

     public ClassAImplB (int a){setB(b);}
    @Override
    public void execute() {
        System.out.println(b);

    }
    public booelan getB() {
        return b;
    }
    public void setA(boolean b) {
        this.b = b;
    }   

main: 主要:

public class main {

    /**
     * @param args
     */
    public static  void main(String[] args) {
        ClassAImplA param1 = new ClassAImplA(10);
        ClassA = param1;
    }


}

By doing this I make ClassA interchangeable, but I lose the capability to access the parameter int a . 这样,我使ClassA可互换,但是I失去了访问参数int a的能力。 Is there a way to still make it interchangeable, and still have access to int a , or in case of ClassAImplB , the field boolean b ? 有没有办法使它仍然可以互换,并且仍然可以访问int a ,或者在ClassAImplB情况下,可以访问字段boolean b

There is a way, but it's not a good idea to do, as it defeats the purpose: 有办法,但这不是一个好主意,因为它违背了目的:

ClassAImplA param1 = new ClassAImplA(10);
ClassA = param1;
if (param1 instanceof ClassAImplA) {
    param1x = (ClassAImplA) param1;
    System.out.println(param1x.getA());
}

But don't do this. 但是不要这样做。 It defeats the purpose of the pattern. 它违反了模式的目的。 The purpose of the pattern is to use objects of type ClassA , without having to know how they work. 该模式的目的是使用ClassA类型的对象,而不必知道它们的工作方式。 The getA method is only defined in ClassAImplA , it's an implementation detail that should not be relevant to users of the ClassA type. getA方法仅在ClassAImplA定义,它是与ClassA类型的用户无关的实现细节。 They shouldn't have to know. 他们不必知道。 It's hidden. 它是隐藏的。 This is called good encapsulation and information hiding. 这称为良好封装和信息隐藏。

you need one more class using composition to decide which implementation is needed. 您还需要一个使用组合的类来决定需要哪种实现。

 public ClassHelper{
     private A a;

     public ClassHelper(A a){
      this.a = a;
     }

     public void execute() {
      this.a.execute();
}


 }  


  public class main {

/**
 * @param args
 */
public static  void main(String[] args) {
    ClassHelper param1 = new ClassHelper(new ClassAImplA(10));
    param1.execute();

   //or when you need classBIMpl

   param1 = new ClassHelper(new ClassAImplB(true));
    param1.execute();


}

} }

And about the ability to access member of implA or implB , no you cannot have that flexibilty with this patter, whole point of this pattern is that caller need not be aware of implementation details. 关于访问implA或implB成员的能力,没有,您不能在这种模式下具有灵活性,此模式的重点是调用者无需了解实现细节。

Define an interface for the strategy and a Factory with different overloaded methods to create the concrete instances of the classes. 为策略定义一个接口,并为工厂定义不同的重载方法,以创建类的具体实例。 Of course the methods are typed to the interface instead of the concrete classes. 当然,将方法键入接口而不是具体类。

The interface. 接口。

public interface Strategy {
  void execute();
}

The first implementation. 第一次执行。

public class ConcreteStrategy implements Strategy {

  private boolean a;

  public ConcreteStrategy(final boolean a) { this.a = a; }

  public void execute() {}
}

The second implementation. 第二实施。

public class AnotherConcreteStrategy implements Strategy {

  private int a;

  public AnotherConcreteStrategy(final int a) { this.a = a; }

  public void execute() {}
}

The factory. 该工厂。

public class Factory {

  public static Strategy create(final boolean a) {
    return new ConcreteStrategy(a);
  }

  public static Strategy create(final int a) {
    return new AnotherConcreteStrategy(a);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM