mardi 1 novembre 2016

Is it possible to write a program for searching design patterns in source code?

I have some doubts about the possibility of writing a program like this because there are a lot of implementation of design patterns but if we consider as much as possible implementations I guess it can be done. And it can be useful for example for understanding already written source code. What do you think about it?

What is the difference between Two-way Adapter and Pluggable Adapter Pattern in C#?

Both Two-way Adapter and Pluggable Adapter can access both the classes and also change the behavior of the method which is required to be changed, the following is my code

Two-Way Adapter

 public interface IAircraft
    {
        bool Airborne { get; }
        void TakeOff();
        int Height { get; }
    }

    // Target
    public sealed class Aircraft : IAircraft
    {
        int height;
        bool airborne;
        public Aircraft()
        {
            height = 0;
            airborne = false;
        }
        public void TakeOff()
        {
            Console.WriteLine("Aircraft engine takeoff");
            airborne = true;
            height = 200; // Meters
        }
        public bool Airborne
        {
            get { return airborne; }
        }
        public int Height
        {
            get { return height; }
        }
    }
    // Adaptee interface
    public interface ISeacraft
    {
        int Speed { get; }
        void IncreaseRevs();
    }
    // Adaptee implementation
    public class Seacraft : ISeacraft
    {
        int speed = 0;
        public virtual void IncreaseRevs()
        {
            speed += 10;
            Console.WriteLine("Seacraft engine increases revs to " + speed + " knots");
        }
        public int Speed
        {
            get { return speed; }
        }
    }
    // Adapter
    public class Seabird : Seacraft, IAircraft
    {
        int height = 0;
        // A two-way adapter hides and routes the Target's methods
        // Use Seacraft instructions to implement this one
        public void TakeOff()
        {
            while (!Airborne)
                IncreaseRevs();
        }
        // Routes this straight back to the Aircraft
        public int Height
        {
            get { return height; }
        }

        // This method is common to both Target and Adaptee
        public override void IncreaseRevs()
        {
            base.IncreaseRevs();
            if (Speed > 40)
                height += 100;
        }
        public bool Airborne
        {
            get { return height > 50; }
        }
    }
    class Experiment_MakeSeaBirdFly
    {
        static void Main()
        {
            // No adapter
            Console.WriteLine("Experiment 1: test the aircraft engine");
            IAircraft aircraft = new Aircraft();
            aircraft.TakeOff();
            if (aircraft.Airborne) Console.WriteLine(
            "The aircraft engine is fine, flying at "
            + aircraft.Height + "meters");
            // Classic usage of an adapter
            Console.WriteLine("\nExperiment 2: Use the engine in the Seabird");
            IAircraft seabird = new Seabird();
            seabird.TakeOff(); // And automatically increases speed
            Console.WriteLine("The Seabird took off");
            // Two-way adapter: using seacraft instructions on an IAircraft object
            // (where they are not in the IAircraft interface)
            Console.WriteLine("\nExperiment 3: Increase the speed of the Seabird:");
            (seabird as ISeacraft).IncreaseRevs();
            (seabird as ISeacraft).IncreaseRevs();
            if (seabird.Airborne)
                Console.WriteLine("Seabird flying at height " + seabird.Height +
                " meters and speed " + (seabird as ISeacraft).Speed + " knots");
            Console.WriteLine("Experiments successful; the Seabird flies!");

            Console.Read();
        }
    }

Pluggable Pattern

class Adaptee
    {
        public double Precise(double a, double b)
        {
            return a / b;
        }
    }

    // New standard for requests
    class Target
    {
        public string Estimate(int i)
        {
            return "Estimate is " + (int)Math.Round(i / 3.0);
        }
    }    

    // Implementing new requests via old
    class Adapter : Adaptee
    {
        public Func<int, string> Request;    
        // Different constructors for the expected targets/adaptees    
        // Adapter-Adaptee
        public Adapter(Adaptee adaptee)
        {
            // Set the delegate to the new standard
            Request = x =>
            {
                return "Estimate based on precision is " +
               (int)Math.Round(Precise(x, 3));
            };
        }

        // Adapter-Target
        public Adapter(Target target)
        {
            // Set the delegate to the existing standard
            Request = target.Estimate;
        }
    }

    class Client
    {    
        static void Main()
        {    
            Adapter adapter1 = new Adapter(new Adaptee());
            Console.WriteLine(adapter1.Request(5));

            Adapter adapter2 = new Adapter(new Target());
            Console.WriteLine(adapter2.Request(5));    
            Console.Read();

        }
    }

in the above two code i dont find anything different in respect of the patterns functionality. So what is the different between both the patterns, can anyone help me understand it? thank you

Advice on using (or not using) singletons

I'm working on a multiplayer game iOS app and I've come to a point in the design where it seems that I would benefit from creating two singletons but I don't know if there is a superior design pattern.

  1. A live game singleton: to get/set properties and start/stop the game from anywhere in the app.
  2. A location manager singleton: to manage user location updates/properties from anywhere in the app but especially from inside of the live game singleton because the game mechanics affect the properties of the location manager.

And if you are wondering do I really need to interact with these objects from absolutely anywhere in the app, the answer is a definite yes. This is a live multiplayer experience so everything related to gameplay needs to persist cleanly throughout the app's lifecycle.

So would this be a correct use of the singleton pattern or is there a better method, and if there is why is it better?

Wrapping a class with additional features outside the project

I have a class in Project1 class library

class MyClass
{

[CustomAttribute]
public string prop1 {get;set;}

public string prop2 {get;set;}

}

I have: CustomAttribute depends on a 3rd party library which I dont want to include within MyClass.

Is there a way in which I can wrap the CustomAttribute of MyClass with prop1 outside the project1 and in a new wrapper project2 that includes the Project1 ?

Thanks

Implement interface with Disposable Pattern

Note: This is a Visual Studio 2015 feature only, as far as I know...

How can I edit the Implement interface with Disposable Pattern option?

Implement Interface with Disposable Pattern

This is NOT a question on how to Implement the Dispose Pattern and its variations, but how to edit the option "Implement interface with Disposable Pattern"

I would like to remove some comments and add some code on the Disposable Pattern, but I want it to be saved and every time I click "Implement interface with Disposable Pattern", it implements my code, not the default one. Also I would like to create my own "Implement interface with MyOwn Pattern" option and target it to a interface (not limited to IDisposable)

I have tried to do it with code snippet, but it doesn't work, I can't create or edit a "Implement interface with XXX" option.

It seems that this option is kind of "built in", but it seems like a "extensible" option...

What are the best designing patterns we should follow while programming in android

Any help would be appreciable as i want to know best practices for designing patterns.

Extending base class vs. instantiating base class

I'm wondering if it's better to extend my base class some times or if I should just instantiate an object of my base class.

Option 1:

public abstract class BaseService
{
    public void DoSomething()
    {
        ...
    }
}

public class UserService : BaseService
{
}

public class MyApp
{
    public void Run()
    {
        UserService userService = new UserService();
        userService.DoSomething();
    }
}

Option 2:

public class BaseService
{
    public void DoSomething()
    {
        ...
    }
}

public class MyApp
{
    public void Run()
    {
        BaseService userService = new BaseService ();
        userService.DoSomething();
    }
}

Which option should be preferred? Option 1 has the advantage that the UserService can extend the BaseService with "user specific" methods, also my base class could have abstract methods - let's assume this is not needed (at the moment).

I'm sure this topic has been discussed before but seems like I'm searching with the wrong keywords.