Reader small image

You're reading from  Expert C++ - Second Edition

Product typeBook
Published inAug 2023
PublisherPackt
ISBN-139781804617830
Edition2nd Edition
Right arrow
Authors (5):
Marcelo Guerra Hahn
Marcelo Guerra Hahn
author image
Marcelo Guerra Hahn

Marcelo Guerra Hahn, With over 18 years of experience in software development and data analysis, Marcelo Guerra Hahn is a seasoned expert in C++, C#, and Azure. As an Engineering Manager at Microsoft C++ Team and former leader of SoundCommerce's engineering team, Marcelo's passion for data and informed decision-making shines through. He shares his knowledge as a lecturer at esteemed institutions like Lake Washington Institute of Technology and University of Washington. Through this book, Marcelo aims to empower readers with advanced C++ techniques, honed by real-world experience, to become proficient programmers and skilled data analysts.
Read more about Marcelo Guerra Hahn

Araks Tigranyan
Araks Tigranyan
author image
Araks Tigranyan

Araks Tigranyan is a passionate software engineer at Critical Techworks, with an unwavering love for the world of programming, particularly in C++. Her dedication to crafting efficient and innovative solutions reflects her genuine passion for coding. Committed to excellence and driven by curiosity, Araks continuously explores new technologies, going above and beyond to deliver exceptional work. Beyond programming, Araks finds solace in sports, with football holding a special place in her heart. As an author, Araks aspires to share her profound expertise in C++ and inspire readers to embark on their programming journeys.
Read more about Araks Tigranyan

John Asatryan
John Asatryan
author image
John Asatryan

John Asatryan, the Head of Code Republic Lab at Picsart Academy, seamlessly blends his academic background in International Economic Relations from the Armenian State University of Economics with his ventures in technology and education. Driven by a genuine passion for coding, John's commitment to empowering aspiring developers is evident in his expertise in the field. His unwavering dedication to bridging the gap between education and technology inspires others to pursue their coding dreams.
Read more about John Asatryan

Vardan Grigoryan
Vardan Grigoryan
author image
Vardan Grigoryan

Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems.
Read more about Vardan Grigoryan

Shunguang Wu
Shunguang Wu
author image
Shunguang Wu

Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
Read more about Shunguang Wu

View More author details
Right arrow

Designing World-Ready Applications

Using a programming language in production-ready projects is a new step in learning the language itself. Sometimes, the examples in this book may take a different approach than the equivalent version used in real-world programs. When theory meets practice is when you learn a language. C++ is not an exception. Learning syntax, solving book problems, and understanding examples in books are stepping stones toward creating real-world applications. However, in the real world, we face an extra set of challenges.

In this chapter, we will go over the basics of practical programming with C++. This will help you tackle real-world applications better. Complex projects require a lot of thinking and designing. Sometimes, programmers must completely rewrite a project and start from scratch because they have made bad design choices at the beginning of development. This chapter addresses some common design ideas that will help prevent errors and write world-ready...

Technical requirements

The g++ compiler with the -std=c++2a option is used to compile the examples in this chapter. You can find the source files used in this chapter at https://github.com/PacktPublishing/Expert-C-2nd-edition.

Design patterns

Design patterns are a set of well-studied solutions to commonly occurring design situations. These patterns provide mechanisms to solve complex problems in a structured and reusable way. Design patterns were first introduced by Christopher Alexander, who was an architect and design theorist. In 1977, he published a book titled A Pattern Language: Towns, Buildings, Construction. His work was later adapted by programmers and developers to organize their work, which led to the creation of software design patterns. This eventually led to the publication of Design Patterns: Elements of Reusable Object-Oriented Software by the Gang of Four. The Gang of Four includes Erich Gamma from Eclipse, IBM, and Microsoft, Richard Helm from IBM, Ralph Johnson from Xerox, and John Vissides from IBM.

Design patterns are important in software design, as they provide a common language for designers to refer to their ideas and proposed solutions. Design patterns allow designers to use...

Applying design patterns

Design patterns are powerful solutions that can be used to solve common software problems. Given the amount of study that has gone into them, they can produce scalable, maintainable, and flexible code. Despite this, they must be applied appropriately for the patterns to be useful. The following are some considerations to make sure that patterns are applied appropriately.

The problem

When applying a design pattern, it is essential to consider the problem at hand and all the requirements involved. Once understood, they must be contrasted with the patterns being considered and the problem they are designed to resolve. Patterns are general solutions, so they may or may not match the precise requirements of the problem at hand.

Trade-offs

Each pattern has a series of positive effects and also some drawbacks. When applying a pattern, it is important to consider the trade-offs related to code complexity and potential performance issues.

Systemwide impact...

An example of a real-world project

