Three Factory Patterns in Java

1. Analysis of the topic

(1), simple factory mode

Use the simple factory pattern to simulate the creation of a person by Nvwa. If the parameter M is passed in, it will return a Man object, and if the parameter W is passed in, it will return a Woman object. Please implement this scenario. Now you need to add a new Robot class. If the parameter R is passed in, it will return a Robot object, modify the code and pay attention to the changes of Nuwa.

(2), the factory method pattern

Haier Factory (Haier) produces Haier Air Condition (HaierAirCondition), Midea Factory (Midea) produces Midea Air Conditioner (MideaAirCondition). Describe the scenario using the factory method pattern, draw the class diagram and implement it programmatically. (3), abstract engineering mode

Computer accessories production plants produce hardware devices such as memory and CPU. The brands and models of these memories and CPUs are not necessarily the same. According to the following "Product Hierarchy Structure - Product Family" diagram, the abstract factory mode is used to realize the production process of computer accessories and draw corresponding class diagram, draw the class diagram and implement it programmatically.

2. Class diagram design

(1), simple factory mode,
insert image description here

(2), the factory method pattern
insert image description here

(3), abstract engineering mode

insert image description here

3. Program realization

1. Simple Factory Pattern



package
SimpleFactoryPattern;

//定义抽象类Person

public abstract class Person {

    public void eat(){}//定义抽象方法

    public void run() {}

}

package
SimpleFactoryPattern;

//定义一个Man类继承Person类

public class Man extends Person {

 

    

        public void eat() {

           System.out.println("男人吃东西...");

           super.eat();//重写父类方法eat()

        }

        public void run() {

           
System.out.println("男人跑步..");

           
super.run();//重写父类方法run()

        }

    }

package
SimpleFactoryPattern;

//定义一个Woman类继承Person类

public class Woman extends Person {

    public void eat() {

        System.out.println("女人吃东西..");

        super.eat();

    }

    public void run() {

        System.out.println("女 人跑步..");

        super.run();

    }

}

package
SimpleFactoryPattern;

//定义一个Robot类继承Person类

public class Robot extends Person{

    public void eat() {

        System.out.println("机器人在吃东西...");

        super.eat();//调用父类的eat()方法

    }

    public void run() {

        System.out.println("机器人在跑步...");

        super.run();

    }

}

package
SimpleFactoryPattern;

//创建Nvwa类来获得人类方法

public class Nvwa {

    public static Person getPerson(String people) {

        Person person=null;

        if(people.equalsIgnoreCase("Man")) {

            person=new Man();//将Man类的对象当做Person类来使用

        }else if(people.equalsIgnoreCase("Woman")) {

           person=new Woman();//将Woman类的对象当做Person类来使用

        }else if(people.equalsIgnoreCase("Robot")) {

           person=new Robot();//将Robot类的对象当做Person类来使用

        }

        return person;

    }   

}

package
SimpleFactoryPattern;

//NvwaMakePeople类实现各种方法

public class NvwaMakePeople {

     public static void main(String[] args) {

         Person person;//创建一个对象

         person=Nvwa.getPerson("Man");

         person.eat();//实现方法

         person.run();

         person=Nvwa.getPerson("Woman");

         person.eat();

         person.run();

         person=Nvwa.getPerson("Robot");

         person.eat();

         person.run();

     }

 }


2. Factory Pattern



package
FactoryPattern;

//定义一个接口Air

public interface Air {

 

    void work();//抽象方法

}

package
FactoryPattern;

//HaierAir实现接口Air

public class HaierAir implements Air{

    

public void work() {

    System.out.println("HaierAir is working...");

}

}

package
FactoryPattern;

//实现接口Air

public class MideaAir implements Air{

    public void work() {

        System.out.println("MideaAir is working...");

    }

 

}

package
FactoryPattern;

//定义一个接口AirFactory

public interface AirFactory {

      Air
productAir();//Air类的抽象方法

}

package
FactoryPattern;

//实现接口AirFactory

public class HaierFactory implements AirFactory 

{

    public HaierAir productAir() {

        System.out.println("HaierAir Produced...");

        return new HaierAir();

    }

}

package
FactoryPattern;

//实现接口AirFactory

public class MideaFactory implements AirFactory{

    public MideaAir productAir() {

         System.out.println("MideaAir produced...");

         return new MideaAir();

    }

 

}

package
FactoryPattern;

//FactoryTest来实现各种方法

public class FactoryTest {

 

    public static void main(String[] args) {

        AirFactory airfactoryH=new HaierFactory();//将HaierFactory实例转为AirFactory引用

        AirFactory airfactoryM=new MideaFactory();//将MideaFactory实例转为AirFactory引用

        Air airH=airfactoryH.productAir();//接口Air来创建一个对象airH用airFactoryH来调用productAir()

        airH.work();

        Air airM=airfactoryM.productAir();

        airM.work();

        

    }

 

}


3. Abstract Factory Pattern (AbstactFactoryPattern)



package
AbstractProjectPattern;

//定义一个接口CPU

public interface CPU {

    public void describe();

}

