Pure Fabrication Principle

(1) Problems

Who is responsible for handling this situation when you don’t want to break the design principles of high cohesion and low coupling?

(2) Scheme

Assign a set of highly cohesive responsibilities to a fictitious or easily handled “behavior” class, which is not a concept in the problem domain, but a fictitious transaction, to support high cohesion, low coupling, and reuse.

(3) Analysis

The pure fiction pattern is used to solve the contradiction between high cohesion and low coupling. It requires that the responsibilities of a part of the class be transferred to the pure fiction class. Ideally, the responsibilities assigned to this fiction class are for the purpose of high cohesion and low coupling. In the process of actual operation, there are many kinds of pure fiction is implemented, for example, the method of database operations spun off from a database entity class, formed a special data access class, through the decomposition of the class to implement class reuse, new data access classes corresponding to persistent storage, it is not the concept of the problem domain, It’s a fiction created by software developers for convenience. Pure fiction can eliminate the bad design of low cohesion and high coupling due to the information expert pattern, resulting in a design that is more reusable. Introducing abstract classes or interfaces into the system to improve the extensibility of the system can also be considered as an application of pure fiction mode. Pure fiction mode is usually a function-centered object or behavior object based on the division of related functions. Pure fiction patterns are found in many design patterns, such as adapter patterns, policy patterns, and so on.

The sample

public class MobileAdapter : Appliance.ITarget {

    public Voltage GetMobileVoltage() {
        var voltage = GetVoltage();
        Console.WriteLine($"Appliance voltage is {voltage.Value}V!");

        voltage.Value = 3; 
        Console.WriteLine($"After adapted,it becomes {voltage.Value}V!"); 

        returnvoltage; }}Copy the code

The above code can be found in the adapter pattern under structural pattern in my C# design pattern series.