簡體   English   中英

在不同的線程上執行兩個不同的接口實現

[英]Have two different Implementations for an interface execute on different thread

我有一個接口說VegetableCreation有一個方法:

public void writeVeggieDataIntoFile();

以及兩個不同的類AppleMango實現了VegetableCreation

還有一個帶有create()方法的工廠類VegetableFactory

public class VegetableFactory {
    public VegetableCreation create(String arg0) {

        if (arg0.equals("Apple"))
           return new Apple();
        else if (arg0.equals("Mango")
           return new Mango();
        else {
           // create and return both apple and mango by spawning two different threads
           // so that the writeVeggieDataIntoFile();  gets invoked concurrently
           // for both apple and mango and two different file is created
        }
    }
}

我在這里想要實現的是當我從客戶端類的main()方法調用VegetableFactory類的create() main()方法並將除"Apple""Mango"之外的任何字符串值作為運行時參數傳遞時。 我想要兩個不同的線程來處理每個AppleMango對象,並在每個writeVeggieDataIntoFile()方法上同時工作。

任何關於設計策略的建議/或使用哪些並發API等都將受到高度贊賞。

PS:我應該稱它為水果**而不是蔬菜*

查看Composite模式,然后構建一個CompositeVegetable,當被告知“執行它的事情”時會啟動兩個線程,一個執行一個操作,另一個執行另一個操作。

public class BothVegetable implements Vegetable {
    public void writeVeggieDataInfoFile() {
        Thread thread1 = new Thread(new AppleRunnable());
        Thread thread2 = new Thread(new MangoRunnable());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}

// and in your factory

    return new CompositeVegetable();

PS。 你的蔬菜看起來像我的水果!

我希望從一家名為VegetableFactory / FruitFactory的工廠獲得Vegetable / Fruit ,而不是VegetableCreation FruitCreation / FruitCreation

我會避免在工廠方法中創建線程和文件作為副作用。

我還要重命名/更改writeFruitDataIntoFile()write(Writer out)因為您編寫的內容由封閉的接口或類告知,並且您寫入的位置由其方法參數writeFruitDataIntoFile()

如果你真的需要並發處理,請在write(...)創建一個線程,讓工廠方法只返回一個Fruit

優點是:

  • 沒有必須記錄的對象創建的副作用。
  • 線程僅按需創建(在調用write()情況下),而不是在每個Fruit對象創建時創建。
  • 你不需要額外的課程。
  • 每個了解工廠設計模式的人都會清楚地了解這是一個干凈的實施方案。

看到:

為了好的OO練習我會使用一個界面:

interface Fruit {

  void write(Writer out);

  ...
}  // Fruit

它的抽象實現:

public abstract class AbstractFruit implements Fruit {

  Data data;

  public void write(Writer out) {
    ...
  }

  ...
}  // AbstractFruit

public classe Apple extends AbstractFruit implements Fruit {
  ...
}

public classe Mango extends AbstractFruit implements Fruit {
  ...
}

為了類型安全,我使用特定的get...()方法(因為它通常在Java API中完成 ):

public class FruitFactory {

  public static Fruit getApple() {
     Fruit a = new Apple()
     ...
     return a;
  }

  public static Fruit getMango() {
     Fruit m = new Mango()
     ...
     return m;
  }
}  // FruitFactory

或者enum

interface Fruit {

  enum Type {
    APPLE,
    MANGO 
  }

  void write(Writer out);

  ...
}  // Fruit

public class FruitFactory {

  public static Fruit get(Fruit.Type fruitType) {

     switch (fruitType) {
        case Fruit.Type.APPLE:
          Fruit a = new Apple()
          ...
          return a;
          break;
        case Fruit.Type.MANGO:
          Fruit m = new Mango()
          ...
          return m;
          break;
        default:
          throw new FruitTypeNotSupported/* Runtime, i.e. unchecked */Exception();
          break; 
     }
  }  // get(...)
}  // FruitFactory

請參閱RuntimeException的API文檔:

未經檢查的異常不需要在方法[...] throws子句中聲明

首先,我會讓你的工廠有靜態創建。 然后在create do item instanceof Fruit中創建fruit線程。 否則如果項目instanceof蔬菜然后做菜線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM