简体   繁体   English

Java工厂和观察者模式

[英]java factory and observer pattern

I have to create a app for school and I have to use both factory pattern and observer pattern. 我必须为学校创建一个应用程序,并且必须同时使用工厂模式和观察者模式。 I'm having some difficulty with understanding which one of my files should be the observer and which one should be observable. 我在理解哪个文件应该是观察者和哪个文件是可观察者方面遇到了一些困难。

I have a class called Shoe, and have concrete classes for different types of shoes and they all extend my Shoe class. 我有一个名为Shoe的课程,并且针对不同类型的鞋子有具体的课程,它们都扩展了我的Shoe课程。 I also have a shoeFactory. 我也有一个鞋厂。 I have assigned an int for the amount of shoes that should be in store and I have generated random numbers for the amount that there is now. 我为应该存放的鞋子数量分配了一个整数,并且为现在的鞋子数量生成了一个随机数。 I want the observer to simulate amount of shoes(types) sold over time. 我希望观察者模拟随时间推移售出的鞋子(类型)的数量。

I'm having some difficulty understanding observers and it might be that my logic is incorrect and that observers are not used for this purpose, if that is the case, please let me know because I am completely lost. 我在理解观察者时遇到了一些困难,这可能是我的逻辑不正确,并且观察者未被用于此目的,如果是这种情况,请让我知道,因为我完全迷失了。

Sounds like the Shoe subclasses represent the inventory for the shoe they represent. 听起来好像Shoe子类代表它们所代表的鞋子的库存。

So the ShowFactory is the observer and it's observing the shoes. 所以ShowFactory是observer ,它在观察鞋子。 So the Shoe classes are the observable. 因此, Shoe是可观察的。

When a shoe is sold or added to the store, you adjust the int value. 当鞋子出售或添加到商店时,您可以调整int值。 The observer notices the change and acts upon it if needed. 观察者会注意到更改,并在需要时采取措施。 For example if the inventory for a given shoe falls below a certain level. 例如,如果给定鞋子的库存下降到一定水平以下。

You know how to make a abstract class and how to extend this class. 您知道如何制作抽象类以及如何扩展此类。

If not you can use this help: http://www.tutorialspoint.com/design_pattern/observer_pattern.htm 如果没有,您可以使用此帮助: http : //www.tutorialspoint.com/design_pattern/observer_pattern.htm

When you have your abstract class and extended classes it is very easy to make a factory for it. 当您拥有抽象类和扩展类时,很容易为其建立工厂。

An example is to find in this link: http://www.tutorialspoint.com/design_pattern/factory_pattern.htm 在此链接中找到一个示例: http : //www.tutorialspoint.com/design_pattern/factory_pattern.htm

for your example. 举个例子 Yo got a class 'Shoe' and you got for example SmallShoe, MediumShoe and LargeShoe. Yo拥有一门“鞋子”类,例如,您有SmallShoe,MediumShoe和LargeShoe。 Now, if you make a factory you make a new class called for example 'ShoeFactory' and give it a method: 现在,如果您创建了一家工厂,则可以创建一个名为“ ShoeFactory”的新类,并为其提供一个方法:

public Shoe makeShoe(int shoeSize)

you can now make the implementation for it for example like this: 您现在可以像这样进行实现:

public class ShoeFactory {
  public static final int SMALL = 1;
  public static final int MEDIUM = 2;
  public static final int LARGE = 3;

  public Shoe makeShoe(int shoeSize) {
    switch(shoeSize) {
      case SMALL:
        return new SmallShoe();
      case MEDIUM:
        return new MediumShoe();
      case LARGE:
        return new LargeShoe();
    }
    return null;
  }

}

// main class //主类

public static void main(String[] args) {
  ShoeFactory factory = new ShoeFactory();
  Shoe shoe = factory.makeShoe(ShoeFactory.SMALL);
}

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

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