【问题标题】:how to create abstract factory to instantiate objects in java如何创建抽象工厂以在java中实例化对象
【发布时间】:2021-12-07 13:43:04
【问题描述】:

我想创建一个抽象工厂。这是我尝试过的。

//抽象类Worker

public abstract class Worker {
    String phoneNumber;
    String firstName;
    String lastName;
    String workerType;
    String ifu;
    String imageParth;
    //....
  public String getWorkerType() {
        return workerType;
    }
}

// 扩展worker的电工类

package worker.domain.worker;

public class Electrician extends Worker{
    
    
    public Electrician() {}
    public Electrician(String phoneNumber, String firstName, String lastName, String ifu, String workerType,
            String imageParth) {
        super(phoneNumber, firstName, lastName, ifu,workerType, imageParth);
    }
    
    
    public String getWorkerType() {
        return "Electrician";
    }
    
}

//梅森类

package worker.domaine.worker;

public class Mason extends Worker{
    
    public Mason() {};

    public Mason(String phoneNumber, String firstName, String lastName, String ifu,String workerType,
            String imageParth) {
        super(phoneNumber, firstName, lastName, ifu, workerType, imageParth);
    }
    
    String getworkerType() {
        return "Mason";
    }
}

// 接口WorkerAbstractFactory

package worker.domaine.worker;

public interface WorkerAbstractFactory {
    Worker createWorker(String typeWorker);
}

//

public class WorkerFactory implements WorkerAbstractFactory{