package
AbstractProjectPattern;

//实现接口CPU

public class MacCPU implements CPU{

    public void describe() {

        System.out.println("MacCPU is producing...");

    }

 

}

package
AbstractProjectPattern;

//PcCPU具体实现接口CPU

public class PcCPU implements CPU{

    public void describe() {

        System.out.println("PcCPU is producing...");

    }

 

}

package
AbstractProjectPattern;

//定义一个接口RAM

public interface RAM {

    public void describe();

}

package
AbstractProjectPattern;

//实现接口RAM

public class PcRAM implements RAM{

    public void describe() {

        System.out.println("PcRAM");

    }

 

}

package
AbstractProjectPattern;

//实现接口RAM

public class MacRAM implements RAM{

    public void describe() {

        System.out.println("MacRAM");

    }

 

}

package
AbstractProjectPattern;

//定义一个接口ComputerAccessoriesFactory电脑配件工厂

public interface ComputerAccessoriesFactory {

        CPU productCPU();//CPU接口类的抽象方法

        RAM productRAM();//RAM接口类的抽象方法

}

package
AbstractProjectPattern;

//实现接口ComputerAccessoriesFactory{

public class PcFactory implements ComputerAccessoriesFactory{

    public PcCPU productCPU() {

        System.out.println("PcCPU produced...");

        return new PcCPU();

 }

 

 public PcRAM productRAM() {

        System.out.println("PRAM produced...");

        return new PcRAM();

 }

 

}

package
AbstractProjectPattern;

//实现接口ComputerAccessoriesFactory

public class MacFactory implements ComputerAccessoriesFactory{

     public MacCPU productCPU() {

         System.out.println("MacCPU produced...");

         return new MacCPU();

  }

 

  public MacRAM productRAM() {

         System.out.println("MacRAM produced...");

         return new MacRAM();

  }

 

}

package
AbstractProjectPattern;

 

public class ComputerTest {

 

    public static void main(String[] args) {

        ComputerAccessoriesFactory  pcfactory=new PcFactory();//将PcFactory实例转为ComputerAccessoriesFactory引用

        ComputerAccessoriesFactory macfactory=new MacFactory();//将PcFactory实例转为ComputerAccessoriesFactory引用

        CPU cpu=pcfactory.productCPU();//用工厂创建的对象去调用工厂类CPU接口定义的方法

        RAM ram=macfactory.productRAM();

        cpu.describe();//调用具体实现CPU中的方法

        ram.describe();//调用具体实现RAM中的方法

 

    }

 

}


Test and run results

(1) Nuwa created a human being
insert image description here

(2) Haier and Midea Factory
insert image description here

(3) Computer CPU and RAM

insert image description here

5. Summary of experience

In this assignment, we have done three factory patterns, the first one is the simple factory pattern: Simple Factory Pattern: Also known as the Static
Factory Method pattern, it belongs to the class creation pattern. In the simple factory pattern, instances of different classes can be returned depending on the parameters. The simple factory pattern specifically defines a class to be responsible for creating instances of other classes, and the created instances usually have a common parent class. The simple factory pattern includes the following roles: Factory: Factory role Product: Abstract product role ConcreteProduct: Concrete product role. A simple factory solves the problem of Nuwa creating a human. First, we should create an abstract class or interface Person class, define abstract methods in the Person class, and then create the Man class, the Woman class, and the Robot class to inherit the Person class in these classes. Specifically describe the abstract methods in the parent class, create a Nvwa class to write a getPerson() method to obtain the objects in each class, and finally create a test class to implement the methods separately.

The second is the factory method pattern: the factory method pattern (Factory Method Pattern) is referred to as the factory pattern, also called the virtual constructor (Virtual Constructor) pattern or the polymorphic factory (Polymorphic
Factory) pattern, which belongs to the class creation pattern.

In the factory method pattern, the factory parent class is responsible for defining the public interface for creating product objects, while the factory subclass is responsible for generating specific product objects. The purpose of this is to delay the instantiation of the product class to the factory subclass. That is, through the factory subclass to determine which specific product class should be instantiated. The factory method pattern includes the following roles: Product: abstract product ConcreteProduct: concrete product Factory: abstract factory ConcreteFactory: concrete factory. In the topic of Haier factory and Midea factory implementation, we first create two interfaces, one is Air and the other is AirFactory; write an abstract method in the Air interface, and generate an abstract method of creating a product object of the Air class in AirFactory. In the interface Air, two subclasses, HaierAir and MideaAir, are written to generate specific product work methods. There are also two subclasses HaierFactory and MideaFactory in the interface AirFactory to generate specific product objects.

The third is the abstract factory pattern: Abstract Factory Pattern (Abstract Factory Pattern): Provide an interface to create a series of related or interdependent objects without specifying their specific classes. The abstract factory pattern, also known as the Kit pattern, is an object creation pattern. The abstract factory pattern includes the following roles: AbstractFactory: abstract factory

ConcreteFactory: concrete factory, AbstractProduct: abstract product

ConcreteProduct: Concrete product.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326632211&siteId=291194637