Reader small image

You're reading from  Advanced Python Programming - Second Edition

Product typeBook
Published inMar 2022
PublisherPackt
ISBN-139781801814010
Edition2nd Edition
Right arrow
Author (1)
Quan Nguyen
Quan Nguyen
author image
Quan Nguyen

Quan Nguyen is a Python programmer and machine learning enthusiast. He is interested in solving decision-making problems under uncertainty. Quan has authored several books on Python programming and scientific computing. He is currently pursuing a Ph.D. degree in computer science at Washington University in St. Louis, researching Bayesian methods in machine learning.
Read more about Quan Nguyen

Right arrow

Chapter 23: Other Structural Patterns

Besides the patterns we covered in the previous chapters, there are other structural patterns we can cover: flyweight, model-view-controller (MVC), and proxy. These patterns are different from those discussed in previous chapters. The flyweight pattern is an optimization design pattern that's suitable for memory usage minimization. The MVC pattern, on the other hand, is popular in object-orient programming and is designed to separate different parts of a larger application. Finally, the proxy pattern is used to maintain actions that are taken on important objects.

These three patterns will complete our discussion on structural patterns.

In this chapter, we will cover the following topics:

  • Implementing the flyweight pattern
  • Implementing the model-view-controller pattern
  • Applying the proxy pattern

By the end of this chapter, we will have gained an overall understanding of various structural patterns and the use...

Technical requirements

Implementing the flyweight pattern

What is the flyweight pattern? Object-oriented systems can face performance issues due to the overhead of object creation. Performance issues usually appear in embedded systems with limited resources, such as smartphones and tablets. They can also appear in large and complex systems where we need to create a very large number of objects (and possibly users) that need to coexist at the same time. The flyweight pattern teaches programmers how to minimize memory usage by sharing resources with similar objects as much as possible.

Whenever we create a new object, extra memory needs to be allocated. Although virtual memory provides us, theoretically, with unlimited memory, the reality is different. If all the physical memory of a system gets exhausted, it will start swapping pages with the secondary storage – usually a hard disk drive (HDD) – which, in most cases, is unacceptable due to the performance differences between the main memory...

Implementing the model-view-controller pattern

The MVC pattern is useful mainly in application development and helps developers improve the maintainability of their applications by avoiding mixing the business logic with the user interface.

One of the design principles related to software engineering is the separation of concerns (SoC) principle. The idea behind the SoC principle is to split an application into distinct sections, where each section addresses a separate concern. Examples of such concerns are the layers that are used in a layered design (data access layer, business logic layer, presentation layer, and so forth). Using the SoC principle simplifies the development and maintenance of software applications.

The MVC pattern is nothing more than the SoC principle applied to OOP. The name of the pattern comes from the three main components that are used to split a software application: the model, the view, and the controller. MVC is considered an architectural pattern...

Applying the proxy pattern

In some applications, we want to execute one or more important actions before accessing an object. This is where the proxy pattern comes in. An example is accessing sensitive information. Before we allow any user to access sensitive information, we want to make sure that the user has sufficient privileges. The important action is not necessarily related to security issues. Lazy initialization (http://j.mp/wikilazy) is another case; we want to delay the creation of a computationally expensive object until the first time the user needs to use it. The idea of the proxy pattern is to help with performing such an action before accessing the actual object.

The proxy design pattern gets its name from the proxy (also known as surrogate) object, which is used to perform an important action before the actual object is accessed. There are four different well-known proxy types (http://j.mp/proxypat). They are as follows:

  • A remote proxy, which acts as the local...

Summary

In this chapter, we covered three other structural design patterns: flyweight, MVC, and proxy. Throughout this chapter, we learned about the differences between these and the other structural patterns we have discussed. Then, we implemented each of these patterns in hands-on examples. As we saw, the flyweight pattern is designed for memory usage minimization, the MVC pattern maintains a logical organization of different parts of an application, and the proxy pattern is typically used when sensitive information is accessed.

In the next chapter, we will start exploring behavioral design patterns. Behavioral patterns cope with object interconnection and algorithms. The first behavioral pattern we will cover is the chain of responsibility.

Questions

  1. What is the main motivation for the flyweight pattern?
  2. What is the main motivation for the MVC pattern?
  3. What is the main motivation for the proxy pattern?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Advanced Python Programming - Second Edition
Published in: Mar 2022Publisher: PacktISBN-13: 9781801814010
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Quan Nguyen

Quan Nguyen is a Python programmer and machine learning enthusiast. He is interested in solving decision-making problems under uncertainty. Quan has authored several books on Python programming and scientific computing. He is currently pursuing a Ph.D. degree in computer science at Washington University in St. Louis, researching Bayesian methods in machine learning.
Read more about Quan Nguyen