Week 7,8: Structural and Creational Patterns

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/41

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

42 Terms

1
New cards

What is a design pattern?

A time-tested, general solution to a recurring design problem, documented as a named problem-solution pair.

2
New cards

Where did the concept of design patterns originate?

From architecture: Christopher Alexander’s "Pattern Language" (1977).

3
New cards

What are the three categories of GoF patterns?

Creational (object creation), Structural (object composition), and Behavioral (object communication).

4
New cards

What is the purpose of the Façade pattern?

Provide a simplified, unified interface to a complex subsystem, reducing coupling.

5
New cards

What problem does the Façade pattern solve?

Direct coupling between clients and many subsystem interfaces makes code fragile and hard to change.

6
New cards

How does the Façade pattern solve this problem?

Introduce a facade object that wraps subsystem components and presents a tailored interface.

7
New cards

Which GoF category does Façade belong to?

Structural pattern.

8
New cards

Give an example of Façade in the POS rules engine context.

POSRuleEngineFacade exposes isInvalid(sli, sale) instead of clients calling each rule implementation directly.

9
New cards

Give an example of Façade in the ATM system context.

BankAccountFacade provides depositCash() and withdrawCash(), hiding account number check, security code, and funds check subsystems.

10
New cards

What is the purpose of the Decorator pattern?

Dynamically add responsibilities to individual objects without altering their interface.

11
New cards

What problem does the Decorator pattern solve?

Inheritance is static and applies to entire classes, not individual instances at runtime.

12
New cards

How does the Decorator pattern work?

Wrap the original object in a decorator object that implements the same interface and adds behavior recursively.

13
New cards

Which GoF category does Decorator belong to?

Structural pattern.

14
New cards

What is recursive composition in the Decorator pattern?

Each decorator holds a reference to a component (base or another decorator), allowing multiple layers of decoration.

15
New cards

Give an example of the Decorator pattern with shapes.

ShapeDecorator wraps a Rectangle, adding behaviors like setRedBorder() and setBlueFill() while preserving draw() interface.

16
New cards

What is the purpose of the Builder pattern?

Separate construction of a complex object from its representation and allow different representations using the same process.

17
New cards

What problem does the Builder pattern solve?

Complex multipart object creation with varying representations can bloat classes and mix construction logic.

18
New cards

How does the Builder pattern work?

Use a Director to control a Builder interface; ConcreteBuilders implement steps to build different products.

19
New cards

Which GoF category does Builder belong to?

Creational pattern.

20
New cards

Name the key roles in the Builder pattern.

Client, Director, Builder (interface), ConcreteBuilder(s), and Product.

21
New cards

Give an example of the Builder pattern for making juice.

JuiceMaker (Director) calls AppleJuiceMaker or OrangeJuiceMaker (ConcreteBuilders) to assemble juice parts in steps.

22
New cards

Give an example of the Builder pattern for creating travel packages.

TravelAgency (Director) uses CruisePackageBuilder or RoadTripBuilder to assemble tickets, hotels, activities, and meals.

23
New cards

What problem does the Adapter pattern solve?

It resolves incompatible interfaces by converting an external component’s interface into one the client expects.

24
New cards

What is the core solution of the Adapter pattern?

Introduce an intermediate adapter object that implements a stable interface and delegates calls to the varying external APIs.

25
New cards

Which category of GoF patterns does Adapter belong to?

Structural pattern.

26
New cards

How does the Adapter pattern relate to GRASP principles?

It is a form of Indirection and Pure Fabrication, uses Polymorphism, achieves Protected Variation, and supports Low Coupling.

27
New cards

In the POS system, what interface do tax adapter classes implement?

ITaxCalculatorAdapter with method getTaxes(Sale): List.

28
New cards

How does an Adapter decouple the POS from external services?

By hiding protocol and API differences behind a common interface, so the POS only depends on the adapter interface.

29
New cards

What common interface is defined in the Disney Madness example?

IDisneyAdapter with method move(), representing character movement.

30
New cards

What do TaxMasterAdapter and GoodAsGoldTaxProAdapter do?

They implement ITaxCalculatorAdapter to adapt their vendor-specific tax APIs to the POS’s expected interface.

31
New cards

What problem does the Concrete Factory pattern address?

Separating complex object-creation logic (e.g., adapter instantiation) into a cohesive helper class.

32
New cards

What are the advantages of using a Concrete Factory?

Hides complex creation, supports lazy instantiation, enables caching or recycling, and centralizes creation logic.

33
New cards

How can a factory be implemented in a data-driven way?

By reading adapter class names from a properties file and using reflection to instantiate them.

34
New cards

What issue arises when deciding who creates adapters?

Domain classes like Register shouldn’t create adapters, as adapter creation is not part of business logic.

35
New cards

What pattern solves the issue of adapter creation responsibility?

Use GRASP Pure Fabrication by encapsulating adapter creation in a Concrete Factory.

36
New cards

What problem does the Singleton pattern solve?

Ensures exactly one instance of a class and provides a global point of access to it.

37
New cards

How is the Singleton pattern typically implemented?

With a static synchronized getInstance() method that lazily initializes and returns the sole instance.

38
New cards

How is a Singleton depicted in UML class diagrams?

Static attributes/methods are underlined, and an optional '1' indicates only one instance.

39
New cards

How does the Register class access the singleton ServicesFactory?

By calling ServicesFactory.getInstance().getAccountingAdapter() inside its initialization.

40
New cards

Why aren’t all factory methods static?

Static methods can’t be overridden, hinder RMI support, and reduce flexibility if multiple instances become necessary.

41
New cards

How do Adapter, Factory, and Singleton patterns work together?

Adapter provides stable interfaces, Factory centralizes adapter creation, and Singleton ensures one Factory instance with global access.

42
New cards

Why is using a pattern language valuable in team-based software design?

It provides a shared vocabulary for succinctly explaining design reasoning and solutions among developers.