FDQbJAJBPJBPKJBPKJABPKA?J{7{J{7B?iHfKqffKqfKqA%wiyy'yyyyyyyyyyAaAaEDCeBZZB|F6VBCCWW-

Factory Pattern.` A factory creates objects. We implement the factory design pattern in a C# program. With this pattern, we develop an abstraction that isolates the logic for determining which type of class to create. `Object `object`Example.` The factory design pattern relies on a type hierarchy. The classes must all implement an interface or derive from a base class. We use an abstract class as the base. The Manager, Clerk and Programmer classes derive from Position. `Interface `interface`Abstract `abstract`The factory design pattern` is found in the Factory class. The point of the Get method is to take a value and instantiate a class based on that value. It translates integers to objects with a switch statement. `Switch `switch`Because Manager, Clerk, and Programmer all derive from the same abstract class, the return type Position can be used.`An implicit cast automatically casts the Manager, Clerk and Programmer to Position references.`The Main method` serves to demonstrate the Factory class in action. We use as part of the demonstration the integers 0, 1, 2, and 3. We use the Get method with each of these values. `Then: `We show that the appropriate type of class was instantiated for each integer.`Discussion.` Imagine you have a system that needs to create objects in many different places. Suppose the system has integers and you want objects for those integers. The factory pattern is ideal for this usage. `You can use the Factory type to handle object creation in a uniform and concise way.`Summary.` We looked the factory design pattern, which is used to instantiate objects based on another data type such as integers. Factories can be used to reduce code bloat and also make it easier to modify which objects need to be created.

456Factory pattern e5: C#6 55555 { 4abstract 5Position 4{ 445abstract 5 Title { get; } 4} 45Manager : Position 4{ 445override 5 Title 44{ 444get 444{ 44445 "Manager"; 444} 44} 4} 45Clerk : Position 4{ 445override 5 Title 44{ 444get 444{ 44445 "Clerk"; 444} 44} 4} 455mer : Position 4{ 445override 5 Title 44{ 444get 444{ 44445 "5mer"; 444} 44} 4} 455Factory 4{6 44/5<5> 44/5Decides which 5to instantiate. 44/5</5> 44655Position Get(5id) 44{ 4445 (id) 444{ 444450: 444445 5Manager(); 444451: 444452: 444445 5Clerk(); 444453: 4444default: 444445 55mer(); 444} 44} 4} 454{ 445(5i50; i <= 3; i++) 44{ 444var position5Factory.Get(i); 4445"Where id5{0}, position5{1} ", i, position.Title); 44} 4} } 6 Where id50, position5Manager Where id51, position5Clerk Where id52, position5Clerk Where id53, position55mer6