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

An example Analysis of Java facade Model

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Java facade model example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

With the continuous improvement of the system and the deepening of development, they will become more and more complex, and the system will generate a large number of classes, which makes the program more difficult to understand. The facade pattern can be described as providing a simplified interface to simplify the complexity of accessing these classes, sometimes this simplification may reduce the flexibility of accessing these underlying tires. but with the exception of particularly demanding clients (that is, classes that call methods), it usually provides all the functionality you need. Of course, those demanding clients still have direct access to the underlying classes and methods.

The Facade pattern, also known as the front mode, appearance mode, is used to wrap a complex set of classes into a simple external interface.

Now consider the scenario where we have a customer who needs to eat at a restaurant, which requires defining a Customer class and a haveDinner method for that class. Suppose the hotel has three departments: the cashier department, the chef department and the service department. Users' meals need to be coordinated by these three departments. This sample program first defines a cashier department, and the user needs to call the pay method of that department:

Public class PaymentImpl implements Payment {public String pay (double price) {String food = "fast food"; System.out.println ("you have paid:" + price + "yuan, purchased is:" + food); return food;}}

Next, define the chef department:

Public class CookImpl implements Cook {public String cook (String food) {System.out.println ("Chef is cooking:" + food);}}

Waiter Department:

Public class WaiterImpl implements Waiter {public void serve (String food) {System.out.println ("waiter is serving:" + food);}}

Next, implement the haveDinner method of the Customer class:

Public class Customer {public void haveDinner () {/ / create three departments Payment payment = new PaymentImpl (); Cook cook = new CookImpl (); Waiter waiter = new WaiterImpl (); / / call different methods of the three departments String food = payment.pay (); cook.cook (food); waiter.serve (food) / / directly rely on the Facade class to implement the dining method Facade f = new Facade (); f.serveFood ();}}

As the above code shows, Customer needs to call the methods of three departments in turn to complete the haveDinner method. In fact, if the hotel has more departments, then the program needs to call more department methods to implement the haveDinner method-- which makes it more difficult to implement the haveDinner method.

To solve this problem, we can provide a facade (Facade) for Payment, Cook and Waiter, wrap these classes with Facade, and provide a simple access method:

Public class Facade () {/ / defines the encapsulated three departmental Payment payment; Cook cook; Waiter waiter; / / constructors public Facade (Payment payment, Cook cook, Waiter waiter) {this.payment = payment; this.cook = cook; this.waiter = waiter;} public void serveFood () {String food = payment.pay (); cook.cook (food) Waiter.serve (food);}}

As can be seen from the Facade code, the facade ensures the three departments of Payment,Cook and Waiter, and the program provides a simple serveFood method, which provides a method of dining, while the bottom layer depends on the three departments' pay (), cook () and serve () methods. Once the facade class Facade is provided by the program, it becomes easier for the Customer class to implement the haveDinner method. Here is the haveDinner method implemented through the Facade class:

Public void haveDinner () {Facade f = new Facade (); f.serveFood ();}

As can be seen from the above structure, if the facade mode is not used, the client needs to decide which classes and methods to call and call them in a reasonable order to achieve the desired functions. When not in facade mode, the program has the structure shown in the figure:

However, after using the facade mode, the client code only needs to interact with the facade class, so the program structure diagram becomes the following style:

In fact, the HibernateTemplate class of Spring is the facade mode used: when our program uses the find () method of Hibernate, the program only needs one line of code to get the List returned by the query. But the find () method actually hides the following code:

Session session = sf.openSession (); Query query = session.createQuery (hql); for (int iTuno; I

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

Development

Wechat

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

12
Report