Skip to main content

VBA versus .NET

Factory Method

Following is an example of the Factory Method.  In this case, a method call sets and returns the type.  Also, pardon the values set as field, rather than as properties, but this is a simple example, rather than a full implementation.


Salient Point(s)


  • A hidden constructor
  • Each class method instantiates the class differently.


Code




using System;


namespace DesignPatterns
{   
    //primary class for factory method
    public class FactoryObject
    {
        //public fields, set as part of method and return
        public int X;
        public int Y;
        public int Result;


        //factory method
        public FactoryObject Addition(int x, int y)
        {
            return new FactoryObject(x, y, x + y);
        }


        //factory method
        public FactoryObject Subtaction(int x, int y)
        {
            return new FactoryObject(x, y, x - y);
        }


        //factory method
        public FactoryObject Multiplication(int x, int y)
        {
            return new FactoryObject(x, y, x * y);
        }


        //factory method
        public FactoryObject Division(int x, int y)
        {
            return new FactoryObject(x, y, (y == 0) ? 0 : (x / y));
        }


        //constructor is private
        //object is returned via factory methods
        private FactoryObject(int x, int y, int result)
        {
            this.X = x;
            this.Y = y;
            this.Result = result;
        }
    }
}

Comments

Popular posts from this blog

James Igoe's Reviews > Thinking Architecturally

Thinking Architecturally by Nathaniel Schutta My rating: 4 of 5 stars An overview of architectural decisions, the politics and persuasion involved, and the needs to balance competing measures and attributes. A fairly easy read, but full of great suggestions, and, for many, reminders of how to handle being a senior developer or architect. View all my reviews

How do you deal with making sure your use of new technology is correct and free from code-smells, security issues, etc.? - Hashnode

Responding to How do you deal with making sure your use of new technology is correct and free from code-smells, security issues, etc.? : Issues can be dealt with in several ways. Understanding what makes high-quality, maintainable code would be first, so knowledge of best practices regarding OOP, SOLID, design patterns, API design, etc. is important. Depending on what you mean by security, best practices in those regarding transfer protocols, coding styles, validation, storage, etc. are equally something one can learn. Planning your work is useful, as a well thought out design is easier to implement, or at least will avoid future problems, than when you are just 'winging it'. Diagramming and project plans can be useful at this stage. Self-management is part of this, so using boards and epic/stories/tasks to track work is important, and there are free tools like Visual Studio Team Services (VSTS) or Trello to help. Requirements gathering will matter so documentation and c...

Do Algorithms Make You a Better Developer?

Responding to a question on HashNode, Developers who practise algorithms are better at software development than people who just do development. Is it true? , I wrote the following: My feeling is that algorithms help make one a better programmer, but that is likely true of many coding concepts. I did not have algorithms as an undergraduate, so my knowledge is acquired through reading and practice, but after reading and applying Algorithm's in a Nutshell, I felt the quality of my work improved. That said, my development work increased more after understanding Design Patterns, or after consuming books on database design.  Since many types of knowledge improve developing and architecting abilities, one has to consider how it helps and to what degree. Algorithms are coding-in-the-small, often narrowly focused solutions, but which can have a great impact at scale. For many applications, a focus on algorithms would be overkill as data sets and requirements do not require it. In this ...