    @Override
    public Worker createWorker(String typeWorker) {
        Worker worker = null;
        if(worker != null) {
            switch (typeWorker) {
            case "Electrician":
                Electrician electrician =new Electrician();
                electrician = new Electrician (electrician.getPhoneNumber(), electrician.getFirstName(), electrician.getLastName(), electrician.getIfu(), electrician.getWorkerType(),electrician.getImageParth());
                
            case "Mason":
                Mason mason =new Mason();
                mason = new Mason (mason.getPhoneNumber(), mason.getFirstName(), mason.getLastName(), mason.getIfu(), mason.getworkerType(),mason.getImageParth());
                }}

//应用类

public class WorkerFactoryProvider {
    
     public static WorkerAbstractFactory getWorkerFactory(String workerCategory) {
         //WorkerFactory workerFactory = new WorkerFactory();
         WorkerFactory workerFactory = new WorkerFactory();
         if (workerCategory != null) {
             switch (workerCategory) {
                case "Electrician":
                    Worker worker1 = workerFactory.createWorker("Electrician");
                    worker1.getWorkerType();
                    String a=worker1.getWorkerType();
                    System.out.println(a);  
                    
                case "Mason":
                    Worker worker2 = workerFactory.createWorker("Mason");
                    worker2.getWorkerType();
                    String b=worker2.getWorkerType();
                    System.out.println(b);               
             
             }
             
             
         }
         return null;
     }

你认为它可以这样工作吗?现在,如果我真的想要一个具体的对象,怎么办?因为我想写一个方法来根据类型计算每个工人的工资例如我如何在方法中使用我的抽象工厂来返回每种类型。

【问题讨论】:

  • 注意:您需要将所有这些字段 phoneNumber 等传递到工厂才能实例化一个 Worker。或者,让工厂创建一个匹配底层类型的 WorkerBuilder。
  • 欢迎来到 StackOverflow。你的第一个问题,“你认为它可以这样工作吗?”征求意见。您有什么(双关语)想问的具体问题吗?

标签: java abstract-factory


【解决方案1】:

您有一个 Worker 类型的类层次结构。要实例化那些你可以使用一个独立的工厂类,你在这里不需要一个抽象工厂。例如,这就足够了:

public class WorkerFactory {            
    public Worker createWorker(String workerType) {
        switch (workerType) {
            case "Electrician": return new Electrician();                    
            case "Mason": return new Mason();
        }
    }
}

抽象工厂模式更加精细,允许为相关层次的对象注入不同的具体工厂,这样客户端就不需要知道其中的区别。例如,您可以有一个抽象的 TransportationFactory:

interface Transportation {
    void travelTo(String destination);
}

interface TransportationFactory {
    Transportation simple();
    Transportation luxurious();
}

还有两个具体的实现(匹配两个不同但相似的类层次结构):

class WaterTransportationFactory {
   Transportation simple() {
       return new Kayak();
   }
   Transportation luxurious() {
       return new Yacht();
   }
}

还有:

class LandTransportationFactory {
   Transportation simple() {
       return new Bike();
   }
   Transportation luxurious() {
       return new RaceCar();
   }
}

这种模式的好处是客户端可以配置为使用水路或陆路运输(或稍后添加的新航空运输),而无需进行任何更改:

class Client {

    private TransportationFactory transportationFactory;

    public Client(TransportationFactory transportationFactory) {
        this.transportationFactory = transportationFactory;
    }

    public void travel(String destination) {
        transportationFactory.simple().travelTo(destination);
    }

    public void travelInStyle(String destination) {
        transportationFactory.luxurious().travelTo(destination);
    }

}

编辑:您可以使用getWorkerType 方法更改简单/豪华的方法以匹配您的示例样式。如果可能,我更喜欢避免条件逻辑,让创建的类自己确定它们的可用性。这进一步解耦,允许以最少的代码更改添加层次结构成员:

enum TransportationType {
    SIMPLE, LUXURIOUS
}

interface Transportation {

    void travelTo(String destination);

    // allow the class to specify its own type
    TransportationType getType();
}

// intermediate interface to distinguish Water from Land
interface WaterTransportation extends Transportation {
}

class Kayak implements WaterTransportation {

    void travelTo(String destination) {
        // splash splash
    }

    TransportationType getType() {
        return TransportationType.SIMPLE;
    }
}

class WaterTransportationFactory {

   private WaterTransportation[] waterTransportations;

   // Inject all available beans implementing WaterTransportation
   // e.g. using Spring or some other dependency injection mechanism
   public WaterTransportationFactory(WaterTransportation[] waterTransportations) {
       this.waterTransportations = waterTransportations;
   }

   public Transportation create(TransportationType type) {
       for(WaterTransportation waterTransportation : waterTransportations) { 
           if (waterTransportation.getType() == type) {
               // we are returning the same instance every time
               // this could be ok for singleton beans
               // but if you really need a fresh instance you could use builders (see below)
               return waterTransportation;
           }
       }
       throw new IllegalArgumentException("No implementation for WaterTransportation type=" + type);
   }
}

建筑商的替代方案:

KayakBuilder implements WaterTransportationBuilder {
    KayakBuilder name(String name) { ... };
    KayakBuilder weight(String weightInKg) { ... };
    KayakBuilder year(String yearBuilt) { ... };
    KayakBuilder speed(String averageSpeed) { ... };
    Kayak build() { return kayak; }
}

有关构建器的更多信息,请参阅full exposition of the Builder pattern

class WaterTransportationFactory {

   private WaterTransportationBuilder[] builders;

   // Inject all available WaterTransportationBuilders
   // e.g. using Spring or some other dependency injection mechanism
   public WaterTransportationFactory(WaterTransportationBuilder[] builders) {
       this.builders = builders;
   }

   // extra arguments can be passed to build the instance
   public Transportation create(TransportationType type, String name, int weightInKg, int yearBuilt, int averageSpeed) {
       for(WaterTransportationBuilder builder: builders) { 
           if (builder.getType() == type) {
               return builder
                   .name(name)
                   .weight(weightInKg)
                   .year(yearBuilt)
                   .speed(averageSpeed)
                   .build();
           }
       }
       throw new IllegalArgumentException("No implementation for WaterTransportation type=" + type);
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多