UNC COMP 301 - F26/Lec 11 - UML; Creational Design Patterns: Singleton, Multiton, Factory
Overview
The lecture introduces UML class diagrams, then compares three creational patterns: Singleton for one shared instance, Multiton for one instance per key, and Factory Method for centralizing subclass creation. It also revisits composition over inheritance, showing how two dog traits with three options each produce nine inheritance subclasses.
Key takeaways
- UML uses solid hollow-triangle arrows for inheritance, dotted hollow-triangle arrows for interface implementation, hollow diamonds for aggregation, and filled diamonds for composition.
- A Singleton combines a private constructor, a static instance field, and a public static accessor that creates the object only when the field is null.
- A Multiton is not limited to one object overall: it enforces one object per key, such as one Student per UNC PID, using a key-to-object map.
- A Factory Method centralizes the logic that chooses a subclass, reducing duplicated notification-preference branching in client code.
- Inheritance can produce m^n subclasses when n independent features each have m options; the Dog example reaches nine subclasses with only two three-option traits.
Chapters
- Muhammad Sayeed Ghani introduces UML as a way to visualize class structure as software and design patterns become more complex.
- The course will pair later code examples with UML diagrams where they help clarify relationships.
- The example adapts the university, course, professor, and student code from Lecture 6.
- A UML class rectangle has compartments for the class name, fields, and methods.
- Visibility symbols map to access modifiers: + public, - private, # protected, and ~ package default.
- Fields use name: type notation; methods list parameters with types and a return type, while constructors omit the return type.
- The adapted example adds a Person interface, an abstract PersonImple class, Student and Professor implementations, and a Roster class.
- Person declares getName and getStatus, while PersonImple stores a private name field and implements getName.
- The abstract base class shares common person behavior without being directly instantiated.
- UML marks an interface with an interface annotation and may omit its empty fields compartment.
- Italicized names indicate types that cannot be instantiated, including interfaces and abstract classes.
- A class implementing an interface connects to it with a dotted line ending in a hollow triangle.
- Student and Professor inherit shared behavior from PersonImple and implement the Person interface.
- Their UML boxes show class-specific members, such as Student credits and Professor promote, without repeating inherited fields.
- A solid line with a hollow triangle denotes inheritance and the is-a relationship.
- Course receives its Professor through its constructor, so the Professor exists independently and the relationship is aggregation.
- Course creates its Roster internally, making the Roster a composition that belongs to the Course.
- UML uses a hollow diamond for aggregation and a filled diamond for composition, placed at the owning or containing class end.
- Roster accepts existing Student objects rather than creating them, so its relationship to Student is aggregation.
- The multiplicity 0..* indicates that a Roster may contain zero or more Students; a Course has one Roster and one Professor.
- Main depends on the classes it creates, such as Course, Student, and Professor, shown with dotted arrows; the lecture's UML workflow moves from types to relationships and then dependencies.
- Creational patterns control object creation beyond ordinary use of Java's new keyword.
- Singleton fits a unique resource such as a camera or printer spooler, an expensive object such as a large database or language model, or a centralized application log.
- The pattern aims to make every reference share one instance rather than create independent copies.
- A public constructor lets any code create another FrontCamera, so the Singleton approach makes the constructor private.
- A public static create method provides access to construction without requiring an instance first.
- The static method alone is insufficient because repeated calls would still create repeated objects.
- FrontCamera stores a static field of its own type; create checks whether the field is null, constructs once, and returns the stored reference on later calls.
- Three calls to create produce three references to the same camera object, not three camera instances.
- The UML diagram marks the Singleton field and create method as static and connects FrontCamera to Camera with a dotted implementation arrow.
- Singleton can behave like a controlled global variable and is risky if future requirements call for multiple instances, such as phones with two front cameras.
- Multiton permits multiple objects overall but restricts each unique key to one corresponding object.
- A student system needs one Student per persistent identifier, such as a UNC PID, to avoid duplicate records when someone returns as a graduate student.
- The Student class keeps a static HashMap<Integer, Student> directory mapping each PID to its Student object.
- A static getStudent method checks containsKey before creating a student, preventing an existing PID from being associated with a second object.
- In the sample calls, PIDs 10 and 20 create two students; a repeated PID 20 returns the existing student or could be rejected with an exception.
- The notification example has an abstract Notification superclass and concrete TextNotification, EmailNotification, and PushNotification subclasses.
- Each Student has a notification preference represented by an enum, such as text, email, or push.
- Without a factory, client code repeatedly checks each student's preference and directly chooses a notification subclass, duplicating selection logic.
- A static factory method accepts a Student, reads the student's preference, and creates the matching notification subclass in one location.
- Client code passes Student objects to the factory instead of repeating branches that instantiate EmailNotification or TextNotification.
- The UML keeps TextNotification and EmailNotification inheriting from Notification, but moves the creation dependency from client code to Notification.
- Composition over inheritance means using composition or aggregation to combine features instead of creating a subclass for every feature combination.
- A Dog example with two traits, size and hair length, and three options per trait requires 3² = 9 subclasses if inheritance represents every combination.
- With m options across n features, the subclass count grows as m^n, motivating composition in later design patterns.
Transcript excerpt
0:01 Good afternoon everyone. So I hope your exam went well. Uh the grading is in process but I don't think the results will be out uh before the end of this week. So uh today we will uh start our discussion on the next major design pattern which is creational design pattern. However, uh as we get into more and more complex design patterns and more complex softwares, it becomes important to be able to visualize the different relationships between the different classes. And so what I'm going to do is spend some time talking about the unified modeling language or UML as it's
0:48 referred to. That should hopefully give you a better sense of how your code is overall structured. So let's take a look at UML and we'll start by taking a look at um a piece of code that I wrote um in lecture six. I'm going to modify that somewhat and show you an example um of a of a you know program that has an interface that has an abstract class. It has a couple of implementations uh of the of the interfaces and the inheritance relationships are there and as well as you know showing relationships of composition and aggregation. So hopefully that should be
1:36 useful and then what we'll do is for the rest of the semester wherever possible we'll also show you a UML diagram of the um code that we're taking a look at. So let's take a look at an example of UML. And so uh let's start by focusing in on one particular class. So how do you depict a particular class? You start by drawing a rectangle and you show uh the rectangle as having three compartments and these compartments uh start from the top with the class name.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Muhammad Sayeed Ghani.