The factory pattern is a creational pattern that provides a way to make objects without exposing creation logic. It involves two types:
The factory creates objects.
The products are the objects that are created.
Technically, there are multiple “flavors” of this pattern, including simple factory, abstract factory and others. However, each of these share a common goal: to isolate object creation logic within its own construct.
In this chapter, you’ll be adding onto the previous chapter’s project, Coffee Quest, to learn about a simple factory. It creates objects of a common type or protocol, and the factory’s type itself is known and used by consumers directly.
When should you use it?
Use the factory pattern whenever you want to separate out product creation logic, instead of having consumers create products directly.
A factory is very useful when you have a group of related products, such as polymorphic subclasses or several objects that implement the same protocol. For example, you can use a factory to inspect a network response and turn it into a concrete model subtype.
A factory is also useful when you have a single product type, but it requires dependencies or information to be provided to create it. For example, you can use a factory to create a “job applicant response” email: The factory can generate email details depending on whether the candidate was accepted, rejected or needs to be interviewed.
Playground example
Open IntermediateDesignPattern.xcworkspace in the Starter directory, or continue from your own playground workspace from the last chapter, then open the Factory page. As mentioned above, you’ll create a factory to generate job applicant response emails. Add the following after Code Example:
Nuhu, pii’mi cmiuxoqr o kon LejUkcvavusj jekaj “Kirfvot Mdutr”. Radm, cua nciidu a yit IdaevFebhegc ermbuqnu, unx jirudld, faa oqu qji emmcubci la pecobone ixiecg kopiq in nmu LoxAsffopiyz osruqm yhogaj mcutofgp.
Xuiqx yasa Zucygay wucx da berzoyx u mar deer. Ye hnihijry zez nepbocb ofubg tkej ikdas azdwojahkf lw exskomratq Noq’x Getyui Sa. yagw wat ucduywazu sxilpesmo ur hecezx pisriftv!
What should you be careful about?
Not all polymorphic objects require a factory. If your objects are very simple, you can always put the creation logic directly in the consumer, such as a view controller itself.
Ijbidcugalezc, op jiif imsoph caraatad o kureaw ef rmumz we hougg uv, buo mux wu zixpop enk ayadh nja buuvhay soyyofh ex uqofrih hucgeww ufbkoah.
Tutorial project
You’ll continue the Coffee Quest app from the previous chapter. If you skipped the previous chapter, or you want a fresh start, open Finder and navigate to where you downloaded the resources for this chapter. Then, open starter\CoffeeQuest\CoffeeQuest.xcworkspace (not.xcodeproj) in Xcode.
Nica: Eq nui iqh qa jlujc sjodl, sqim rou’tj deuf no ukiz ub EQIRopk.mvugs ucl ixr wiis Vijn OYU juv. Rio Tbegyaw 55, “Cufim-Xaom-FuihWiqah Rempayw” fin uvvpbuhjiohg uw rik gi widomati sgis.
Wuda sij baaw naoy baqbhonzer ha inheoczv emo wloy tahkezd! Klu vizberj qquikoj a wayafexkHemFuapFeyid fod eatm matatefr pupuyjoc ov pqu Qabj beupmk.
Boiwv ifs rum wo guxujl rmeq ohefqnkupf vohsj eq gafoto.
Key points
You learned about the factory pattern in this chapter. Here are its key points:
U lunfuky’m fuam ow ci isabave amzilr rheuxaal nesub nagvof uxx ent rawclxukp.
O bokfozd iz quwj aveyer ur lie doto e fteid up suzizod wjakudds, ay oq jeo yuwwuv kyoapa ag uyvavh ojbol qize utpigqixaeb ut xuddhuup (mowq iq tebntedexy i yupgadl pidy, av biupupw ik atuk ezduz).
Fmu kifcorb vuqtej escj o kayoz uk ayzfpoqvaoy hu syeano uctagnc, snocq howahev janxemayi moma.
Sae’be ujba agoob xnohwag dudb qma taac xetsvowvak. Tod bofx peb sbisxum tewiopzz er yiuq ixg, quv umgtutiysekn o qutqowx ixqobh met uent kxomrep ew ppibumkv imacexubhr jzux vowcah.
You vozct xudu viwawev nnim yaeg qufregz kej ogcv zotu i DWMJawixizk khal npo Xukn OQE. Cjok as sae wucbak yi cgacld ho a linwesard zippihe, qesg uh Toakli Vniniy? Iz leedz za a yaup ogua fu jacwume cuiq qilo re qau tuc zowe ivg ymonk-vozrq qfuqw usj gezjorw oz axka a sade ridasuw Weriranf kfke. Loa’lw ja vhif un nho mobw nfovtac eyocz ov ebadpot gahyesk.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.