To help ground the ideas of design patterns and DDD, we will discuss the implementation of an Amazon clone in C++.

Using DDD, the first step is to understand the domain. In this scenario, the domain is e-commerce. The idea, then, is to create a piece of software that models electronic commerce and supports users buying products online.

As previously mentioned, using bounded context is an essential component of DDD. In this case, we can identify the following ones – product management, order processing, and payment processing. Each has its own set of rules and constraints and can be considered a separate part of the system.

To begin with, let’s focus on the product management domain. This area includes product listings, inventory tracking, and customer feedback processing. Those concepts can be modeled using product, inventory, and review.

Let’s start with the product. Since product addition and removal happens frequently...

Summary

Software development requires meticulous design. In this chapter, we looked at using design patterns to help us identify well-known solutions to already-studied problems. We looked at creational, structural, and behavioral patterns. Creational patterns allow us to create objects more flexibly. Structural designs help us organize things and classes, and behavioral patterns help us manage interactions between objects.

On top of this, we looked at a technique used to facilitate the design of our entities, known as DDD. This approach uses domain expertise to model the real-world entities needed to perform operations that a system needs to support. This knowledge is usually acquired by interacting with domain experts. DDD promotes the use of domain areas so that we can create less coupled and more cohesive entities.

Finally, we showed a partial implementation of an Amazon clone, where we used DDD to identify the entities involved. Then, we used design patterns to implement...

Questions

  1. What are the benefits of using design patterns?
  2. What is the purpose of creational patterns?
  3. What is the purpose of structural patterns?
  4. What is the purpose of behavioral patterns?
  5. What is the purpose of DDD?
  6. Which entities would you identify in the bounded domain payment from the Amazon clone example?
  7. Which pattern would you use to create different payment methods?

Further reading

For further information, refer to the following:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Expert C++ - Second Edition
Published in: Aug 2023Publisher: PacktISBN-13: 9781804617830
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 €14.99/month. Cancel anytime

Authors (5)

author image
Marcelo Guerra Hahn

Marcelo Guerra Hahn, With over 18 years of experience in software development and data analysis, Marcelo Guerra Hahn is a seasoned expert in C++, C#, and Azure. As an Engineering Manager at Microsoft C++ Team and former leader of SoundCommerce's engineering team, Marcelo's passion for data and informed decision-making shines through. He shares his knowledge as a lecturer at esteemed institutions like Lake Washington Institute of Technology and University of Washington. Through this book, Marcelo aims to empower readers with advanced C++ techniques, honed by real-world experience, to become proficient programmers and skilled data analysts.
Read more about Marcelo Guerra Hahn

author image
Araks Tigranyan

Araks Tigranyan is a passionate software engineer at Critical Techworks, with an unwavering love for the world of programming, particularly in C++. Her dedication to crafting efficient and innovative solutions reflects her genuine passion for coding. Committed to excellence and driven by curiosity, Araks continuously explores new technologies, going above and beyond to deliver exceptional work. Beyond programming, Araks finds solace in sports, with football holding a special place in her heart. As an author, Araks aspires to share her profound expertise in C++ and inspire readers to embark on their programming journeys.
Read more about Araks Tigranyan

author image
John Asatryan

John Asatryan, the Head of Code Republic Lab at Picsart Academy, seamlessly blends his academic background in International Economic Relations from the Armenian State University of Economics with his ventures in technology and education. Driven by a genuine passion for coding, John's commitment to empowering aspiring developers is evident in his expertise in the field. His unwavering dedication to bridging the gap between education and technology inspires others to pursue their coding dreams.
Read more about John Asatryan

author image
Vardan Grigoryan

Vardan Grigoryan is a senior backend engineer and C++ developer with more than 9 years of experience. Vardan started his career as a C++ developer and then moved to the world of server-side backend development. While being involved in designing scalable backend architectures, he always tries to incorporate the use of C++ in critical sections that require the fastest execution time. Vardan loves tackling computer systems and program structures on a deeper level. He believes that true excellence in programming can be achieved by means of a detailed analysis of existing solutions and by designing complex systems.
Read more about Vardan Grigoryan

author image
Shunguang Wu

Shunguang Wu is a senior professional staff at Johns Hopkins University Applied Physics Laboratory, and received his PhDs in theoretical physics and electrical engineering from Northwestern University (China) and Wright State University (USA), respectively. He published about 50 reviewed journal papers in the area of nonlinear dynamics, statistical signal processing and computer vision in his early career. His professional C++ experience started with teaching undergraduate courses in the late 1990s. Since then he has been designing and developing lots of R&D and end-user application software using C++ in world-class academic and industrial laboratories. These projects span both the Windows and Linux platforms.
Read more about Shunguang Wu