This is the 30th day of my participation in the More Text Challenge. For more details, see more text Challenge


Related articles

Design Patterns Series: Design Patterns column


The basic business logic is not difficult to write

Basic business logic is easy to write, and the ability of a qualified programmer to write extensible, maintainable, highly cohesive, low-coupling code is the real ability. So starting today, I will be looking at design patterns. Design patterns are not aimed at programming languages, but at the idea of programming.


  • The simplest of the design patterns is probably the factory pattern, and the factory pattern and the singleton pattern are the most commonly used patterns in our enterprise projects. Today we will briefly introduce the factory model. Is a personal summary, do not like spray!!

describe

  • The factory pattern simplifies building objects. We normally build objects with new in Java, but some objects may require additional operations such as setting defaults and so on. At this time, in order to simplify the user’s operation, we introduced the design idea of factory mode. The factory pattern encapsulates the process of building objects in a factory class, and we need specific objects in the factory class that we can retrieve through the factory. Improve the flexibility of use.

advantages

  • It is easy to see the benefits of the factory pattern from the above description. With the factory pattern, we don’t need to focus on the process of building objects, we just need to send instructions to the factory to produce objects. Eliminate the complexity of building and increase the readability of your code.

Scene reappearance

  • Before the advent of the industrial age our production was artificial, in other words what we need, we need to do it ourselves. And when you have a factory, you get rid of your hands. Let’s say you are the owner of a clothing factory. Summer is coming and you need to buy a batch of summer clothes. I believe it is impossible for you to make clothes by yourself now, it is all through contacting the garment factory, telling the garment factory the style of clothes you need, and after a period of time you get the clothes you specify. You don’t have to worry about how these clothes work. The garment factory here is the factory model of our factory.

classification

  • The factory pattern is further divided into three types: simple factory pattern + factory method pattern + Abstract factory pattern

  • Simple factory mode: simple factory mode is the situation that we reproduce the above scenario. Simple factory means that all the details are completed by a single factory. The pressure of the factory under this mode is particularly large.

  • Factory method pattern, factory method pattern on the basis of the above situation will be changed factory optimization, before the factory is a factory production of clothing all the year round, now a factory class abstraction, there are four factories in the implementation of class is the abstract factory, these four concrete which is responsible for the production of spring, summer, autumn and winter clothes. It’s still the factory model, but the relatively simple factory model has taken the pressure off the factory.

  • Abstract factory pattern:

The abstract factory pattern is a reinforcement of the abstract method pattern, in which a concrete factory implementation class is responsible for producing one type of clothing, while in the abstract factory pattern, a concrete factory implementation class can produce more than one class of clothing. This will take more pressure off the garment factories.

Code implementation

Here we implement the simple factory pattern code, the other two pattern knowledge based on the simple factory pattern factory class reinforcement

Clothes abstract class

package dingyingjun.JavaDesignDetails. Factory model. Public abstract class Clothes {// Wristband private int wristband=2; Private int neckline=1; Public void hold() {system.out.println (" MY way to dress is from the neckline "); } public int getWristband() { return wristband; } public void setWristband(int wristband) { this.wristband = wristband; } public int getNeckline() { return neckline; } public void setNeckline(int neckline) { this.neckline = neckline; }}Copy the code

AutumnCloth

package dingyongjun.JavaDesignDetails. Special; import dingyongjun.JavaDesignDetails. Factory mode. Features.Clothes; Public class AutumnCloth extends Clothes {@override public void hold() {system.out.println (); }}Copy the code

Specific clothing category 2 (SpringCloth)

package dingyongjun.JavaDesignDetails. Special; import dingyongjun.JavaDesignDetails. Factory mode. Features.Clothes; Public class SpringCloth extends Clothes {@override public void hold() {system.out.println (" I want to wear my Clothes from the collar "); }}Copy the code

Specific clothing category 3 (SummerCloth)

package dingyongjun.JavaDesignDetails. Special; import dingyongjun.JavaDesignDetails. Factory mode. Features.Clothes; Public class SummerCloth extends Clothes {@override public void hold() {system.out.println (" SummerCloth extends Clothes "); }}Copy the code

The factory class (Clothfactory)

package dingyongjun.JavaDesignDetails. Factory mode. Simple factory mode. import dingyongjun.JavaDesignDetails. Factory mode. Features.Clothes; import dingyongjun.JavaDesignDetails. AutumnCloth; import dingyongjun.JavaDesignDetails. Special.SpringCloth; import dingyongjun.JavaDesignDetails. Special.SummerCloth; public class ClothFactory { public static Clothes createClothes(String type) throws Exception { Clothes clothes=null; switch (type) { case "spring": clothes=new SpringCloth(); break; case "summer": clothes=new SummerCloth(); break; case "autumn": clothes=new AutumnCloth(); break; Default: Throw new Exception(" The type you specified is not currently supported "); } return clothes; }}Copy the code

Client call

package dingyongjun.JavaDesignDetails. Factory mode. Simple factory mode. Client; import dingyongjun.JavaDesignDetails. Factory mode. Features.Clothes; import dingyongjun.JavaDesignDetails. Factory mode. Simple factory mode. Factory.clothfactory; Public class client {public static void main(String[] args) throws Exception {system.out.println (" factory you gave me a spring collection "); Clothes spring = ClothFactory.createClothes("spring"); spring.hold(); System.out.println("********************"); System.out.println(" factory you give me a fall suit "); Clothes autum = ClothFactory.createClothes("autumn"); autum.hold(); System.out.println("********************"); System.out.println(" factory you give me a summer suit "); Clothes summer = ClothFactory.createClothes("summer"); summer.hold(); System.out.println("********************"); }}Copy the code

Results show


conclusion

  • The learning of design mode is imperceptible. Don’t worry about learning design mode. It takes time to grow.

I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah