The business scenario

The last book said that tianting thousand years single Wang dog used the decorative mode to perfectly solve the problem of adding coffee types and coffee attributes. Incidentally, he made a fortune by earning 20 taels of gold. PS: Erlang God earned 5880! The whole system worked for several years without any problems (one day per year on earth). But with the expansion of the business encountered new problems. Today, the dog is playing happily with the Jade Rabbit, and The prince of Heaven’s little prince Nezha stepped on the Hot wheels.

“Puppy, there are new demands on the system. Because the market is so good, people often have several cups of different types of coffee, so after the big guy’s decision to launch the set service, our system will have to upgrade, limited to one day to complete, otherwise directly launched the south Gate. By the way, it’s cheaper to order through a package than it is to order separately and the price can’t appear at the same time, of course the package is pre-edited.”

Small dog a listen to nasty, hurriedly dropped the jade rabbit to move bricks.

Problem analysis

In fact, this is the binding of multiple cups of coffee. As for how to bind, customers don’t care. What they care about is drinking the coffee they want, and they don’t care about the order and production process. (Actually not very suitable, but choose the wrong model can only be forced to speak)

First, three packages A, B and C are generated

class ComboA { fun make(){ var coffee = ShortBlack() as Coffee coffee = Sugur(coffee) coffee.price = coffee.cost() The v (" set A = = = = = = = ", "description: ${coffee. The description} = = = = = = price ${coffee. Price}")}}Copy the code
class ComboB { fun make(){ var coffee = ShortBlack() as Coffee coffee = BigCup(coffee) coffee.price = coffee.cost() The v (" set A = = = = = = = ", "description: ${coffee. The description} = = = = = = price ${coffee. Price}")}}Copy the code
class ComboC { fun make(){ var coffee = ShortBlack() as Coffee coffee = Sugur(coffee) coffee.price = coffee.cost() coffee = BigCup(coffee) coffee.price = coffee.cost() The v (" set A = = = = = = = ", "description: ${coffee. The description} = = = = = = price ${coffee. Price}")}}Copy the code

This is then managed through an intermediate class that can be invoked directly in the code. Call it a day.

class AppearCombo { var comboA = ComboA() var comboB = ComboB() var comboC = ComboC() fun comboA() { comboA.make() } fun  comboB() { comboB.make() } fun comboC() { comboC.make() } }Copy the code

This is the externalist pattern or the implementation of the facade pattern

Definition:

Facade pattern is a structural pattern of objects, and external communication with a subsystem must be carried out through a unified facade object. Facade mode provides a high-level interface that makes subsystems easier to use.Copy the code

Advantages:

● Loosely coupled facade mode loosens the coupling between the client and the subsystem, making it easier to expand and maintain the modules within the subsystem. ● Easy to use facade mode makes the subsystem more easy to use, the client no longer need to understand the implementation of the internal subsystem, also do not need to interact with many modules within the subsystem, just need to interact with the facade class. A good use of Facade can help us better divide the layers of access. Some methods are external to the system, while others are internal to the system. Centralize functions that need to be exposed to the outside in the facade, which is easy for clients to use and hides internal detailsCopy the code