Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the simple factory pattern of web design pattern

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to realize the simple factory pattern of web design pattern". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the simple factory pattern of web design pattern" can help you solve the problem.

1. Words written in front of you

First of all, what is the use of the factory model? Its main function is to separate the creator from the caller. What does it mean? That is to say, when we callers go to create an instance, we will not be in direct contact with the created instance.

Its core essence is to instantiate objects and use factory methods instead of new operations.

2. Simple factory model

The so-called simple factory model

Used to produce any product in the same hierarchical structure. (for adding new products, you need to modify the already written code)

The simple factory pattern is the simplest factory pattern and the most frequently used factory pattern, but it still has its shortcomings-that is, it violates the object-oriented OCP principle (opening and closing principle): a software entity should be closed for extended development and modification.

I'll use a code example to explain what a simple factory pattern is!

First, let's write an Animal.java interface.

Public interface Animal {

Void speak ()

}

Here we give the animal an attribute-called

two。 Then we create two classes, Dog.java and Cat.java, and both inherit the Animal interface

Public class Cat implements Animal {

@ Override

Public void speak () {

System.out.println ("Meow Meow!")

}

}

Public class Dog implements Animal {

@ Override

Public void speak () {

System.out.println ("woof!")

}

}

3. Next let's create our animal factory AnimalFactory.java, and write a method to create the animal-- CreateAnimal (String type)-- and pass in a type parameter.

Public class AnimalFactory {

Public static Animal createAnimal (String type) {

If ("cat" .equals (type)) {/ / A pair of types are judged and their respective objects are returned.

Return new Cat ()

} else if ("dog" .equals (type)) {

Return new Dog ()

} else {

Return null

}

}

}

4. The final step is to test our code, we create a Test.java

Public class Test {

Public static void main (String [] args) {

Animal A1 = AnimalFactory.createAnimal ("cat")

A1.speak ()

Animal a2 = AnimalFactory.createAnimal ("dog")

A2.speak ()

}

}

The result after running:

Meow, Meow, Meow!

Woof! woof!

Process finished with exit code 0

As you can see from the above code, instead of creating these objects through new Cat () or new Dog (), we create objects through the createAnimal () method, so we separate the creator from the caller.

But as mentioned above, the drawback of this method is that it violates the OCP principle of the object-oriented principle. If we add another animal, then we have to modify the code in AnimalFactory.

This is the end of the content about "how to implement the simple factory pattern of the web design pattern". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report