Factory Method - An Overview - ScienceDirect Topics
Factory Method - An Overview - ScienceDirect Topics
Related terms:
Patterns
Martin Reddy, in API Design for C++, 2011
Tip
Your API should be closed to incompatible changes in its interface, but open to
extensibility of its functionality.
Entity creation with Abstract Factory introduces a loose coupling between the client
(e.g., DApp server) and specific smart contract implementations (i.e., the
Blockchain component). In particular, newly defined entity interactions can inherit
from the abstract contract to preserve common properties and have entity-specific
customizations. Because of the loose coupling, existing contracts remain
unmodified, minimizing contract deprecation. The downside of using this design
in a Blockchain, however, would be the extra storage (and therefore extra cost if
used in a public Blockchain with a mining incentive) overhead incurred by an
added layer of indirection for the abstract factory and its instantiation.
EXAMPLE
Figure 9.25 shows a class hierarchy for a bank domain that represents the relevant
(banking) values. Examples of domain values include the classes
AccountNumber, Amount, InterestRate, and Date (see Section 8.10).
Domain values are used in almost all frameworks of the business and product
domain layers as well as in the use context layer, as in the case of electronic forms,
modeling bank forms in classes. The fields of these forms are represented by
domain values.
A forms editor used to represent and edit forms on the screen requires appropriate
presentation forms to represent domain values (see Section 8.8). These
presentation forms utilize the specific representation and editing options for
domain values. For example, an amount with decimal places for thousands in a
bank application is separated by commas, and the sign is put after the amount.
Figure 9.25 also shows a class hierarchy, corresponding to the domain values and
presentation forms (PFs). A presentation form, LoanAmountPF, specialized for
the bank's loan business, represents the domain value Amount with a leading sign.
Figure 9.25 shows how the classes are distributed in the T&M model architecture.
For example, the classes LoanAmountPF and LoanApplication are part of
the Loan product domain. All other classes reside in the business domain layer,
because they represent core concepts for the product domains.
The example discussed in this section uses a forms editor for editing signature
forms. This forms editor has to create a matching presentation form for all domain
values it requests by calling the getFields method. We can identify the
following important points for this example:
• The forms editor contains a large case statement to create a suitable
presentation form, depending on the domain value type. Since the editor has
to create the specialized presentation form, LoanAmountPF, for a loan
amount, an invalid dependence to a class (LoanAmountPF) in a higher layer
is produced in the class FormsEditor. In addition, the case statement has to
be adapted for each extension of the domain value hierarchy. Each change to
the class hierarchy of the domain values can cause the business domain to be
opened.
• The domain values implement a so-called factory method (as proposed by
Gamma et al.), which creates an instance of the appropriate presentation form.
The allocation of a domain value to a presentation form cannot then be
dynamically configured. Instead, it is specified once and then used to create the
same presentation form in all applications when the factory method is called.
For example, if we allocate the presentation form AmountPF to the domain
value Amount in the factory method, then we wouldn't be able to adapt this
allocation for a loan application without having to change the code or set
additional parameters. Again, we would have to open the business domain.
• An abstract factory as proposed by Gamma et al., hides the creation of objects
from a client (FormsEditor, in this example), by providing an abstract
operation to create a matching presentation form for each domain value. A
specific factory implements abstract operations by creating the desired
presentation forms. Though this design pattern allows a specific factory for the
loan domain to allocate the domain value Amount to the presentation form
LoanAmountPF, it has a few problems. The FormsEditor has to
implement an expensive case statement to determine which operation of the
abstract factory has to be called, depending on the domain value type. New
domain values lead to new abstract operations in the abstract factory. Changes
to the domain value hierarchy or the presentation forms require us to open the
business domain, even when these changes are implemented in one single
product domain.
• Alternatively to using a factory, the forms editor could be specialized in each
product domain, with specific requirements to the representation and editing
of domain values. Unfortunately, this may cause problems when the forms
editor is required in a use context where specialized value representations from
several product domains are required. In this case, we would have to add
another specialization of the FormsEditor class in this use context. To
implement such a specialization, we would have to use multiple inheritance of
copy code. None of these two implementation methods is elegant.
Read full chapter
URL: https://www.sciencedirect.com/science/article/pii/B9781558606876500098
Extensibility
Martin Reddy, in API Design for C++, 2011
Encapsulation Smells
Girish Suryanarayana, ... Tushar Sharma, in Refactoring for Software Design
Smells, 2015
FIGURE 4.9. The class Encryption provides DES algorithm for encryption (Example
1).
FIGURE 4.10. The class Encryption provides both DES and AES algorithms for
encryption (Example 1).
In this design, there are two variation points: the kind of content, and the kind of
encryption algorithm that is supported. Every possible combination of these two
variations is captured in a dedicated class. For instance, DESImageEncryption
class deals with encrypting images using DES encryption algorithm. Similarly,
AESTextEncryption class deals with encrypting text using AES encryption
algorithm.
How does this design support a new type of content, a new encryption algorithm,
or both? Figure 4.12 shows how the existing design will be extended to support
TDES, a new encryption algorithm, and Data, a new type of content. Clearly, there
is an explosion of classes. This is because the variations in the implementation are
mixed together and have not been encapsulated separately, i.e., the design suffers
from Missing Encapsulation smell.
FIGURE 4.12. Class explosion in Encryption hierarchy when new kind of content
and algorithm are added (Example 2).
FIGURE 4.15. Suggested refactoring for Encryption class using Strategy pattern
(Example 1).
Introduction
Martin Reddy, in API Design for C++, 2011