Παρασκευή 10 Ιουλίου 2020

Abstract Factory (OOP)

M2000 has no abstract classes. But we can emulate them, because all modules and functions in a group (the object which the class function return) can be changed unless we state Final. So for abstract modules and functions we place an Error statement. This means we have to replace it to use it.

Classes are global functions as long as the module where defined running. The object's never need the classes to execute. Classes are for creating objects only.
Here we have a Creator abstract class because the factoryMethod is abstract (using the error statement, to emulate it).
The ConcreteCreator is a Creator which replace the factoryMethod with a final one.We can make another class as ConcreteCreator2 which is also a Creator but with different factoryMethod. Each one of them produce a specific Product at the Creator's anOperation. So anOperation make product, from some kinds of objects but all objects are type Product (and each one has some additional logic).



Class Product {
Class:
      Module Product {
            Report "A new Product"
      }
}
class Creator {
      module anOperation       {
            Report "An Operation of Creator - Use a specific to object factoryMethod"
            product = .factoryMethod()
      }
      function factoryMethod {
            Error "Not implemented yet "+module.name$
      }
Class:
      Module Creator {
            Report "A new Creator"
      }
}
class ConcreteProduct as Product {
class:
      module ConcreteProduct {
      \\ call Product module which is part of Product.
            .Product
            Report "A new ConcreteProduct is a Product"
      }


}
class ConcreteProduct2 as Product {
class:
      module ConcreteProduct2 {
      \\ call Product module which is part of Product.
            .Product
            Report "A new ConcreteProduct2 is a Product"
      }
}


class ConcreteCreator as Creator {
      function final factoryMethod {
            Print "factoryMethod 001"
            =ConcreteProduct()
      }
class:
      module ConcreteCreator {
            .Creator
            Report "A new ConcreteCreator is a Creator"
      }
}
class ConcreteCreator2 as Creator {
      function final factoryMethod {
            Print "factoryMethod 002"
            =ConcreteProduct2()
      }
class:
      module ConcreteCreator2 {
            .Creator
            Report "A new ConcreteCreator2 is a Creator"
      }
}
Module Client {
      Report 2, "ConcreteCreator attach a new factoryMethod to a creator class"
      creator = ConcreteCreator()
      creator.anOperation
      creator2= ConcreteCreator2()
      creator2.anOperation
}
Client

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.