The code address is at the end of the text

1 Factory method mode

There was an OEM manufacturer for HP laptops, and then the manufacturer got orders from more brands Acer, Lenovo, Dell, and the OEM found that it was a little difficult to manage if it made more than one brand at a time. Improve the design with the factory pattern by implementing the OEM manufacturer’s factory pattern in the JAVA language (or as a C# console application). Draw a UML diagram for the pattern.

Applicability of the factory method pattern is as follows:

  • When a class does not know the class of the object it needs, in the factory method pattern, the client does not need to know the class name of the specific product class, just the corresponding factory.
  • When a class wants to through its subclasses to specify when creating an object, in the factory method pattern, for the abstract factory class only need to provide an interface for creating products, and by its subclasses to determine specific to create objects, using object-oriented polymorphism and substitution principle on the Richter scale, when the program is run by a subclass overrides the superclass object, so as to make the system more scalable.
  • Delegate object creation task to one of multiple factory subclasses. The client does not need to care which factory subclass creates the product subclass when using, and can dynamically specify when needed. The class name of the specific factory class can be stored in the configuration file or database.

Abstract factory pattern

McDonalds and KFC both operate Hamburg and Cole, using the JAVA language (C# console application) to implement the abstract factory pattern of the two fast-food restaurants operating products. Draw a UML diagram for the pattern.

The abstract factory pattern is primarily applicable when a set of products is created, composed, and represented independently of it. • When a system is to be configured by one of multiple product families. • When emphasizing the design of a set of related product objects for joint use. • When you want to provide a library of product classes and simply display their interface rather than their implementation.

3 builder mode

Example: THE KFC meal builder pattern can be used to describe how KFC creates a meal: Package is a complex object, it usually contains staple food (such as hamburger, chicken roll, etc.) and beverages (such as fruit juice, coke, etc.) part of different package have different part of the KFC attendant can according to customer requirements, step by step, assembling these components, to construct a complete package, and then return to the customer. Designed using the builder pattern and implemented in the JAVA language (or C# console application). Draw a UML diagram for the pattern.

The builder mode applies to the following application scenarios:

  • The same method, different execution order, produces different results.
  • Multiple parts, or parts, can be assembled into an object but produce different results.
  • The product class is very complex, or the different order of calls in the product class has different effects.
  • Initializing an object is particularly complex, with many arguments, many of which have default values.

4 Singleton mode

In an operating system, a Print Spooler is an application for managing Print tasks. With a Print Spooler, users can delete, abort, or change the priority of Print tasks. Only one Print Spooler object is allowed to run on a system, and exceptions are thrown if Print pools are repeatedly created. The singleton pattern is used to simulate the design of the print pool. Implement this pattern in the JAVA language (C# console application). Draw a UML diagram for the pattern.

Application scenarios of the singleton mode:

  • Only one class instance is allowed in the entire program;
  • Objects that need to be frequently instantiated and then destroyed.
  • An object that takes too much time to create or consumes too many resources, but is often used.
  • An environment that facilitates communication between resources

5 Combination Mode

In the fruit Plate (Plate) there are some fruits, such as Apple (Apple), Banana (Banana), Pear (Pear), of course, the large fruit Plate can also have a small fruit Plate, now need to go through the fruits in the Plate (eat), of course, if the implementation of a fruit Plate “eat” method, is actually to eat the fruit. Simulate this scenario using composite mode. Designed using the compositor pattern and implemented in the JAVA language (or C# console application). Draw a UML diagram for the pattern.

The applicability of the composition pattern is as follows: • You want to represent a partial-whole hierarchy of objects. • You want the user to ignore the difference between a composite object and a single object and use all objects in the composite structure uniformly.

6 Adapter Mode

Encryption adapter A system needs to provide an encryption module to encrypt user information (such as password and other confidential information) and then store it in the database. The system has defined the database operation class. In order to improve the development efficiency, it is necessary to reuse the existing encryption algorithms, which are encapsulated in some classes provided by third parties, some of which even have no source code. The adaptor pattern is used to design the encryption module to reuse third-party encryption methods without modifying existing classes. The above design is simulated using the adapter pattern. Implement this pattern in the JAVA language (C# console application). Draw a UML diagram for the pattern.

The applicability of the adapter pattern is as follows: • When the system wants to use existing classes, but the interfaces of existing classes do not meet the needs of the system. • When it is necessary to work together using classes that are incompatible and unrelated to the original interface by creating a reusable class. • Design that requires multiple subclass interfaces to be changed to accommodate classes or methods that act the same but have different names.

7 Appearance Mode

Power master switch Now consider an example of a power master switch to further illustrate the appearance mode. For ease of use, a power switch controls the start and turn off of four lights, a fan, an air conditioner and a television. All the electrical equipment mentioned above can be controlled at the same time through the power main switch, and the system is designed in appearance mode. Implement it in the JAVA language (or C# console application). Draw a UML diagram for the pattern.

The applicability of the appearance mode is as follows:

  • The facade pattern is used when you want to provide a simple entry point for accessing a complex set of subsystems.
  • There are large dependencies between the client program and multiple subsystems. The introduction of facade classes can decouple subsystems from clients, thus improving subsystem independence and portability.
  • In the hierarchical structure, the appearance pattern can be used to define the entrance of each layer in the system. The connection between layers is not directly generated, but the connection is established through the appearance class to reduce the coupling degree between layers.

Code gitee.com/rightstar/s…